Skip to content
Myr

[actions] cache container images, verified against digests looked up live

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Each container job now looks up the image's index, manifest, and config digests from the registry, keys an `actions/cache` entry on the config digest, and only uses a cached image if its ID matches one of those digests; otherwise it is discarded and the image is pulled (with retries) instead. A poisoned or stale cache entry can therefore never be used, and a cache hit needs only a couple of small manifest requests, so this scales regardless of pull rate limits.

authored by Jordan Harband · committed by Jordan Harband · parent 37be73f · Browse files · 4 files+170 added−26 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+170 added−26 removed

  • other:+170 added−26 removed

4 files+170 added−26 removed

added:.github/scripts/docker-image.sh+114 added
1#!/bin/sh23# Cache a container image as a tarball, keyed by digests looked up live from the registry: a cached4# image is only used if it is exactly what the registry serves now, so a poisoned cache entry is never used.5#6# usage:7# sh .github/scripts/docker-image.sh resolve <image>8# prints `key=<config digest>` and `digests=<index, manifest, and config digests>`, for $GITHUB_OUTPUT9# DIGESTS='<digests>' sh .github/scripts/docker-image.sh ensure <image> <tarball>10# loads <tarball> if it holds one of $DIGESTS; otherwise pulls <image>, checks it, and saves it to <tarball>1112set -eu1314ACCEPT='application/vnd.oci.image.index.v1+json,application/vnd.docker.distribution.manifest.list.v2+json,application/vnd.oci.image.manifest.v1+json,application/vnd.docker.distribution.manifest.v2+json'1516sha256_of() {17 if command -v sha256sum >/dev/null 2>&1; then18 sha256sum "$1"19 else20 shasum -a 256 "$1"21 fi | cut -d ' ' -f 122}2324# fetches a manifest into a file; digests are computed from its exact bytes25registry_get() {26 curl -fsSL --retry 5 --retry-all-errors -H "Accept: ${ACCEPT}" -o "$2" "$1"27}2829resolve() {30 IMAGE="$1"31 HOST="${IMAGE%%/*}"32 REST="${IMAGE#*/}"33 REPO="${REST%:*}"34 TAG="${REST##*:}"35 OS="$(docker version --format '{{.Server.Os}}')"36 ARCH="$(docker version --format '{{.Server.Arch}}')"3738 WORK="$(mktemp -d)"39 trap 'rm -rf "${WORK}"' EXIT4041 registry_get "https://${HOST}/v2/${REPO}/manifests/${TAG}" "${WORK}/index.json"42 INDEX_DIGEST="sha256:$(sha256_of "${WORK}/index.json")"4344 if jq -e '.manifests' "${WORK}/index.json" >/dev/null; then45 MANIFEST_DIGEST="$(jq -r --arg os "${OS}" --arg arch "${ARCH}" '[.manifests[] | select(.platform.os == $os and .platform.architecture == $arch)][0].digest // empty' "${WORK}/index.json")"46 if [ -z "${MANIFEST_DIGEST}" ]; then47 echo "${IMAGE} has no ${OS}/${ARCH} image" >&248 exit 149 fi50 registry_get "https://${HOST}/v2/${REPO}/manifests/${MANIFEST_DIGEST}" "${WORK}/manifest.json"51 if [ "sha256:$(sha256_of "${WORK}/manifest.json")" != "${MANIFEST_DIGEST}" ]; then52 echo "the ${OS}/${ARCH} manifest of ${IMAGE} does not match its digest" >&253 exit 154 fi55 else56 MANIFEST_DIGEST="${INDEX_DIGEST}"57 cp "${WORK}/index.json" "${WORK}/manifest.json"58 fi5960 CONFIG_DIGEST="$(jq -r '.config.digest' "${WORK}/manifest.json")"61 echo "key=${CONFIG_DIGEST}"62 echo "digests=${INDEX_DIGEST} ${MANIFEST_DIGEST} ${CONFIG_DIGEST}"63}6465# an image's ID is its config digest (or, with the containerd image store, its manifest or index digest)66image_matches() {67 ID="$(docker image inspect --format '{{.Id}}' "$1" 2>/dev/null)" || return 168 case " ${DIGESTS} " in69 *" ${ID} "*) return 0 ;;70 esac71 return 172}7374ensure() {75 IMAGE="$1"76 TARBALL="$2"7778 if [ -f "${TARBALL}" ]; then79 if docker load -i "${TARBALL}" && image_matches "${IMAGE}"; then80 echo "loaded ${IMAGE} from the cache"81 return 082 fi83 echo "::warning::the cached ${IMAGE} does not match the registry's digests; pulling it instead"84 docker image rm -f "${IMAGE}" >/dev/null 2>&1 || true85 rm -f "${TARBALL}"86 fi8788 ATTEMPT=189 until docker pull "${IMAGE}"; do90 if [ "${ATTEMPT}" -ge 5 ]; then91 echo "docker pull ${IMAGE} failed after ${ATTEMPT} attempts" >&292 return 193 fi94 echo "docker pull failed, attempt ${ATTEMPT}/5"95 sleep $((ATTEMPT * 5))96 ATTEMPT=$((ATTEMPT + 1))97 done9899 # the tag can move between `resolve` and the pull; then this run uses the image, but does not cache it100 if image_matches "${IMAGE}"; then101 docker save -o "${TARBALL}" "${IMAGE}"102 else103 echo "::warning::the pulled ${IMAGE} no longer matches the digests looked up earlier; not caching it"104 fi105}106107case "${1-}" in108 resolve) resolve "${2-}" ;;109 ensure) ensure "${2-}" "${3-}" ;;110 *)111 echo 'usage: docker-image.sh resolve <image> | ensure <image> <tarball>' >&2112 exit 2113 ;;114esac
modified:.github/workflows/tests-alpine.yml+28 added-12 removed
65 unmodified lines
66 with:67 node-version: 'lts/*'68 skip-ls-check: true69 - run: npm ls urchin70 # Pull through Google's Docker Hub mirror, which is not subject to Docker Hub's71 # anonymous pull rate limits, and cache the image; a cached image is only used72 # if it matches the digests the registry serves now73 - name: 'Resolve the alpine:${{ matrix.alpine }} image digests'74 id: image75 run: sh .github/scripts/docker-image.sh resolve mirror.gcr.io/library/alpine:${{ matrix.alpine }} >> "$GITHUB_OUTPUT"76 - uses: actions/cache@v677 with:78 path: ${{ runner.temp }}/docker-image.tar79 key: docker-image-${{ runner.arch }}-${{ steps.image.outputs.key }}80 - name: 'Load or pull the alpine:${{ matrix.alpine }} image'81 env:82 DIGESTS: ${{ steps.image.outputs.digests }}83 run: sh .github/scripts/docker-image.sh ensure mirror.gcr.io/library/alpine:${{ matrix.alpine }} "${RUNNER_TEMP}/docker-image.tar"84 - name: 'Run fast tests on Alpine ${{ matrix.alpine }} (${{ matrix.arch }})'85 run: |72 # Pull through Google's Docker Hub mirror, which is not subject to Docker Hub's73 # anonymous pull rate limits; retry to tolerate transient registry failures74 for i in 1 2 3 4 5; do75 docker pull mirror.gcr.io/library/alpine:${{ matrix.alpine }} && break76 echo "docker pull failed, attempt $i/5"; sleep $((i * 5))77 done86 docker run --rm \87 -v "${{ github.workspace }}:/workspace" \88 -w /workspace \89 -e "TEST_SHELL=${{ matrix.shell }}" \
68 unmodified lines
158 production.cloudfront.docker.com:443159 - uses: actions/checkout@v6160 with:161 submodules: false162 # Pull through Google's Docker Hub mirror, which is not subject to Docker Hub's163 # anonymous pull rate limits, and cache the image; a cached image is only used164 # if it matches the digests the registry serves now165 - name: 'Resolve the alpine:${{ matrix.alpine }} image digests'166 id: image167 run: sh .github/scripts/docker-image.sh resolve mirror.gcr.io/library/alpine:${{ matrix.alpine }} >> "$GITHUB_OUTPUT"168 - uses: actions/cache@v6169 with:170 path: ${{ runner.temp }}/docker-image.tar171 key: docker-image-${{ runner.arch }}-${{ steps.image.outputs.key }}172 - name: 'Load or pull the alpine:${{ matrix.alpine }} image'173 env:174 DIGESTS: ${{ steps.image.outputs.digests }}175 run: sh .github/scripts/docker-image.sh ensure mirror.gcr.io/library/alpine:${{ matrix.alpine }} "${RUNNER_TEMP}/docker-image.tar"176 - name: 'Install node ${{ matrix.node }} from a musl binary on Alpine ${{ matrix.alpine }}'177 run: |156 # Pull through Google's Docker Hub mirror, which is not subject to Docker Hub's157 # anonymous pull rate limits; retry to tolerate transient registry failures158 for i in 1 2 3 4 5; do159 docker pull mirror.gcr.io/library/alpine:${{ matrix.alpine }} && break160 echo "docker pull failed, attempt $i/5"; sleep $((i * 5))161 done178 docker run --rm \179 -v "${{ github.workspace }}:/workspace" \180 -w /workspace \181 -e "NODE_VERSION=${{ matrix.node }}" \
34 unmodified lines
modified:.github/workflows/tests-installation-node.yml+14 added-7 removed
modified:.github/workflows/tests-xenial.yml+14 added-7 removed
59 unmodified lines
60 node-version: 'lts/*'61 skip-ls-check: true62 - run: npm ls urchin63 - run: npx which urchin64 # Pull through Google's Docker Hub mirror, which is not subject to Docker Hub's65 # anonymous pull rate limits, and cache the image; a cached image is only used66 # if it matches the digests the registry serves now67 - name: Resolve the ubuntu:16.04 image digests68 id: image69 run: sh .github/scripts/docker-image.sh resolve mirror.gcr.io/library/ubuntu:16.04 >> "$GITHUB_OUTPUT"70 - uses: actions/cache@v671 with:72 path: ${{ runner.temp }}/docker-image.tar73 key: docker-image-${{ runner.arch }}-${{ steps.image.outputs.key }}74 - name: Load or pull the ubuntu:16.04 image75 env:76 DIGESTS: ${{ steps.image.outputs.digests }}77 run: sh .github/scripts/docker-image.sh ensure mirror.gcr.io/library/ubuntu:16.04 "${RUNNER_TEMP}/docker-image.tar"78 - name: Run xenial tests in container79 run: |66 # Pull through Google's Docker Hub mirror, which is not subject to Docker Hub's67 # anonymous pull rate limits; retry to tolerate transient registry failures68 for i in 1 2 3 4 5; do69 docker pull mirror.gcr.io/library/ubuntu:16.04 && break70 echo "docker pull failed, attempt $i/5"71 sleep $((i * 5))72 done80 docker run --rm \81 -v "${{ github.workspace }}:/workspace" \82 -w /workspace \83 -e "TEST_SHELL=${{ matrix.shell }}" \
42 unmodified lines