|
| 1 | +#!/usr/bin/env bash |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Updates the pinned sha256 digest for node:22-slim in the Dockerfile. |
| 6 | +# Queries Docker Hub for the current linux/amd64 manifest digest and |
| 7 | +# rewrites every FROM line that references node:22-slim. |
| 8 | +# |
| 9 | +# Usage: |
| 10 | +# scripts/update-docker-pin.sh # update Dockerfile in repo root |
| 11 | +# scripts/update-docker-pin.sh --check # exit 0 if up-to-date, 1 if stale |
| 12 | + |
| 13 | +set -euo pipefail |
| 14 | + |
| 15 | +case "${1:-}" in |
| 16 | + "" | --check) ;; |
| 17 | + *) |
| 18 | + echo "Usage: scripts/update-docker-pin.sh [--check]" >&2 |
| 19 | + exit 2 |
| 20 | + ;; |
| 21 | +esac |
| 22 | + |
| 23 | +IMAGE="node" |
| 24 | +TAG="22-slim" |
| 25 | +DOCKERFILE="${DOCKERFILE:-Dockerfile}" |
| 26 | +REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 27 | +DOCKERFILE_PATH="${REPO_ROOT}/${DOCKERFILE}" |
| 28 | + |
| 29 | +# --------------------------------------------------------------------------- |
| 30 | +# Resolve the current multi-arch manifest digest for linux/amd64 |
| 31 | +# --------------------------------------------------------------------------- |
| 32 | +resolve_latest_digest() { |
| 33 | + local token manifest |
| 34 | + |
| 35 | + # Step 1: get an auth token for the Docker Hub library repo |
| 36 | + token=$(curl -fsSL --retry 3 --retry-delay 1 --retry-all-errors \ |
| 37 | + --connect-timeout 10 --max-time 30 \ |
| 38 | + "https://auth.docker.io/token?service=registry.docker.io&scope=repository:library/${IMAGE}:pull" \ |
| 39 | + | python3 -c "import sys,json; print(json.load(sys.stdin)['token'])") |
| 40 | + |
| 41 | + if [[ -z "$token" ]]; then |
| 42 | + echo "ERROR: failed to obtain Docker Hub auth token" >&2 |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | + |
| 46 | + # Step 2: fetch the manifest list and pick the linux/amd64 digest |
| 47 | + manifest=$(curl -fsSL --retry 3 --retry-delay 1 --retry-all-errors \ |
| 48 | + --connect-timeout 10 --max-time 30 \ |
| 49 | + -H "Authorization: Bearer ${token}" \ |
| 50 | + -H "Accept: application/vnd.docker.distribution.manifest.list.v2+json, application/vnd.oci.image.index.v1+json" \ |
| 51 | + "https://registry-1.docker.io/v2/library/${IMAGE}/manifests/${TAG}") |
| 52 | + |
| 53 | + local digest |
| 54 | + digest=$(echo "$manifest" | python3 -c " |
| 55 | +import sys, json |
| 56 | +data = json.load(sys.stdin) |
| 57 | +for m in data.get('manifests', []): |
| 58 | + p = m.get('platform', {}) |
| 59 | + if p.get('os') == 'linux' and p.get('architecture') == 'amd64' and not p.get('variant'): |
| 60 | + print(m['digest']) |
| 61 | + break |
| 62 | +") |
| 63 | + |
| 64 | + if [[ -z "$digest" ]]; then |
| 65 | + echo "ERROR: could not resolve digest for ${IMAGE}:${TAG}" >&2 |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | + |
| 69 | + echo "$digest" |
| 70 | +} |
| 71 | + |
| 72 | +# --------------------------------------------------------------------------- |
| 73 | +# Extract the currently-pinned digest from the Dockerfile (first match) |
| 74 | +# --------------------------------------------------------------------------- |
| 75 | +current_digest() { |
| 76 | + sed -n "s|^FROM ${IMAGE}:${TAG}@\(sha256:[a-f0-9]*\).*|\1|p" "$DOCKERFILE_PATH" | head -1 |
| 77 | +} |
| 78 | + |
| 79 | +# --------------------------------------------------------------------------- |
| 80 | +# Main |
| 81 | +# --------------------------------------------------------------------------- |
| 82 | +latest=$(resolve_latest_digest) |
| 83 | +pinned=$(current_digest) |
| 84 | + |
| 85 | +if [[ -z "$pinned" ]]; then |
| 86 | + echo "ERROR: no pinned digest found in ${DOCKERFILE} for ${IMAGE}:${TAG}" >&2 |
| 87 | + echo "Expected lines like: FROM ${IMAGE}:${TAG}@sha256:..." >&2 |
| 88 | + exit 1 |
| 89 | +fi |
| 90 | + |
| 91 | +if [[ "$pinned" == "$latest" ]]; then |
| 92 | + echo "OK: ${IMAGE}:${TAG} digest is already up-to-date (${pinned})" |
| 93 | + exit 0 |
| 94 | +fi |
| 95 | + |
| 96 | +if [[ "${1:-}" == "--check" ]]; then |
| 97 | + echo "STALE: pinned digest does not match the latest upstream digest." |
| 98 | + echo "" |
| 99 | + echo " pinned: ${pinned}" |
| 100 | + echo " latest: ${latest}" |
| 101 | + echo "" |
| 102 | + echo "To update, run:" |
| 103 | + echo "" |
| 104 | + echo " scripts/update-docker-pin.sh" |
| 105 | + echo "" |
| 106 | + exit 1 |
| 107 | +fi |
| 108 | + |
| 109 | +# Perform the replacement |
| 110 | +sed -i.bak "s|${IMAGE}:${TAG}@${pinned}|${IMAGE}:${TAG}@${latest}|g" "$DOCKERFILE_PATH" |
| 111 | +rm -f "${DOCKERFILE_PATH}.bak" |
| 112 | + |
| 113 | +echo "Updated ${DOCKERFILE}: ${IMAGE}:${TAG} digest" |
| 114 | +echo " old: ${pinned}" |
| 115 | +echo " new: ${latest}" |
0 commit comments