Skip to content

Commit 55244c6

Browse files
committed
cache nginx
1 parent a181e1a commit 55244c6

4 files changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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

.github/workflows/ci.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ on:
66
paths-ignore:
77
- .github/workflows/nginx-ci.yaml
88
- tenant-nginx
9+
- .github/workflows/cache-nginx-ci.yaml
10+
- cache-nginx/**
911
env:
1012
IMAGE: ghcr.io/cloudwebmanage/cwm-cdn-api
1113
# these should match the versions in Dockerfile

cache-nginx/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
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

cache-nginx/entrypoint.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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()

0 commit comments

Comments
 (0)