🪞 ci: Mirror Published Images to a Legacy GHCR Namespace (#16214)

After a repository moves to another owner, images publish under the new owner while existing deployments still pull the old namespace. The merge job now copies each published tag to LEGACY_GHCR_OWNER when that variable and the LEGACY_GHCR_TOKEN secret are set, using GHCR cross-repo blob mounts. It stays off while the legacy owner matches the publishing owner, and never fails the build.
This commit is contained in:
Danny Avila 2026-09-22 14:08:44 -04:00 • committed by GitHub
parent a54cc32b31
commit 07e155338d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 50 additions and 0 deletions

View file

@ -46,3 +46,4 @@ jobs:
secrets:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
LEGACY_GHCR_TOKEN: ${{ secrets.LEGACY_GHCR_TOKEN }}

View file

@ -42,3 +42,4 @@ jobs:
secrets:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
LEGACY_GHCR_TOKEN: ${{ secrets.LEGACY_GHCR_TOKEN }}

View file

@ -21,3 +21,4 @@ jobs:
secrets:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
LEGACY_GHCR_TOKEN: ${{ secrets.LEGACY_GHCR_TOKEN }}

View file

@ -45,6 +45,11 @@ on:
required: true
DOCKERHUB_TOKEN:
required: true
LEGACY_GHCR_TOKEN:
description: >-
Token that can write to the LEGACY_GHCR_OWNER namespace. Without it the
mirror step is skipped.
required: false
permissions:
contents: read
@ -242,3 +247,43 @@ jobs:
set -euo pipefail
first_tag=$(grep -m1 -v '^[[:space:]]*$' <<< "$TAG_SUFFIXES")
docker buildx imagetools inspect "${GHCR_IMAGE}:${first_tag}"
# After the move to another owner, deployments still pull the old GHCR
# namespace. Copying each published tag there keeps them updating until the
# deprecation window closes. Copies are cross-repo blob mounts within GHCR,
# so no image data is rebuilt or re-uploaded.
#
# Set the LEGACY_GHCR_OWNER variable and the LEGACY_GHCR_TOKEN secret (a token
# with write:packages for that owner) to turn this on. It stays off when either
# is unset, and while the owner still matches the one publishing the images.
- name: Mirror tags to the legacy namespace
if: ${{ vars.LEGACY_GHCR_OWNER != '' }}
continue-on-error: true
env:
LEGACY_OWNER: ${{ vars.LEGACY_GHCR_OWNER }}
LEGACY_TOKEN: ${{ secrets.LEGACY_GHCR_TOKEN }}
IMAGE_NAME: ${{ matrix.image.image_name }}
TAG_SUFFIXES: ${{ inputs.tag_suffixes }}
run: |
set -euo pipefail
legacy_owner="${LEGACY_OWNER,,}"
if [ "$legacy_owner" = "${GITHUB_REPOSITORY_OWNER,,}" ]; then
echo "Legacy owner matches the current owner; nothing to mirror."
exit 0
fi
if [ -z "$LEGACY_TOKEN" ]; then
echo "::warning::LEGACY_GHCR_OWNER is set but LEGACY_GHCR_TOKEN is not; skipping the mirror."
exit 0
fi
# The pull side needs this login too: it replaces the GITHUB_TOKEN
# credential for ghcr.io, and both namespaces are read with it.
echo "$LEGACY_TOKEN" | docker login ghcr.io -u "$legacy_owner" --password-stdin
legacy_image="ghcr.io/${legacy_owner}/${IMAGE_NAME}"
while IFS= read -r suffix; do
[ -n "$suffix" ] || continue
echo "Mirroring ${GHCR_IMAGE}:${suffix} -> ${legacy_image}:${suffix}"
docker buildx imagetools create -t "${legacy_image}:${suffix}" "${GHCR_IMAGE}:${suffix}"
done <<< "$TAG_SUFFIXES"

View file

@ -57,3 +57,4 @@ jobs:
secrets:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
LEGACY_GHCR_TOKEN: ${{ secrets.LEGACY_GHCR_TOKEN }}

View file

@ -73,3 +73,4 @@ jobs:
secrets:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
LEGACY_GHCR_TOKEN: ${{ secrets.LEGACY_GHCR_TOKEN }}