File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ name : Cache Nginx CI
2+ on :
3+ push :
4+ branches :
5+ - main
6+ paths :
7+ - .github/workflows/cache-nginx-ci.yaml
8+ - cache-nginx/**
9+ env :
10+ IMAGE : ghcr.io/cloudwebmanage/cwm-cdn-api-cache-nginx
11+ jobs :
12+ ci :
13+ runs-on : ubuntu-latest
14+ steps :
15+ - uses : actions/checkout@v4
16+ - uses : docker/login-action@v3
17+ with :
18+ registry : ghcr.io
19+ username : ${{ github.actor }}
20+ password : ${{ secrets.GITHUB_TOKEN }}
21+ - uses : docker/setup-buildx-action@v3
22+ - id : docker_build
23+ uses : docker/build-push-action@v6
24+ with :
25+ context : cache-nginx
26+ push : true
27+ tags : |
28+ ${{ env.IMAGE }}:latest
29+ ${{ env.IMAGE }}:${{ github.sha }}
30+ cache-from : type=registry,ref=${{ env.IMAGE }}:buildcache-latest
31+ cache-to : type=registry,ref=${{ env.IMAGE }}:buildcache-latest,mode=max
Original file line number Diff line number Diff line change 66 paths-ignore :
77 - .github/workflows/nginx-ci.yaml
88 - tenant-nginx
9+ - .github/workflows/cache-nginx-ci.yaml
10+ - cache-nginx/**
911env :
1012 IMAGE : ghcr.io/cloudwebmanage/cwm-cdn-api
1113 # these should match the versions in Dockerfile
Original file line number Diff line number Diff line change 1+ FROM nginx:alpine@sha256:42a516af16b852e33b7682d5ef8acbd5d13fe08fecadc7ed98605ba5e3b26ab8
2+ RUN apk update && apk add python3
3+ COPY entrypoint.py /docker-entrypoint.d/999-cache-nginx-py.sh
Original file line number Diff line number Diff line change 1+ #!/usr/bin/python3
2+ import os
3+
4+
5+ TYPE = os .environ .get ("TYPE" , "router" ) # router or cache
6+ NUM_CACHE_SERVERS = int (os .environ .get ("NUM_CACHE_SERVERS" , "3" ))
7+
8+
9+ DEFAULT_CONF_ROUTER_TEMPLATE = '''
10+ upstream cache {
11+ hash $http_x_cwmcdn_tenant_name$request_uri consistent;
12+ __SERVERS__
13+ }
14+
15+ server {
16+ listen 80;
17+ server_name _;
18+ location / {
19+ proxy_pass http://cache;
20+ }
21+ }
22+ '''
23+
24+ DEFAULT_CONF_CACHE_TEMPLATE = '''
25+ server {
26+ listen 80;
27+ server_name _;
28+ location / {
29+ proxy_pass http://tenant.$http_x_cwmcdn_tenant_name;
30+ }
31+ }
32+ '''
33+
34+
35+ def main ():
36+ if not TYPE or not NUM_CACHE_SERVERS :
37+ raise Exception ("TYPE, NUM_CACHE_SERVERS environment variables must be set" )
38+ if TYPE == "router" :
39+ default_conf = DEFAULT_CONF_ROUTER_TEMPLATE
40+ servers_conf = []
41+ for i in range (1 , NUM_CACHE_SERVERS + 1 ):
42+ servers_conf .append (f' server cache{ i } :80;' )
43+ default_conf = default_conf .replace ("__SERVERS__" , "\n " .join (servers_conf ))
44+ elif TYPE == "cache" :
45+ default_conf = DEFAULT_CONF_CACHE_TEMPLATE
46+ else :
47+ raise Exception ("TYPE must be 'router' or 'cache'" )
48+ with open ("/etc/nginx/conf.d/default.conf" , "w" ) as f :
49+ f .write (default_conf )
50+
51+
52+ if __name__ == "__main__" :
53+ main ()
You can’t perform that action at this time.
0 commit comments