name: Redeploy Nginx # Infra files only (spec-v4-deploy-infra D1): a change to the nginx template or # the compose definition redeploys the container — content commits never do. # workflow_dispatch is the token-rotation runbook. The Basic-auth file is no # longer repository content (spec-v6 D16): the HTPASSWD_CONTENT secret carries # it and the job writes it on the server, so credential rotation is a secret # edit + a dispatch, never a commit. on: push: branches: - master paths: - nginx.conf - compose.yaml workflow_dispatch: concurrency: group: deploy-infra cancel-in-progress: false jobs: redeploy: runs-on: ubuntu-latest permissions: contents: read steps: - uses: actions/checkout@v7 # The stack directory and the container name are the server's deployment layout, # not repository content: both come from repository variables (spec-v6 D15), and # STACK_DIR is the same value `deploy.yml` uses. A missing STACK_DIR would rsync # the two infra files into the remote user's home and then run docker in the # wrong place; a CONTAINER_NAME that disagrees with the service this repository # ships would recreate nothing and probe a container that does not exist. Both are # refused here — before any rsync, before anything reaches the server. - name: Check stack variables env: STACK_DIR: ${{ vars.STACK_DIR }} CONTAINER_NAME: ${{ vars.CONTAINER_NAME }} run: | case "$STACK_DIR" in /*) ;; *) echo "::error::STACK_DIR is not an absolute path — add it as a repository variable (Settings → Secrets and variables → Variables), value: the Dockge stack directory, e.g. /opt/stacks/dong-site" exit 1 ;; esac case "$STACK_DIR" in */) echo "::error::STACK_DIR must not end with / — this job appends the slashes it needs" exit 1 ;; esac # The name is used twice over: as the compose service to recreate, and as the # docker target of the health probe. compose.yaml — this repository — declares # both, so the variable is checked against the file it has to agree with. if ! grep -qE "^ ${CONTAINER_NAME}:" compose.yaml; then echo "::error::CONTAINER_NAME is not a service in compose.yaml — the recreate step names a compose service, so the two must match" exit 1 fi if ! grep -qF "container_name: ${CONTAINER_NAME}" compose.yaml; then echo "::error::CONTAINER_NAME does not match container_name in compose.yaml — docker exec and docker logs target that name" exit 1 fi echo "Stack: $STACK_DIR, container: $CONTAINER_NAME" # Ship exactly the two triggered files. Never .env, never secrets/ (D6). # The action builds the rsync source as "$GITHUB_WORKSPACE/$path" and passes # it as one argument (entrypoint.sh) — so NO --relative: -R mirrors the whole # absolute workspace path onto the server (run 2026-10-23 created # dong-site/workspace/dong.nguyen/cv/… on the VPS). Plain -avz lands each # file's basename in remote_path. - name: Sync nginx.conf uses: burnett01/rsync-deployments@v9 with: switches: -avz path: nginx.conf remote_path: ${{ vars.STACK_DIR }}/ remote_host: ${{ secrets.DEPLOY_HOST }} remote_port: ${{ secrets.DEPLOY_SSH_PORT }} remote_user: ${{ secrets.DEPLOY_USER }} remote_key: ${{ secrets.DEPLOY_SSH_PRIVATE_KEY }} - name: Sync compose.yaml uses: burnett01/rsync-deployments@v9 with: switches: -avz path: compose.yaml remote_path: ${{ vars.STACK_DIR }}/ remote_host: ${{ secrets.DEPLOY_HOST }} remote_port: ${{ secrets.DEPLOY_SSH_PORT }} remote_user: ${{ secrets.DEPLOY_USER }} remote_key: ${{ secrets.DEPLOY_SSH_PRIVATE_KEY }} # The Basic-auth file is repository-secret content, not repository content # (spec-v6 D16): the HTPASSWD_CONTENT secret carries the one htpasswd line # (user + bcrypt hash). The secret is inlined into a single-quoted shell # literal — a bcrypt hash is riddled with `$` and appleboy's `envs:` # mechanism exports values unquoted, so the remote shell would expand `$2y…` # away; the htpasswd character set (user name + bcrypt base64) contains no # single quote, so this quoting is exact. Written BEFORE the test step: # nginx -t opens auth_basic_user_file, so its mount needs a real file. # chmod 644, NOT 600: nginx verifies Basic auth in the worker process — # the image's unprivileged nginx user (uid 101) — and opens # auth_basic_user_file per request, so the file must be readable by a uid # other than the deploy user's. A 600 file made the live site answer # open() … 13: Permission denied on /api/content/ (2026-10-28): nginx -t # and the wget probe run as root, which ignores DAC permissions, and / has # no auth — only a real Basic-auth request reads the file as uid 101. The # file holds a bcrypt hash only; 644 is the mode production ran with while # it was rsynced from the repository. - name: Write deploy/htpasswd from the secret uses: appleboy/ssh-action@v1.2.0 with: host: ${{ secrets.DEPLOY_HOST }} port: ${{ secrets.DEPLOY_SSH_PORT }} username: ${{ secrets.DEPLOY_USER }} key: ${{ secrets.DEPLOY_SSH_PRIVATE_KEY }} script: | cd "${{ vars.STACK_DIR }}" HTPASSWD='${{ secrets.HTPASSWD_CONTENT }}' if [ -z "$HTPASSWD" ]; then echo "HTPASSWD_CONTENT is not set as a repository secret — add the output of: htpasswd -nbB ''" >&2 exit 1 fi mkdir -p deploy printf '%s\n' "$HTPASSWD" > deploy/htpasswd chmod 644 deploy/htpasswd # Test-before-touch (D3): the throwaway container runs the same entrypoint # render pipeline as production, with placeholder values — nginx -t checks # syntax only, so the real token never enters this container or the job. # A failure here fails the job; the live container keeps serving. # The placeholder must be a resolvable literal IP: a literal hostname in # proxy_pass IS resolved while the config is parsed (spec-v4-deploy-infra §4). - name: Test new config uses: appleboy/ssh-action@v1.2.0 with: host: ${{ secrets.DEPLOY_HOST }} port: ${{ secrets.DEPLOY_SSH_PORT }} username: ${{ secrets.DEPLOY_USER }} key: ${{ secrets.DEPLOY_SSH_PRIVATE_KEY }} script: | cd "${{ vars.STACK_DIR }}" docker run --rm \ -v "$PWD/nginx.conf:/etc/nginx/templates/default.conf.template:ro" \ -v "$PWD/deploy/htpasswd:/etc/nginx/htpasswd:ro" \ -e CONTENT_API_TOKEN=placeholder \ -e CONTENT_API_BASE=http://127.0.0.1:1 \ -e 'NGINX_ENVSUBST_FILTER=CONTENT_API_TOKEN|CONTENT_API_BASE' \ nginx:alpine nginx -t # Apply + verify (D4): recreate is the same command the Dockge deploy # button runs, with the service/container names from CONTAINER_NAME — already # checked against compose.yaml above. The health probe uses wget because # nginx:alpine carries no curl; on failure the job prints the container log and # fails. - name: Recreate and probe uses: appleboy/ssh-action@v1.2.0 with: host: ${{ secrets.DEPLOY_HOST }} port: ${{ secrets.DEPLOY_SSH_PORT }} username: ${{ secrets.DEPLOY_USER }} key: ${{ secrets.DEPLOY_SSH_PRIVATE_KEY }} script: | cd "${{ vars.STACK_DIR }}" docker compose up -d --force-recreate "${{ vars.CONTAINER_NAME }}" sleep 2 if ! docker exec "${{ vars.CONTAINER_NAME }}" wget -q -O /dev/null http://127.0.0.1/; then docker logs --tail 50 "${{ vars.CONTAINER_NAME }}" exit 1 fi docker exec "${{ vars.CONTAINER_NAME }}" nginx -t