CI/CD: gate deploys on tests and security, deploy the exact tested commit, verify and roll back #55

Open
opened 2026-11-22 15:26:41 -05:00 by jared · 0 comments
Owner

This came out of the CI/CD work on Pulse (LotusGuild/pulse#45). Pulse's pipeline is in .gitea/workflows/ci.yml there, and its server-side deploy script is deploy/pulse_deploy.sh. The proven pieces can be copied from both.

Problems in Gandalf's CI today

  1. Deploy is gated on lint only. In lint.yml, deploy has needs: [python-lint, js-lint]. pytest (test.yml) and bandit (security.yml) are separate workflows, so a commit with failing tests or a bandit finding still deploys.
  2. The deploy isn't pinned or verified. The signed payload is just {"ref"}:
    • the same signature can be replayed forever;
    • the server deploys whatever main is when the hook runs, not the commit CI tested;
    • CI never checks that the new version came up, and nothing rolls back.
  3. Actions aren't pinned. actions/checkout@v3 is a moving tag, and old.
  4. Unpinned tools, and possibly a test environment that doesn't match production. npm install eslint@8 doesn't use the lockfile, and flake8, bandit and pytest are pip-installed unpinned. The Python version comes from whatever the runner image has, and may differ from production.
  5. Failure alerts only cover lint. pytest and bandit failures on main send nothing.

Fixes that worked in Pulse

  • One ci.yml: jobs lint, test, security and deploy, with deploy needing all checks, plus a notify job. Permissions contents: read by default and contents: write only on deploy. A concurrency group so deploys queue and are never cancelled mid-way.

  • Pinned actions and tools: actions pinned to full SHAs, e.g. actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0. Pinned versions of flake8, bandit and pytest (a requirements-dev.txt). npm ci.

  • Pin the job image (runner quirk): set container: python:3.X-bookworm to production's Python version, instead of apt-installing Python on each run.

    • Don't use actions/setup-python or setup-node. The runner shares one tool cache volume between concurrent jobs, and parallel setup actions overwrite each other's installs. That broke Pulse's CI twice.
    • Service containers aren't reachable by name either, because jobs run on Docker's default bridge network. Install any test database inside the job container.
  • Deploy the exact sha (pulse_deploy.sh pattern):

    • The payload is {"ref","sha","ts"}, HMAC-signed.
    • The webhook passes sha and ts to the script via pass-arguments-to-command.
    • The script refuses requests older than 5 minutes, and any ts that isn't newer than the last accepted one (replay protection).
    • It checks that the sha is on origin/main, then git checkout --detach <sha>, installs, restarts and polls a health endpoint for that sha, and rolls back to the previous commit if it never reports healthy.
    • CI then polls the same endpoint until production reports the sha, and only then tags deploy-*.
    • This needs a /healthz (or similar) that returns the running git sha. Pulse reads it once at startup with git rev-parse HEAD.
  • Scan job:

    • gitleaks over the full history (fetch-depth: 0, --redact);
    • actionlint on .gitea/workflows/*.yml;
    • shellcheck on *.sh.

    Tool downloads are pinned and checked with SHA-256. Run a local dry run first so the job starts green.

  • Branch protection: require every check, using the job names as CI / <job name> (push).

Pulse doesn't depend on any of this; it's just parity. No changes to Pulse's API are involved.

This came out of the CI/CD work on Pulse (LotusGuild/pulse#45). Pulse's pipeline is in `.gitea/workflows/ci.yml` there, and its server-side deploy script is `deploy/pulse_deploy.sh`. The proven pieces can be copied from both. ## Problems in Gandalf's CI today 1. **Deploy is gated on lint only.** In `lint.yml`, `deploy` has `needs: [python-lint, js-lint]`. pytest (`test.yml`) and bandit (`security.yml`) are separate workflows, so a commit with failing tests or a bandit finding still deploys. 2. **The deploy isn't pinned or verified.** The signed payload is just `{"ref"}`: - the same signature can be replayed forever; - the server deploys whatever `main` is when the hook runs, not the commit CI tested; - CI never checks that the new version came up, and nothing rolls back. 3. **Actions aren't pinned.** `actions/checkout@v3` is a moving tag, and old. 4. **Unpinned tools, and possibly a test environment that doesn't match production.** `npm install eslint@8` doesn't use the lockfile, and flake8, bandit and pytest are pip-installed unpinned. The Python version comes from whatever the runner image has, and may differ from production. 5. **Failure alerts only cover lint.** pytest and bandit failures on `main` send nothing. ## Fixes that worked in Pulse - **One `ci.yml`:** jobs lint, test, security and deploy, with `deploy` needing all checks, plus a notify job. Permissions `contents: read` by default and `contents: write` only on deploy. A `concurrency` group so deploys queue and are never cancelled mid-way. - **Pinned actions and tools:** actions pinned to full SHAs, e.g. `actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0`. Pinned versions of flake8, bandit and pytest (a `requirements-dev.txt`). `npm ci`. - **Pin the job image (runner quirk):** set `container: python:3.X-bookworm` to production's Python version, instead of apt-installing Python on each run. - Don't use `actions/setup-python` or `setup-node`. The runner shares one tool cache volume between concurrent jobs, and parallel setup actions overwrite each other's installs. That broke Pulse's CI twice. - Service containers aren't reachable by name either, because jobs run on Docker's default bridge network. Install any test database inside the job container. - **Deploy the exact sha (`pulse_deploy.sh` pattern):** - The payload is `{"ref","sha","ts"}`, HMAC-signed. - The webhook passes `sha` and `ts` to the script via `pass-arguments-to-command`. - The script refuses requests older than 5 minutes, and any `ts` that isn't newer than the last accepted one (replay protection). - It checks that the sha is on `origin/main`, then `git checkout --detach <sha>`, installs, restarts and polls a health endpoint for that sha, and rolls back to the previous commit if it never reports healthy. - CI then polls the same endpoint until production reports the sha, and only then tags `deploy-*`. - This needs a `/healthz` (or similar) that returns the running git sha. Pulse reads it once at startup with `git rev-parse HEAD`. - **Scan job:** - gitleaks over the full history (`fetch-depth: 0`, `--redact`); - actionlint on `.gitea/workflows/*.yml`; - shellcheck on `*.sh`. Tool downloads are pinned and checked with SHA-256. Run a local dry run first so the job starts green. - **Branch protection:** require every check, using the job names as `CI / <job name> (push)`. Pulse doesn't depend on any of this; it's just parity. No changes to Pulse's API are involved.
Sign in to join this conversation.
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: LotusGuild/gandalf#55