2026-11-11 17:22:24 -08:00
#!/usr/bin/env bash
# Assemble the multi-arch index tag(s) from per-platform images that were
# already pushed BY DIGEST (no tag). Used by the trusted x86 publish job
# (copied verbatim from haproxy-manager-base PR #17; see its build-push-coraza.yaml; template for the other WHP
# image repos, docs/arm64/IMAGE-PIPELINE-PLAN.md section 4.1/4.4).
#
# assemble-multiarch-index.sh \
# --amd64 sha256:<digest> \
# --arm64 sha256:<digest> --arm64-outcome success|failure|cancelled|skipped \
# --repo <registry/path> [--repo <other registry/path> ...] \
2026-11-19 21:05:57 -08:00
# --tag <tag> [--tag <tag> ...] [--builder <buildx builder>] \
# [--verify-bin PATH ...] [--guard-existing-arm64]
2026-11-11 17:22:24 -08:00
#
# Rules (all enforced here so the workflow stays thin and these are testable):
# * amd64 is mandatory. A missing/malformed amd64 digest, or an amd64 digest
# that does not inspect as linux/amd64, aborts with exit 1 and publishes
# NOTHING (the same "build failed, no tag" outcome as before multi-arch).
# * arm64 is attached ONLY when its step outcome is exactly "success" AND the
# digest is well-formed AND it inspects as linux/arm64. Otherwise the tag is
# published amd64-only (warning, exit 0). The arm64 digest must come from
# THIS run's step output - the workflow never reads it from a registry tag,
# so a previous build's arm64 can never be re-attached to a newer amd64.
2026-11-19 21:05:57 -08:00
# * --verify-bin (repeatable): before anything is published, the amd64 entry must hold x86-64
# and (if it would be attached) the arm64 entry AArch64 binaries at those paths
# (scripts/verify-image-arch.py, nothing is executed). Wrong/unverifiable amd64 = exit 1,
# nothing published. Wrong/unverifiable arm64 = treated as a failed arm64 leg.
# * --guard-existing-arm64: if the result would be amd64-only and a CURRENT tag already has a
# linux/arm64 entry, exit 1 and publish nothing (never silently drop arm64), unless
# env ALLOW_AMD64_ONLY=true. An unreadable tag fails closed; a missing tag is fine.
2026-11-11 17:22:24 -08:00
# * every --tag is created in every --repo from <repo>@<digest> sources in
# that same repo (both digests were pushed to every --repo by the build).
#
2026-11-19 21:05:57 -08:00
# Env: ALLOW_AMD64_ONLY=true lets the guard pass; VERIFY_ARCH overrides the verifier command
# (tests use a stub); IMAGETOOLS overrides the command (default: "docker buildx imagetools");
2026-11-11 17:22:24 -08:00
# the test suite points it at a stub.
set -euo pipefail
IMAGETOOLS = ${ IMAGETOOLS :- docker buildx imagetools }
amd64 = "" ; arm64 = "" ; outcome = "skipped" ; builder = ""
2026-11-19 21:05:57 -08:00
repos =() ; tags =() ; vbins =() ; guard = 0
HERE = $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " && pwd )
2026-11-11 17:22:24 -08:00
die() { echo "assemble-multiarch-index: $* " >& 2; exit 1; }
warn() { echo "::warning::assemble-multiarch-index: $* " ; }
while [ $# -gt 0 ] ; do
case " $1 " in
--amd64) amd64 = ${ 2 - } ; shift 2 ;;
--arm64) arm64 = ${ 2 - } ; shift 2 ;;
--arm64-outcome) outcome = ${ 2 - } ; shift 2 ;;
--repo) repos +=( " ${ 2 - } " ) ; shift 2 ;;
--tag) tags +=( " ${ 2 - } " ) ; shift 2 ;;
--builder) builder = ${ 2 - } ; shift 2 ;;
2026-11-19 21:05:57 -08:00
--verify-bin) vbins +=( " ${ 2 - } " ) ; shift 2 ;;
--guard-existing-arm64) guard = 1; shift ;;
2026-11-11 17:22:24 -08:00
*) die "unknown argument: $1 " ;;
esac
done
re = '^sha256:[0-9a-f]{64}$'
[ ${# repos [@] } -gt 0 ] || die "need at least one --repo"
[ ${# tags [@] } -gt 0 ] || die "need at least one --tag"
[[ $amd64 = ~ $re ]] || die "amd64 digest missing or malformed (' $amd64 '); refusing to publish"
bflag =() ; [ -n " $builder " ] && bflag =( --builder " $builder " )
# arch_of <ref>: prints the image config's architecture ("" on any failure).
arch_of() {
# shellcheck disable=SC2086
$IMAGETOOLS inspect " ${ bflag [@] } " " $1 " --format '{{json .Image}}' 2>/dev/null \
| python3 -I -c 'import json,sys; print(json.load(sys.stdin).get("architecture",""))' 2>/dev/null || true
}
for r in " ${ repos [@] } " ; do
[ " $( arch_of " $r @ $amd64 " ) " = amd64 ] || die " $r @ $amd64 does not inspect as linux/amd64; refusing to publish"
done
use_arm64 = 0
if [ " $outcome " != success ] ; then
warn "arm64 step outcome is ' $outcome ': publishing amd64-only (no arm64 digest is carried over)"
elif ! [[ $arm64 = ~ $re ]] ; then
warn "arm64 step reported success but its digest is missing/malformed: publishing amd64-only"
elif [ " $arm64 " = " $amd64 " ] ; then
warn "arm64 digest equals the amd64 digest: publishing amd64-only"
else
use_arm64 = 1
for r in " ${ repos [@] } " ; do
if [ " $( arch_of " $r @ $arm64 " ) " != arm64 ] ; then
warn " $r @ $arm64 does not inspect as linux/arm64: publishing amd64-only"
use_arm64 = 0; break
fi
done
fi
2026-11-19 21:05:57 -08:00
# ELF machine-type check (labels alone do not prove the binaries are of that architecture).
if [ ${# vbins [@] } -gt 0 ] ; then
# shellcheck disable=SC2206
verify =( ${ VERIFY_ARCH :- python3 -I " $HERE /verify-image-arch.py" } )
vargs =() ; for b in " ${ vbins [@] } " ; do vargs +=( --bin " $b " ) ; done
for r in " ${ repos [@] } " ; do
" ${ verify [@] } " --image " $r @ $amd64 " --platform linux/amd64 " ${ vargs [@] } " \
|| die " $r @ $amd64 failed the x86-64 ELF check; refusing to publish"
if [ " $use_arm64 " = 1 ] ; then
if ! " ${ verify [@] } " --image " $r @ $arm64 " --platform linux/arm64 " ${ vargs [@] } " ; then
warn " $r @ $arm64 failed the AArch64 ELF check: publishing amd64-only"
use_arm64 = 0; break
fi
fi
done
fi
# Never silently replace a multi-arch tag by an amd64-only one.
if [ " $guard " = 1 ] && [ " $use_arm64 " = 0 ] && [ " ${ ALLOW_AMD64_ONLY :- false } " != true ] ; then
for r in " ${ repos [@] } " ; do for t in " ${ tags [@] } " ; do
rc = 0; info = $( IMAGETOOLS = $IMAGETOOLS " $HERE /index-platforms.sh" " $r : $t " ) || rc = $?
case " $rc " in
0) ;;
3) info = "not found" ;;
*) echo "::error::assemble-multiarch-index: cannot tell whether $r : $t is multi-arch; refusing to overwrite it (fail closed)" >& 2; exit 1 ;;
esac
case " $info " in
*linux/arm64*)
echo "::error::assemble-multiarch-index: $r : $t has an arm64 entry but this build has none (arm64 outcome ' $outcome '); publishing amd64-only would drop it. Nothing published. amd64 digest of this build: $amd64 . Fix: re-dispatch only=<leg>, or re-run with allow_amd64_only=true and then restore arm64 with the arm64-attach workflow." >& 2
exit 1 ;;
esac
done ; done
fi
2026-11-11 17:22:24 -08:00
for r in " ${ repos [@] } " ; do
targs =() ; for t in " ${ tags [@] } " ; do targs +=( -t " $r : $t " ) ; done
srcs =( " $r @ $amd64 " ) ; [ " $use_arm64 " = 1 ] && srcs +=( " $r @ $arm64 " )
echo "assemble-multiarch-index: $r tags= ${ tags [*] } sources= ${# srcs [@] } ( $( [ " $use_arm64 " = 1 ] && echo 'amd64+arm64' || echo 'amd64-only' ) )"
# shellcheck disable=SC2086
$IMAGETOOLS create " ${ bflag [@] } " " ${ targs [@] } " " ${ srcs [@] } "
done
echo "platforms= $( [ " $use_arm64 " = 1 ] && echo linux/amd64,linux/arm64 || echo linux/amd64) "