Skip to content

ci: deploy only after CI has passed for the commit - #246

Merged
TheCodeHeist-Coder merged 1 commit into
mainfrom
ci/gate-deploy-on-ci
Nov 22, 2026
Merged

TheCodeHeist-Coder merged 1 commit into
mainfrom
ci/gate-deploy-on-ci

Conversation

@TheCodeHeist-Coder

Copy link
Copy Markdown
Owner

Closes the code half of #241.

What does this PR do?

CD triggered on any push to main with nothing connecting it to CI, so the two ran in parallel — and the deploy normally finished first. A commit whose tests failed still reached rexial.in.

That made every check in CI advisory: the type gate, the tests across five services, the secret scan, the compose validation. All of it reported on something already live.

CD now triggers from CI completing on main, and refuses to run unless the conclusion was success:

on:
  workflow_run:
    workflows: [CI]
    types: [completed]
    branches: [main]
if: >-
  github.event_name == 'workflow_dispatch' ||
  github.event.workflow_run.conclusion == 'success'

Behaviour across every CI outcome:

CI result Deploys
success yes
failure no
cancelled no
timed_out no
manual redeploy (workflow_dispatch) yes, unchanged

The part worth reviewing

Under workflow_run, github.sha is the default branch tip, not the commit CI tested. Those are the same thing most of the time and different exactly when it matters — a second merge landing while the first is still deploying.

Left alone, that would have built images from one commit while the server reset to another. So every job now pins a single DEPLOY_SHA:

DEPLOY_SHA: ${{ github.event.workflow_run.head_sha || github.sha }}

including the actions/checkout ref, the image tags, the Trivy scan target, the git reset --hard on the server and IMAGE_TAG. The fallback keeps manual redeploys working, since those have no triggering run.

Not in this PR

Branch protection on main is the other half of #241 and is a repository setting rather than code — requiring a pull request, requiring the CI checks, and disallowing force pushes. Without it, a direct push to main still lands; it just will not deploy unless CI passes.

Leaving #241 open until that side is done too.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Refactoring
  • Performance
  • Tests
  • Build / CI / deployment

How did you test this?

  • yaml.safe_load on the workflow, and the parsed structure checked: triggers, the if guard, and that deploy still needs: build-and-push so the guard propagates.
  • The guard expression evaluated against all five outcomes above.
  • Confirmed the CI workflow is named CI and runs on push to main, so workflow_run will fire.
  • Confirmed no bare github.sha remains outside the DEPLOY_SHA definition.

The first merge after this lands is the real test: CD should start only once CI reports green, and the logs should name the same SHA throughout.

CD triggered on any push to main with nothing connecting it to CI, so the
two ran in parallel and the deploy normally finished first. A commit whose
tests failed still reached rexial.in, which made every check in CI —
the type gate, the tests across five services, the secret scan — advisory:
they reported on something already live.

Trigger CD from CI completing on main instead, and refuse to run unless
the conclusion was success. A manual redeploy has no triggering run, so
the guard allows workflow_dispatch explicitly and that path is unchanged.

Under workflow_run, github.sha is the default branch tip rather than the
commit CI tested. Those are the same thing most of the time and different
exactly when it matters — a second merge landing while the first is still
deploying. Every job now pins DEPLOY_SHA, including the checkout, so the
images built, the images pulled and the commit the server resets to all
refer to the commit that actually passed.

Branch protection on main is the other half of this and is a repository
setting rather than code; tracked in #241.
@TheCodeHeist-Coder
TheCodeHeist-Coder merged commit 0740d72 into main Nov 22, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant