Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions .github/scripts/registry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# shellcheck shell=bash
# Registry and git calls that ride out GitHub's transient failures (5xx, rate limits,
# timeouts, dropped connections, a manifest ghcr.io rejects right after its layers
# upload). A failed call is tried again after 10 s, doubling to 2 minutes, 8 attempts in
# all (about 9 minutes). A reference the registry says does not exist is an answer, not
# a failure: it is returned at once. Every function is safe to run again.
# Source it from a step: . .github/scripts/registry.sh

retry_attempts=${RETRY_ATTEMPTS:-8}
retry_first_pause=${RETRY_FIRST_PAUSE:-10}

# Sleeps before the attempt after $1.
_retry_pause() {
local pause=$((retry_first_pause * 2 ** ($1 - 1)))
[ "$pause" -le 120 ] || pause=120
echo "Trying again in ${pause}s (attempt $(($1 + 1)) of $retry_attempts)." >&2
sleep "$pause"
}

# retry COMMAND...: runs COMMAND until it succeeds, at most $retry_attempts times.
retry() {
local attempt=1
until "$@"; do
if [ "$attempt" -ge "$retry_attempts" ]; then
echo "Failed $attempt times: $*" >&2
return 1
fi
_retry_pause "$attempt"
attempt=$((attempt + 1))
done
}

# registry_inspect FORMAT REF: prints `docker buildx imagetools inspect --format FORMAT REF`.
# Returns 0; 2 when the registry says REF does not exist; 1 when it could not tell.
registry_inspect() {
local attempt=1 error
error=$(mktemp)
until docker buildx imagetools inspect --format "$1" "$2" 2> "$error"; do
if grep -qiE 'not found|manifest unknown' "$error"; then
rm -f "$error"
return 2
fi
cat "$error" >&2
if [ "$attempt" -ge "$retry_attempts" ]; then
rm -f "$error"
echo "Could not read $2." >&2
return 1
fi
_retry_pause "$attempt"
attempt=$((attempt + 1))
done
rm -f "$error"
}

# registry_digest REF: prints REF's manifest digest; returns as registry_inspect.
registry_digest() {
registry_inspect '{{.Manifest.Digest}}' "$1"
}

# registry_revision REF: prints the commit REF's image names (its
# org.opencontainers.image.revision label; empty if it has none); returns as registry_inspect.
registry_revision() {
local image status=0
image=$(registry_inspect '{{json .Image}}' "$1") || status=$?
[ "$status" -eq 0 ] || return "$status"
# One image, or one per platform.
jq -r 'if has("config") then .config.Labels["org.opencontainers.image.revision"] // ""
else [.[] | .config.Labels["org.opencontainers.image.revision"] // ""] | first end' <<< "$image"
}

# registry_push REF: pushes the local image REF.
registry_push() {
retry docker push -q "$1"
}

# registry_copy SOURCE TARGET: points the tag TARGET (repository:tag) at SOURCE
# (repository@digest), a carbon copy of its manifest, and checks that it does.
registry_copy() {
local want=${1##*@} got status=0
retry docker buildx imagetools create --prefer-index=false --tag "$2" "$1" || return 1
got=$(registry_digest "$2") || status=$?
if [ "$status" -ne 0 ] || [ "$got" != "$want" ]; then
echo "$2 is ${got:-unreadable}, not $want." >&2
return 1
fi
echo "$2: $want"
}

# registry_move_latest IMAGE COMMIT BRANCH: points IMAGE:latest at IMAGE:COMMIT's digest,
# unless that would move it backwards (a re-run of an older commit's run, or two runs
# finishing out of order): COMMIT must be BRANCH's tip, or descend from the commit
# latest's image names. Needs BRANCH's history in the checkout (fetch-depth: 0).
registry_move_latest() {
local image=$1 commit=$2 branch=$3 source tip revision status=0
source=$(registry_digest "$image:$commit") || { echo "Could not read $image:$commit." >&2; return 1; }
tip=$(retry git ls-remote --exit-code origin "refs/heads/$branch") || return 1
tip=${tip%%[[:space:]]*}
if [ "$commit" != "$tip" ]; then
revision=$(registry_revision "$image:latest") || status=$?
case $status in
0)
if [ -z "$revision" ] || ! git merge-base --is-ancestor "$revision" "$commit" 2> /dev/null; then
echo "$commit is neither $branch's tip ($tip) nor a descendant of latest's commit (${revision:-unlabelled}); $image:latest stays."
return 0
fi
;;
2) ;; # No latest yet.
*) return 1 ;;
esac
fi
registry_copy "$image@$source" "$image:latest"
}
19 changes: 14 additions & 5 deletions .github/workflows/image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ on:
# The build runs on the Foundation's public runner pool gf-public-8 on forge (8 vCPUs, a fresh VM
# per job).

# Runs of one branch build one at a time and in order, so latest only moves forward. A run is not
# cancelled: every commit it builds keeps its <commit> image, which a release tags.
# Runs of one branch build one at a time and in order. A run is not cancelled: every commit it
# builds keeps its <commit> image, which a release tags. latest only moves forward, also when an
# older run is re-run: to the branch's tip, or to a descendant of latest's commit.
# Registry and git calls retry GitHub's transient failures (.github/scripts/registry.sh), and a
# failed run is re-run once on its own (rerun.yml); running it again is safe.
concurrency:
group: image-${{ github.ref }}
cancel-in-progress: false
Expand All @@ -36,6 +39,10 @@ jobs:
steps:
- name: Check out the repo
uses: actions/checkout@v4
with:
# The default branch's history, to move latest only forward.
fetch-depth: 0
filter: blob:none

- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
Expand All @@ -62,9 +69,11 @@ jobs:
- name: Push Docker image
env:
LATEST: ${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }}
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
run: |
docker push "$IMAGE:$GITHUB_SHA"
. .github/scripts/registry.sh
registry_push "$IMAGE:$GITHUB_SHA"
if [ "$LATEST" = true ]; then
docker tag "$IMAGE:$GITHUB_SHA" "$IMAGE:latest"
docker push "$IMAGE:latest"
# A carbon copy of the manifest: latest gets the commit image's digest.
registry_move_latest "$IMAGE" "$GITHUB_SHA" "$DEFAULT_BRANCH"
fi
69 changes: 34 additions & 35 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ on:

# Both jobs run on the Foundation's public runner pool gf-public-4 on forge.

# Registry and git calls retry GitHub's transient failures (.github/scripts/registry.sh), and a
# failed run is re-run once on its own (rerun.yml): a release run again tags nothing twice.

# Releases run one at a time, and none is cancelled.
concurrency:
group: release
Expand Down Expand Up @@ -78,7 +81,8 @@ jobs:
env:
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
run: |
git fetch --no-tags origin "+refs/heads/$DEFAULT_BRANCH:refs/remotes/origin/$DEFAULT_BRANCH"
. .github/scripts/registry.sh
retry git fetch --no-tags origin "+refs/heads/$DEFAULT_BRANCH:refs/remotes/origin/$DEFAULT_BRANCH"
if ! git merge-base --is-ancestor "$GITHUB_SHA" "origin/$DEFAULT_BRANCH"; then
echo "$GITHUB_REF_NAME ($GITHUB_SHA) is not on $DEFAULT_BRANCH; only its commits are released." >&2
exit 1
Expand All @@ -93,43 +97,38 @@ jobs:

- name: Tag the image
run: |
set -o pipefail
. .github/scripts/registry.sh
tag=$GITHUB_REF_NAME
error=$(mktemp)
# Prints the digest of $1; fails on any registry error, "not found" included.
digest() {
docker buildx imagetools inspect --format '{{.Manifest.Digest}}' "$1" 2> "$error"
}
not_found() {
grep -qiE 'not found|manifest unknown' "$error"
}
# The default branch's build of the commit may still be running when the tag arrives:
# wait for its image, up to 40 minutes.
attempt=0
until source=$(digest "$IMAGE:$GITHUB_SHA"); do
if ! not_found; then
cat "$error" >&2
exit 1
fi
attempt=$((attempt + 1))
if [ "$attempt" -ge 80 ]; then
echo "$IMAGE:$GITHUB_SHA does not exist: the default branch's build of $GITHUB_SHA pushes it (image.yml)." >&2
exit 1
fi
# wait for its image, up to 40 minutes. Other registry errors are retried
# (.github/scripts/registry.sh), then stop the run.
for attempt in $(seq 80); do
status=0
source=$(registry_digest "$IMAGE:$GITHUB_SHA") || status=$?
[ "$status" -eq 2 ] || break
sleep 30
done
if current=$(digest "$IMAGE:$tag"); then
if [ "$current" != "$source" ]; then
echo "$IMAGE:$tag is already $current, not $source ($GITHUB_SHA); a version tag never moves." >&2
case $status in
0) ;;
2)
echo "$IMAGE:$GITHUB_SHA does not exist: the default branch's build of $GITHUB_SHA pushes it (image.yml)." >&2
exit 1
fi
echo "$IMAGE:$tag is already $source ($GITHUB_SHA)."
exit 0
elif ! not_found; then
cat "$error" >&2
exit 1
fi
;;
*) exit 1 ;;
esac
status=0
current=$(registry_digest "$IMAGE:$tag") || status=$?
case $status in
0)
if [ "$current" != "$source" ]; then
echo "$IMAGE:$tag is already $current, not $source ($GITHUB_SHA); a version tag never moves." >&2
exit 1
fi
echo "$IMAGE:$tag is already $source ($GITHUB_SHA)."
exit 0
;;
2) ;;
*) exit 1 ;;
esac
# A carbon copy of the manifest: the version tag gets the commit image's digest.
docker buildx imagetools create --prefer-index=false --tag "$IMAGE:$tag" "$IMAGE@$source"
[ "$(digest "$IMAGE:$tag")" = "$source" ] || { echo "$IMAGE:$tag is not $source" >&2; exit 1; }
echo "$IMAGE:$tag: $source ($GITHUB_SHA)"
registry_copy "$IMAGE@$source" "$IMAGE:$tag"
37 changes: 37 additions & 0 deletions .github/workflows/rerun.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Re-run

on:
workflow_run:
workflows: [Image, Release]
types: [completed]

# GitHub fails often: a runner lost mid-job, a 5xx from ghcr.io or the API, a download that
# checkout or a setup action could not finish. When a run of Image or Release started by a push
# fails, this re-runs its failed jobs once, 5 minutes later. Both are safe to run again (see
# each). A run that fails again waits for a person: re-run it from the Actions tab.

permissions:
actions: write

jobs:
rerun:
if: >-
github.event.workflow_run.conclusion == 'failure' && github.event.workflow_run.event == 'push' && github.event.workflow_run.run_attempt == 1
runs-on: gf-public-4
timeout-minutes: 15
steps:
- name: Re-run the failed jobs
env:
GH_TOKEN: ${{ github.token }}
RUN: ${{ github.event.workflow_run.id }}
run: |
sleep 300
for attempt in 1 2 3 4 5 6; do
if curl -fsS -X POST -H "Authorization: Bearer $GH_TOKEN" -H 'Accept: application/vnd.github+json' \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/actions/runs/$RUN/rerun-failed-jobs"; then
echo "Re-running the failed jobs of $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$RUN."
exit 0
fi
sleep $((attempt * 30))
done
exit 1
Loading