From f04fdef31f83ac9823f715d30ca6cc2ac7fac45c Mon Sep 17 00:00:00 2001 From: Luke Tornquist Date: Wed, 18 Nov 2026 12:17:56 -0500 Subject: [PATCH] ci(docker): publish the push gateway only when its inputs change (#9658) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Why Every main push that rebuilds the relay image also published a new push gateway image. A staging environment that promotes each published `main--<7>` gateway image then rolls out identical gateways. In one 29-hour window, main got 43 image-eligible commits, and only 2 of them changed the gateway. The gateway crate depends on no other workspace crate, so the other 41 images could not differ in behavior. ## What - New `push-gateway-inputs` job in `docker.yml`. It runs `scripts/push-gateway-image-inputs-changed.sh`. On a main push, the script reads the `org.opencontainers.image.revision` label of the current `ghcr.io/block/buzz-push-gateway:main` image. It then compares the gateway's build inputs at that revision and at HEAD: `crates/buzz-push-gateway`, `Cargo.toml`, `Cargo.lock`, `.cargo`, `rust-toolchain.toml`, `Dockerfile.push-gateway`, `.dockerignore`, `docker.yml` and the script itself. Every other event prints `build=true`. The step appends the script's output to `$GITHUB_OUTPUT`, so a failing check fails the run instead of skipping the build. - `push-gateway-build` and `push-gateway-personal-dev` run only when those inputs changed. When the build is skipped, `push-gateway-merge` is skipped too. - `push-gateway-merge` now moves `:main` in its own step, after provenance is attested. The first publication sets every other tag. So `:main`'s source label always names a completed publication, and a failed attestation is retried on the next run. - It compares with the published image, not `github.event.before`. Main runs share one concurrency group, so a newer push replaces a pending run, and a failed run publishes nothing. In both cases the next run still sees the gateway change. - When in doubt, it builds: an unreadable image, an unknown source, or a source that isn't an ancestor of HEAD. - `scripts/test-relay-image-eligibility-workflow.sh` pins the job gating, the decision step, and `:main` moving after attestation. It runs the script against a fixture repository with `docker` stubbed: multi-platform and single-platform `:main` at HEAD, a non-main event, a lookup failure, relay-only, toolchain-only, a gateway change behind a later relay-only commit, a missing source, and a non-ancestor source. I checked that each assertion fails when its production line is broken. ## Risk - A base image update (`rust`, `debian:bookworm-slim`) alone no longer publishes a new gateway image. The next gateway input change picks it up. A manual dispatch at a `relay-v*` tag still builds. - `:main` now moves a few seconds after the other tags on main pushes. Deployments that follow `main--<7>` are unaffected. - The relay image and its triggers are unchanged. ## Simpler There are fewer published gateway images, and every one of them changes the gateway. No new configuration. ## Testing - `scripts/test-relay-image-eligibility-workflow.sh` passes locally. Mutations of the script and the job gating fail it. - `actionlint .github/workflows/docker.yml` passes. - Checked locally that the `docker buildx imagetools inspect … --format '{{json .Image}}'` + `jq` read returns the current `:main` gateway source revision. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Luke Tornquist Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz> Co-authored-by: Claude Opus 5.5 --- .github/workflows/docker.yml | 49 +++++++- scripts/push-gateway-image-inputs-changed.sh | 61 ++++++++++ .../test-relay-image-eligibility-workflow.sh | 110 ++++++++++++++++++ 3 files changed, 217 insertions(+), 3 deletions(-) create mode 100755 scripts/push-gateway-image-inputs-changed.sh diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 65408e22fc..93e98bb2e0 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -37,7 +37,9 @@ name: Docker image # # ghcr.io/block/buzz-push-gateway carries the same :main, :sha-<7> and # :main--<7> tags on main pushes, and publishes only after the -# relay's `qualify` job accepts the same source SHA. +# relay's `qualify` job accepts the same source SHA. A main push publishes it +# only when the gateway's build inputs changed since the source of the current +# :main gateway image (scripts/push-gateway-image-inputs-changed.sh). # # :main--<7> names the same main build as :sha-<7>, prefixed # with its UTC commit time so the tag sorts by build order. Kargo's Lexical @@ -80,6 +82,7 @@ on: - "scripts/create-deployment-eligibility-predicate.jq" - "scripts/select-qualified-ci-run.jq" - "scripts/test-relay-image-eligibility-workflow.sh" + - "scripts/push-gateway-image-inputs-changed.sh" - "Cargo.toml" - "Cargo.lock" - "rust-toolchain.toml" @@ -522,8 +525,33 @@ jobs: fi } >> "$GITHUB_STEP_SUMMARY" + # Staging takes every published main gateway image, so a main push publishes + # one only when the gateway's build inputs changed since the source of the + # current :main image. Other events always build. + push-gateway-inputs: + name: Check push gateway build inputs + runs-on: ubuntu-24.04 + timeout-minutes: 10 + permissions: + contents: read + outputs: + build: ${{ steps.check.outputs.build }} + steps: + - name: Checkout + uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 + with: + fetch-depth: 0 + persist-credentials: false + - name: Compare with the published gateway image + id: check + env: + MAIN_PUSH: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} + run: scripts/push-gateway-image-inputs-changed.sh "$GITHUB_SHA" >> "$GITHUB_OUTPUT" + push-gateway-build: name: Build public push gateway (${{ matrix.platform }}) + needs: push-gateway-inputs + if: needs.push-gateway-inputs.outputs.build == 'true' runs-on: ${{ matrix.runner }} timeout-minutes: 60 permissions: @@ -601,6 +629,8 @@ jobs: push-gateway-personal-dev: name: Build personal development push gateway + needs: push-gateway-inputs + if: needs.push-gateway-inputs.outputs.build == 'true' runs-on: ubuntu-24.04 timeout-minutes: 60 permissions: @@ -676,12 +706,18 @@ jobs: working-directory: /tmp/gateway-digests env: META_TAGS: ${{ steps.meta.outputs.tags }} + # :main is held back until provenance is attested; see "Publish :main". run: | set -euo pipefail - tags=(); while IFS= read -r tag; do [ -n "$tag" ] && tags+=("-t" "$tag"); done <<< "$META_TAGS" + tags=(); first_tag=""; main=false + while IFS= read -r tag; do + [ -n "$tag" ] || continue + if [ "$tag" = "ghcr.io/block/buzz-push-gateway:main" ]; then main=true; continue; fi + tags+=("-t" "$tag"); first_tag=${first_tag:-$tag} + done <<< "$META_TAGS" + echo "main=${main}" >> "$GITHUB_OUTPUT" digests=(); for digest in *; do digests+=("ghcr.io/block/buzz-push-gateway@sha256:${digest}"); done docker buildx imagetools create "${tags[@]}" "${digests[@]}" - first_tag=$(echo "$META_TAGS" | head -n1) digest=$(docker buildx imagetools inspect "$first_tag" --format '{{json .Manifest}}' | jq -r '.digest') echo "digest=${digest}" >> "$GITHUB_OUTPUT" - name: Attest gateway image provenance @@ -690,6 +726,13 @@ jobs: subject-name: ghcr.io/block/buzz-push-gateway subject-digest: ${{ steps.manifest.outputs.digest }} push-to-registry: true + # Main pushes compare with the source of :main to decide whether to build + # (push-gateway-inputs), so :main moves only once the image is attested. + - name: Publish :main + if: steps.manifest.outputs.main == 'true' + env: + GATEWAY_DIGEST: ${{ steps.manifest.outputs.digest }} + run: docker buildx imagetools create -t ghcr.io/block/buzz-push-gateway:main "ghcr.io/block/buzz-push-gateway@${GATEWAY_DIGEST}" - name: Gateway publication summary env: GATEWAY_DIGEST: ${{ steps.manifest.outputs.digest }} diff --git a/scripts/push-gateway-image-inputs-changed.sh b/scripts/push-gateway-image-inputs-changed.sh new file mode 100755 index 0000000000..3499993781 --- /dev/null +++ b/scripts/push-gateway-image-inputs-changed.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env bash +# Print `build=true` when the push gateway image must be built for HEAD_REV, +# and `build=false` when its build inputs match the source of the current :main +# gateway image. docker.yml appends the line to $GITHUB_OUTPUT, so a failure +# here fails the step instead of leaving the decision empty. +# +# Usage: MAIN_PUSH=true|false scripts/push-gateway-image-inputs-changed.sh HEAD_REV +# +# Only a main push (MAIN_PUSH=true) may skip; every other event builds. The +# docker.yml merge job moves :main only after the image's provenance is +# attested, so its source label names a completed publication. Comparing with +# it, not the previous push, keeps a change from being lost when a main run is +# replaced while pending or fails. The gateway crate depends on no other +# workspace crate, so only the paths below can change its image. Any doubt +# builds: an unreadable image, or a source that is unknown or not an ancestor +# of HEAD_REV. +set -euo pipefail + +if [[ $# -ne 1 ]]; then + echo "usage: MAIN_PUSH=true|false $0 HEAD_REV" >&2 + exit 2 +fi +head_rev=$1 + +if [[ "${MAIN_PUSH:-}" != "true" ]]; then + echo build=true + exit 0 +fi + +inputs=( + crates/buzz-push-gateway + Cargo.toml + Cargo.lock + .cargo + rust-toolchain.toml + Dockerfile.push-gateway + .dockerignore + .github/workflows/docker.yml + scripts/push-gateway-image-inputs-changed.sh +) + +# A multi-platform index reports one image per platform; a single image +# reports its config directly. +published_rev=$(docker buildx imagetools inspect ghcr.io/block/buzz-push-gateway:main \ + --format '{{json .Image}}' 2>/dev/null | + jq -r '(if has("config") then . else .["linux/amd64"] end) + | .config.Labels["org.opencontainers.image.revision"] // empty' 2>/dev/null || true) +echo "Published :main gateway source: ${published_rev:-unknown}" >&2 + +if [[ ! "$published_rev" =~ ^[0-9a-f]{40}$ ]] || + ! git cat-file -e "${published_rev}^{commit}" 2>/dev/null || + ! git merge-base --is-ancestor "$published_rev" "$head_rev" 2>/dev/null; then + echo build=true + exit 0 +fi + +if git diff --quiet "$published_rev" "$head_rev" -- "${inputs[@]}"; then + echo build=false +else + echo build=true +fi diff --git a/scripts/test-relay-image-eligibility-workflow.sh b/scripts/test-relay-image-eligibility-workflow.sh index e4457162c2..58a3dec60b 100755 --- a/scripts/test-relay-image-eligibility-workflow.sh +++ b/scripts/test-relay-image-eligibility-workflow.sh @@ -93,6 +93,116 @@ grep -Fxq " type=raw,value=main-{{commit_date 'YYYYMMDDHHmmss'}}-{{sh exit 1 } +# A main push publishes a gateway image only when its build inputs changed +# since the current :main image's source, so staging does not roll out +# identical gateways. Both gateway build jobs wait for that check. +for job in push-gateway-build push-gateway-personal-dev; do + job_block=$(awk -v job=" $job:" '$0==job{f=1;print;next} f&&/^ [a-z][a-z_-]*:$/{exit} f' "$workflow") + grep -Fxq " needs: push-gateway-inputs" <<<"$job_block" && + grep -Fxq " if: needs.push-gateway-inputs.outputs.build == 'true'" <<<"$job_block" || { + echo "$job must run only when the push gateway build inputs changed" >&2 + exit 1 + } +done +inputs_job=$(awk '/^ push-gateway-inputs:/{f=1} f&&/^ [a-z][a-z_-]*:$/&&!/^ push-gateway-inputs:/{exit} f' "$workflow") +grep -Fxq " MAIN_PUSH: \${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}" <<<"$inputs_job" && + grep -Fxq ' run: scripts/push-gateway-image-inputs-changed.sh "$GITHUB_SHA" >> "$GITHUB_OUTPUT"' <<<"$inputs_job" || { + echo "push-gateway-inputs must decide with scripts/push-gateway-image-inputs-changed.sh on main pushes" >&2 + exit 1 +} +# The step's exit status must be the script's, so a failing check fails the +# run instead of writing an empty decision that skips both gateway builds. +decision_run=$(grep -F 'run: scripts/push-gateway-image-inputs-changed.sh' <<<"$inputs_job" | sed 's/^ *run: //') +decision_dir=$(mktemp -d) +mkdir "$decision_dir/scripts" +printf '#!/usr/bin/env bash\nexit 1\n' >"$decision_dir/scripts/push-gateway-image-inputs-changed.sh" +chmod +x "$decision_dir/scripts/push-gateway-image-inputs-changed.sh" +if (cd "$decision_dir" && GITHUB_SHA=HEAD GITHUB_OUTPUT="$decision_dir/out" bash -e -c "$decision_run"); then + rm -rf "$decision_dir" + echo "push-gateway-inputs must fail when the decision script fails" >&2 + exit 1 +fi +rm -rf "$decision_dir" +# The check trusts the source label of :main, so :main must move only after +# the image's provenance is attested: the first publication leaves it out, and +# a later step adds it. +grep -Fxq ' if [ "$tag" = "ghcr.io/block/buzz-push-gateway:main" ]; then main=true; continue; fi' <<<"$gateway_merge_job" || { + echo "push gateway first publication must leave out :main" >&2 + exit 1 +} +attest_line=$(grep -nFx " - name: Attest gateway image provenance" <<<"$gateway_merge_job" | cut -d: -f1) +main_line=$(grep -nFx " - name: Publish :main" <<<"$gateway_merge_job" | cut -d: -f1) +[[ -n "$attest_line" && -n "$main_line" && "$main_line" -gt "$attest_line" ]] && + grep -Fxq ' run: docker buildx imagetools create -t ghcr.io/block/buzz-push-gateway:main "ghcr.io/block/buzz-push-gateway@${GATEWAY_DIGEST}"' <<<"$gateway_merge_job" || { + echo "push gateway :main must move only after provenance is attested" >&2 + exit 1 +} + +# Run the decision script against a fixture repository, with `docker` stubbed +# to report the :main image the registry would return. +inputs_changed="$repo_root/scripts/push-gateway-image-inputs-changed.sh" +fixture=$(mktemp -d) +trap 'rm -rf "$fixture"' EXIT +mkdir "$fixture/bin" "$fixture/repo" +cat >"$fixture/bin/docker" <<'STUB' +#!/usr/bin/env bash +[[ "$*" == "buildx imagetools inspect ghcr.io/block/buzz-push-gateway:main --format {{json .Image}}" ]] || exit 1 +[[ -n "${STUB_IMAGE_JSON:-}" ]] || exit 1 +printf '%s\n' "$STUB_IMAGE_JSON" +STUB +chmod +x "$fixture/bin/docker" +( + cd "$fixture/repo" + export PATH="$fixture/bin:$PATH" + git init -q + git config user.email ci@example.org + git config user.name ci + git config commit.gpgsign false + git config core.hooksPath /dev/null + mkdir -p crates/buzz-push-gateway/src crates/buzz-relay/src + echo 'fn main() {}' >crates/buzz-push-gateway/src/main.rs + echo 'fn main() {}' >crates/buzz-relay/src/main.rs + echo '[workspace]' >Cargo.toml + echo 'channel = "1.0.0"' >rust-toolchain.toml + git add -A && git commit -qm base + published=$(git rev-parse HEAD) + label() { + jq -cn --arg rev "$1" '{config: {Labels: {"org.opencontainers.image.revision": $rev}}}' + } + multi_arch() { + jq -cn --argjson image "$(label "$1")" '{"linux/amd64": $image, "linux/arm64": $image}' + } + expect() { + local want=$1 main_push=$2 image=$3 why=$4 got + got=$(MAIN_PUSH=$main_push STUB_IMAGE_JSON=$image "$inputs_changed" HEAD 2>/dev/null) + [[ "$got" == "build=$want" ]] || { + echo "push gateway inputs check: $why: want $want, got $got" >&2 + exit 1 + } + } + + expect false true "$(multi_arch "$published")" "a multi-platform :main at HEAD" + expect false true "$(label "$published")" "a single-platform :main at HEAD" + expect true false "$(multi_arch "$published")" "an event other than a main push" + expect true true "" "a :main lookup failure" + echo '// relay only' >>crates/buzz-relay/src/main.rs + git commit -qam relay + expect false true "$(multi_arch "$published")" "a relay-only change" + echo 'channel = "1.0.1"' >rust-toolchain.toml + git commit -qam toolchain + expect true true "$(multi_arch "$published")" "a toolchain-only change" + git reset -q --hard HEAD~1 + echo '// gateway' >>crates/buzz-push-gateway/src/main.rs + git commit -qam gateway + echo '// relay again' >>crates/buzz-relay/src/main.rs + git commit -qam relay-again + expect true true "$(multi_arch "$published")" "a gateway change behind a later relay-only commit" + expect true true "$(multi_arch 0000000000000000000000000000000000000000)" "a source missing from history" + git checkout -q --orphan unrelated "$published" + git commit -qm unrelated + expect true true "$(multi_arch "$published")" "a source that is not an ancestor" +) + select_run() { jq -r --arg source_sha aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -f "$selector" | jq -r '.id // empty' }