Files
floci/.github/scripts/release-images-mirror.sh
T

114 lines
4.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Checks a release's images on Docker Hub against the digests its release run built, and copies
# them to public ECR by digest.
#
# VERSION=2.6.0 NATIVE_DIGEST=sha256:... BASELINE_DIGEST=sha256:... COMPAT_DIGEST=sha256:... \
# release-images-mirror.sh verify|copy
#
# The digests are the trust anchor: a tag run passes the ones push-native built, and a repair
# dispatch passes the ones the release's own run pushed (its log names each, e.g. `pushing manifest
# for docker.io/floci/floci:2.6.0@sha256:...`). Docker Hub tags are mutable, so they are never
# trusted on their own.
#
# verify Refuses unless floci/floci:<version><suffix> points at exactly the given digest, for the
# native image, -baseline and -compat. On success writes verified=true to $GITHUB_OUTPUT;
# signing waits for it. Needs no AWS access, so an AWS failure cannot hold back signing.
# copy Points public.ecr.aws/floci/floci:<version><suffix> at the given digest with `docker
# buildx imagetools create`, a registry-to-registry copy of the index, every image and
# attestation manifest it references and their blobs: nothing is rebuilt and both
# registries serve the same digest. Then moves latest<suffix> there too, but only when
# Docker Hub's latest<suffix> points at that digest, read again right before each move, so
# repairing an older release never moves `latest`. Public ECR has dropped a blob upload
# mid-push (2.6.0-compat, "The upload with id ... does not exist"), so each copy is retried;
# RETRY_DELAYS lists the waits in seconds.
#
# Exits non-zero on a malformed digest, a tag that does not match, or a copy that still fails after
# its retries.
set -euo pipefail
MODE=${1:?usage: release-images-mirror.sh verify|copy}
VERSION=${VERSION:?VERSION is required (x.y.z)}
SOURCE=${SOURCE_REPO:-floci/floci}
MIRROR=${MIRROR_REPO:-public.ecr.aws/floci/floci}
DOCKER=${DOCKER:-docker}
read -ra DELAYS <<< "${RETRY_DELAYS:-15 30 60}"
OUT=${GITHUB_OUTPUT:-/dev/stdout}
SUMMARY=${GITHUB_STEP_SUMMARY:-/dev/null}
VARIANTS="native baseline compat"
fail() {
echo "::error::$1" >&2
exit 1
}
[[ $VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || fail "'$VERSION' is not a release version (x.y.z)"
suffix_of() {
if [ "$1" = native ]; then echo ""; else echo "-$1"; fi
}
expected_of() {
local name
name="$(echo "$1" | tr '[:lower:]' '[:upper:]')_DIGEST"
echo "${!name:-}"
}
digest_of() {
"$DOCKER" buildx imagetools inspect "$1" --format '{{.Manifest.Digest}}' 2>/dev/null
}
for variant in $VARIANTS; do
expected=$(expected_of "$variant")
[[ $expected =~ ^sha256:[0-9a-f]{64}$ ]] \
|| fail "the $variant digest '$expected' is not a sha256 digest; pass the one the release run pushed"
done
copy() {
local tag=$1 digest=$2 attempt=0
while true; do
if "$DOCKER" buildx imagetools create --tag "$MIRROR:$tag" "$SOURCE@$digest"; then
echo "- \`$tag\`: \`$digest\`" >> "$SUMMARY"
return 0
fi
if [ "$attempt" -ge "${#DELAYS[@]}" ]; then
echo "::error::could not copy $SOURCE@$digest to $MIRROR:$tag after $((attempt + 1)) attempts" >&2
echo "- \`$tag\`: copy failed" >> "$SUMMARY"
return 1
fi
echo "::warning::copy to $MIRROR:$tag failed; retrying in ${DELAYS[$attempt]}s" >&2
sleep "${DELAYS[$attempt]}"
attempt=$((attempt + 1))
done
}
case "$MODE" in
verify)
for variant in $VARIANTS; do
ref="$SOURCE:$VERSION$(suffix_of "$variant")"
expected=$(expected_of "$variant")
actual=$(digest_of "$ref") || fail "$ref is not on Docker Hub"
[ "$actual" = "$expected" ] \
|| fail "$ref points at $actual, not $expected, the digest the release run built; refusing to copy or sign it"
done
echo "verified=true" >> "$OUT"
;;
copy)
failed=0
printf '### Images for %s on %s\n\n' "$VERSION" "$MIRROR" >> "$SUMMARY"
for variant in $VARIANTS; do
copy "$VERSION$(suffix_of "$variant")" "$(expected_of "$variant")" || failed=1
done
for variant in $VARIANTS; do
suffix=$(suffix_of "$variant")
expected=$(expected_of "$variant")
if [ "$(digest_of "$SOURCE:latest$suffix" || true)" = "$expected" ]; then
copy "latest$suffix" "$expected" || failed=1
fi
done
exit "$failed"
;;
*)
fail "unknown mode '$MODE' (verify or copy)"
;;
esac