diff --git a/.github/ci/test_nightly_images.py b/.github/ci/test_nightly_images.py new file mode 100644 index 0000000000..5aee751a7e --- /dev/null +++ b/.github/ci/test_nightly_images.py @@ -0,0 +1,107 @@ +"""Tests for .github/scripts/nightly-images-mirror.sh and the image pushes in nightly.yml. + +The script runs against the stub docker of test_release_images: it logs every `buildx imagetools +create` and fails the first COPY_FAILS of them, as public ECR does when it drops a blob upload. +""" +import os +import pathlib +import re +import subprocess +import tempfile +import unittest + +from test_release_images import STUB_DOCKER + +ROOT = pathlib.Path(__file__).resolve().parents[2] +MIRROR = ROOT / '.github' / 'scripts' / 'nightly-images-mirror.sh' +NIGHTLY = ROOT / '.github' / 'workflows' / 'nightly.yml' + +NATIVE = 'sha256:' + 'a' * 64 +COMPAT = 'sha256:' + 'c' * 64 + + +class NightlyImagesMirrorTest(unittest.TestCase): + + def setUp(self): + self.tmp = pathlib.Path(tempfile.mkdtemp()) + bin_dir = self.tmp / 'bin' + bin_dir.mkdir() + stub = bin_dir / 'docker' + stub.write_text(STUB_DOCKER) + stub.chmod(0o755) + self.calls = self.tmp / 'docker.log' + self.env = { + 'PATH': f'{bin_dir}:{os.environ["PATH"]}', + 'DOCKER_CALLS': str(self.calls), + 'RETRY_DELAYS': '0 0', + 'GITHUB_STEP_SUMMARY': str(self.tmp / 'summary.md'), + 'VERSION': '12012026', + 'NATIVE_DIGEST': NATIVE, + 'COMPAT_DIGEST': COMPAT, + } + + def run_mirror(self, **env): + return subprocess.run(['bash', str(MIRROR)], env={**self.env, **env}, + capture_output=True, text=True) + + def copies(self): + return self.calls.read_text().splitlines() if self.calls.exists() else [] + + def test_each_nightly_tag_points_at_the_digest_docker_hub_got(self): + result = self.run_mirror() + + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(self.copies(), [ + f'--tag public.ecr.aws/floci/floci:nightly floci/floci@{NATIVE}', + f'--tag public.ecr.aws/floci/floci:nightly-12012026 floci/floci@{NATIVE}', + f'--tag public.ecr.aws/floci/floci:nightly-compat floci/floci@{COMPAT}', + f'--tag public.ecr.aws/floci/floci:nightly-12012026-compat floci/floci@{COMPAT}', + ]) + + def test_a_dropped_upload_is_retried(self): + result = self.run_mirror(COPY_FAILS='2') + + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(len(self.copies()), 6) + self.assertIn('retrying', result.stderr) + + def test_a_copy_that_keeps_failing_fails_the_run_after_the_other_copies(self): + result = self.run_mirror(COPY_FAILS='3') + + self.assertNotEqual(result.returncode, 0) + self.assertIn('could not copy', result.stderr) + # The first tag used up its three attempts; the three other tags were still copied. + self.assertEqual(len(self.copies()), 6) + + def test_an_image_docker_hub_did_not_get_is_not_copied(self): + result = self.run_mirror(COMPAT_DIGEST='') + + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual([c.split()[1] for c in self.copies()], + ['public.ecr.aws/floci/floci:nightly', 'public.ecr.aws/floci/floci:nightly-12012026']) + + def test_a_malformed_digest_or_version_is_refused_before_anything_is_copied(self): + for env in ({'NATIVE_DIGEST': 'latest'}, {'VERSION': '2026-12-01'}, + {'NATIVE_DIGEST': '', 'COMPAT_DIGEST': ''}): + with self.subTest(env=env): + result = self.run_mirror(**env) + self.assertNotEqual(result.returncode, 0) + self.assertEqual(self.copies(), []) + + +class NightlyWorkflowTest(unittest.TestCase): + + def test_the_image_pushes_go_to_docker_hub_only(self): + # Public ECR gets its tags from the mirror script, by digest, so a dropped ECR upload + # neither fails the build nor skips the compat image. + steps = re.split(r'\n - ', NIGHTLY.read_text()) + pushes = [s for s in steps if 'uses: ./.github/actions/push-zstd-image' in s] + + self.assertEqual(len(pushes), 2) + for push in pushes: + self.assertNotIn('public.ecr.aws', push) + self.assertIn('.github/scripts/nightly-images-mirror.sh', NIGHTLY.read_text()) + + +if __name__ == '__main__': + unittest.main() diff --git a/.github/scripts/nightly-images-mirror.sh b/.github/scripts/nightly-images-mirror.sh new file mode 100755 index 0000000000..d1296e8191 --- /dev/null +++ b/.github/scripts/nightly-images-mirror.sh @@ -0,0 +1,65 @@ +#!/usr/bin/env bash +# Copies the nightly images from Docker Hub to public ECR by digest. +# +# VERSION=12012026 NATIVE_DIGEST=sha256:... COMPAT_DIGEST=sha256:... nightly-images-mirror.sh +# +# nightly.yml pushes each image to Docker Hub only and passes the digest each push produced. This +# points public.ecr.aws/floci/floci:nightly and :nightly- at the native digest, and the +# -compat pair at the compat digest, with `docker buildx imagetools create`: a registry-to-registry +# copy, so nothing is rebuilt and both registries serve the same digest. An empty digest means that +# image was not pushed, and its tags are left alone. +# +# Public ECR drops a blob upload now and then ("The upload with id ... does not exist", nightly run +# 46814301866), so each copy is retried; RETRY_DELAYS lists the waits in seconds. A tag that still +# fails does not stop the others; the script exits non-zero at the end. +set -euo pipefail + +VERSION=${VERSION:?VERSION is required (mmddyyyy)} +SOURCE=${SOURCE_REPO:-floci/floci} +MIRROR=${MIRROR_REPO:-public.ecr.aws/floci/floci} +DOCKER=${DOCKER:-docker} +read -ra DELAYS <<< "${RETRY_DELAYS:-15 30 60}" +SUMMARY=${GITHUB_STEP_SUMMARY:-/dev/null} +NATIVE_DIGEST=${NATIVE_DIGEST:-} +COMPAT_DIGEST=${COMPAT_DIGEST:-} + +fail() { + echo "::error::$1" >&2 + exit 1 +} + +[[ $VERSION =~ ^[0-9]{8}$ ]] || fail "'$VERSION' is not a nightly version (mmddyyyy)" +[ -n "$NATIVE_DIGEST$COMPAT_DIGEST" ] || fail "no digest given: nothing was pushed to copy" +for digest in "$NATIVE_DIGEST" "$COMPAT_DIGEST"; do + [ -z "$digest" ] || [[ $digest =~ ^sha256:[0-9a-f]{64}$ ]] || fail "'$digest' is not a sha256 digest" +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 +} + +echo "### Public ECR nightly tags" >> "$SUMMARY" +failed=0 +if [ -n "$NATIVE_DIGEST" ]; then + copy nightly "$NATIVE_DIGEST" || failed=1 + copy "nightly-$VERSION" "$NATIVE_DIGEST" || failed=1 +fi +if [ -n "$COMPAT_DIGEST" ]; then + copy nightly-compat "$COMPAT_DIGEST" || failed=1 + copy "nightly-$VERSION-compat" "$COMPAT_DIGEST" || failed=1 +fi +exit "$failed" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6c6313f147..b6889abef6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,6 +10,8 @@ on: - '.github/ci/**' - '.github/scripts/release-binaries-*' - '.github/scripts/release-images-*' + - '.github/scripts/nightly-images-*' + - '.github/workflows/nightly.yml' - '.github/release-tooling/contributors*' - '.github/workflows/ci.yml' pull_request: @@ -22,6 +24,8 @@ on: - '.github/ci/**' - '.github/scripts/release-binaries-*' - '.github/scripts/release-images-*' + - '.github/scripts/nightly-images-*' + - '.github/workflows/nightly.yml' - '.github/release-tooling/contributors*' - '.github/workflows/ci.yml' @@ -116,6 +120,7 @@ jobs: python3 -m unittest discover -s .github/ci -p test_partition.py python3 -m unittest discover -s .github/ci -p test_release_binaries.py python3 -m unittest discover -s .github/ci -p test_release_images.py + python3 -m unittest discover -s .github/ci -p test_nightly_images.py node --test .github/release-tooling/contributors.test.mjs .github/ci/partition.sh all-classes.txt "$SHARD_TOTAL" "${{ matrix.shard }}" > shard.txt # An empty includesFile makes surefire fall back to its default includes diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 00b9c226fc..6b47ec89b4 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -6,7 +6,7 @@ name: Nightly # org have reached several hours. The schedule is a target, not a guarantee. # Can also be triggered manually. # -# Published tags: +# Published tags, on Docker Hub and, copied by digest, on public.ecr.aws/floci/floci: # Native: floci/floci:nightly floci/floci:nightly-mmddyyyy # Compat: floci/floci:nightly-compat floci/floci:nightly-mmddyyyy-compat # @@ -230,7 +230,9 @@ jobs: with: registry-type: public + # Docker Hub only: the step after the pushes copies each image to public ECR by digest. - name: Build and push native nightly image + id: native uses: ./.github/actions/push-zstd-image with: source-date-epoch: ${{ steps.source_date.outputs.epoch }} @@ -239,8 +241,6 @@ jobs: tags: | floci/floci:nightly floci/floci:nightly-${{ needs.prepare.outputs.version }} - public.ecr.aws/floci/floci:nightly - public.ecr.aws/floci/floci:nightly-${{ needs.prepare.outputs.version }} build-args: | VERSION=nightly-${{ needs.prepare.outputs.version }} labels: | @@ -252,6 +252,7 @@ jobs: vcs-ref=${{ github.sha }} - name: Build and push compat nightly image + id: compat uses: ./.github/actions/push-zstd-image with: source-date-epoch: ${{ steps.source_date.outputs.epoch }} @@ -260,8 +261,6 @@ jobs: tags: | floci/floci:nightly-compat floci/floci:nightly-${{ needs.prepare.outputs.version }}-compat - public.ecr.aws/floci/floci:nightly-compat - public.ecr.aws/floci/floci:nightly-${{ needs.prepare.outputs.version }}-compat build-args: | VERSION=nightly-${{ needs.prepare.outputs.version }} labels: | @@ -271,3 +270,14 @@ jobs: version=nightly-${{ needs.prepare.outputs.version }} build-date=${{ steps.created.outputs.created }} vcs-ref=${{ github.sha }} + + # Runs when either push produced a digest, so a failed compat push still leaves the native + # nightly on public ECR. It copies from Docker Hub by digest, so both registries serve the + # same image. + - name: Copy the images to public ECR by digest + if: ${{ !cancelled() && (steps.native.outputs.digest != '' || steps.compat.outputs.digest != '') }} + env: + VERSION: ${{ needs.prepare.outputs.version }} + NATIVE_DIGEST: ${{ steps.native.outputs.digest }} + COMPAT_DIGEST: ${{ steps.compat.outputs.digest }} + run: .github/scripts/nightly-images-mirror.sh