mirror of
https://github.com/block/buzz.git
synced 2026-11-24 02:02:07 +00:00
ci(docker): publish the push gateway only when its inputs change (#9658)
## Why
Every main push that rebuilds the relay image also published a new push
gateway image. A staging environment that promotes each published
`main-<commit UTC>-<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-<commit UTC>-<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 <tornquist@squareup.com>
Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
61
scripts/push-gateway-image-inputs-changed.sh
Executable file
61
scripts/push-gateway-image-inputs-changed.sh
Executable file
@@ -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
|
||||
@@ -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'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user