Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

ci(docker): move the :main tag only for the tested branch head

Yudhi Armyndharisd17834c59b21
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index e0cb5febb..014497359 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -13,6 +13,14 @@ env:
permissions:
contents: read
+# A new push to a pull request cancels that pull request's superseded run. Push runs each get a group
+# of their own (run_id), so pushes to main never queue behind or cancel one another and
+# every commit keeps its tests and its `:<sha>` image; the docker job's last step decides which of
+# them the branch tag follows.
+concurrency:
+ group: ci-${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.pull_request.number) || github.run_id }}
+ cancel-in-progress: true
+
jobs:
lint:
name: Lint
@@ -442,15 +450,17 @@ jobs:
uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6
with:
images: ghcr.io/${{ github.repository }}
- # Branch + SHA tags only. `latest` is deliberately NOT set here: re-pointing it on
- # every main push would bypass the boot-smoke → promote discipline in release.yml
- # (a main-push image is build-gated only, never boot-tested). `latest` moves exclusively
- # through the tested release path.
+ # SHA tag only. The branch tag (`:main`) is applied by the last step of this job,
+ # after the non-root smoke passes and only while this commit is still the branch head, so
+ # a slower run for an older commit cannot re-point it. `latest` is deliberately NOT set
+ # here: re-pointing it on every main push would bypass the boot-smoke → promote
+ # discipline in release.yml (a main-push image is build-gated only, never boot-tested).
+ # `latest` moves exclusively through the tested release path.
tags: |
- type=ref,event=branch
type=sha,prefix=
- name: Build and optionally push
+ id: build
uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7
with:
context: .
@@ -492,3 +502,20 @@ jobs:
env:
OPENWA_SMOKE_IMAGE: openwa-ci:non-root-smoke
run: ./scripts/smoke-test-non-root.sh
+
+ # Re-point the branch tag at the image this run built, by digest. The head check sits seconds
+ # before the re-point rather than a whole build before it: a run whose commit has been
+ # superseded leaves the tag to the newer run.
+ - name: Move the branch tag to this image when it is still the branch head
+ if: github.event_name == 'push'
+ env:
+ REPO: ${{ github.repository }}
+ DIGEST: ${{ steps.build.outputs.digest }}
+ run: |
+ set -euo pipefail
+ head="$(git ls-remote origin "$GITHUB_REF" | cut -f1)"
+ if [ "$head" != "$GITHUB_SHA" ]; then
+ echo "::notice::$GITHUB_SHA is no longer the head of $GITHUB_REF_NAME ($head); leaving the branch tag alone"
+ exit 0
+ fi
+ docker buildx imagetools create --tag "ghcr.io/${REPO,,}:${GITHUB_REF_NAME}" "ghcr.io/${REPO,,}@${DIGEST}"
diff --git a/docs/09-testing-strategy.md b/docs/09-testing-strategy.md
index c9817bd78..e195d50d0 100644
--- a/docs/09-testing-strategy.md
+++ b/docs/09-testing-strategy.md
@@ -256,7 +256,8 @@ on broad integration coverage.
## 9.6 CI Checks
-Main CI is defined in `.github/workflows/ci.yml`.
+Main CI is defined in `.github/workflows/ci.yml`. A new push to a pull request cancels that pull
+request's superseded run; push runs on `main` are never cancelled or queued behind one another.
| Job | Checks |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
@@ -268,7 +269,7 @@ Main CI is defined in `.github/workflows/ci.yml`.
| `scripts-smoke` | shellcheck on `docker-entrypoint.sh` and every `scripts/*.sh`, plus the backup/restore smoke test |
| `chart` | helm lint, helm template with default and fully-toggled values, kubeconform on both renders, the rendered-behaviour check, actionlint on the workflows |
| `build` | backend build after lint/audit/test/dashboard/scripts-smoke/chart jobs pass |
-| `docker` | multi-arch Docker build on pushes and pull requests, then `scripts/smoke-test-non-root.sh` against the built image so the entrypoint's root→openwa drop is verified, not assumed; publishes to GHCR only on push, so fork pull requests validate both architectures without publishing |
+| `docker` | multi-arch Docker build on pushes and pull requests, then `scripts/smoke-test-non-root.sh` against the built image so the entrypoint's root→openwa drop is verified, not assumed; publishes to GHCR only on push, so fork pull requests validate both architectures without publishing. A push publishes `:<sha>` from the build and moves the branch tag (`:main`, `:develop`) to that image only after the non-root smoke passes and only while the commit is still the branch head |
SDK CI is defined in `.github/workflows/sdk-ci.yml` and is path-filtered to SDK sources plus server
contract surfaces that SDKs mirror (`src/**/dto/**`, `src/**/*.controller.ts`, `src/**/*.service.ts`, and
diff --git a/src/common/ci-privilege-gate.spec.ts b/src/common/ci-privilege-gate.spec.ts
index 9650bc4fd..adb085fe1 100644
--- a/src/common/ci-privilege-gate.spec.ts
+++ b/src/common/ci-privilege-gate.spec.ts
@@ -358,3 +358,59 @@ describe('deployment docs describe what the entrypoint, probes and backup script
expect(read('docs/11-operational-runbooks.md')).toContain(phrase);
});
});
+
+/**
+ * `ghcr.io/<repo>:main` is the channel an operator pulls to track main. Two things once decided which
+ * commit it pointed at: whichever push run finished its build last, even an older commit's, and a push
+ * that happened before the non-root smoke test, so an image that failed the smoke had already moved it.
+ * The branch tag is now a final step that runs only after the smoke passes and only while the commit is
+ * still the branch head; the build itself publishes nothing but the immutable `:<sha>` tag.
+ */
+describe('ci.yml moves the branch image tag only for the tested branch head', () => {
+ type CiStep = Step & { if?: string; id?: string };
+ type CiWorkflow = {
+ concurrency?: { group?: string; 'cancel-in-progress'?: boolean | string };
+ jobs?: Record<string, { steps?: CiStep[] }>;
+ };
+ const ci = (): CiWorkflow => workflowOf('ci.yml');
+ const dockerSteps = (): CiStep[] => ci().jobs?.docker?.steps ?? [];
+
+ it('finds the docker job and its steps', () => {
+ expect(dockerSteps().length).toBeGreaterThan(3);
+ });
+
+ // One group per pull request, so a new push cancels the superseded run; one group per push run
+ // (run_id), so pushes to main are never queued behind, or cancelled by, each other.
+ it('cancels superseded pull request runs without grouping push runs together', () => {
+ const concurrency = ci().concurrency;
+ expect(concurrency?.['cancel-in-progress']).toBe(true);
+ expect(concurrency?.group).toContain('github.event.pull_request.number');
+ expect(concurrency?.group).toContain('github.run_id');
+ // github.ref would put every push to main in one group, where a third push cancels the pending
+ // run of the second and that commit never gets tests or an image.
+ expect(concurrency?.group).not.toMatch(/github\.ref\b/);
+ });
+
+ it('never publishes the branch tag from the build step', () => {
+ const tagRules = dockerSteps()
+ .filter(step => (step.uses ?? '').startsWith('docker/metadata-action'))
+ .map(step => String((step.with as { tags?: string } | undefined)?.tags ?? ''));
+ expect(tagRules.length).toBe(1);
+ expect(executableLines(tagRules.join('\n'))).not.toContain('type=ref,event=branch');
+ });
+
+ it('re-points the branch tag as the last step, after the smoke, on push, only at the branch head', () => {
+ const steps = dockerSteps();
+ const smoke = steps.findIndex(step => executableLines(step.run ?? '').includes('smoke-test-non-root.sh'));
+ const retag = steps.findIndex(step => executableLines(step.run ?? '').includes('imagetools create'));
+ expect(smoke).toBeGreaterThan(-1);
+ expect(retag).toBeGreaterThan(smoke);
+ // Last, so every check added to this job gates the branch tag, not only the non-root smoke.
+ expect(retag).toBe(steps.length - 1);
+ const step = steps[retag];
+ expect(step.if ?? '').toContain("github.event_name == 'push'");
+ const run = executableLines(step.run ?? '');
+ expect(run).toMatch(/git ls-remote/);
+ expect(run).toMatch(/GITHUB_SHA/);
+ });
+});