docker-image.sh
This file
Path
.github/scripts/docker-image.sh
114 lines · 3.8 KiB · Shell
Last changed by
[actions] cache container images, verified against digests looked up liveJordan Harbandbefore adoption5c05fb6
History of this fileTake it
[actions] cache container images, verified against digests looked up liveJordan Harbandbefore adoption5c05fb6
lines 1–114why, one step back1#!/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