|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2025 The Tekton Authors |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +set -o errexit |
| 18 | +set -o nounset |
| 19 | +set -o pipefail |
| 20 | + |
| 21 | +# Print error message and exit 1 |
| 22 | +# Parameters: $1..$n - error message to be displayed |
| 23 | +function abort() { |
| 24 | + echo "error: $*" >&2 |
| 25 | + exit 1 |
| 26 | +} |
| 27 | + |
| 28 | +# Check if crane is available |
| 29 | +if ! command -v crane &>/dev/null; then |
| 30 | + abort "crane command not found. Please install it from https://github.com/google/go-containerregistry" |
| 31 | +fi |
| 32 | + |
| 33 | +# Check if .ko.yaml exists |
| 34 | +if [[ ! -f ".ko.yaml" ]]; then |
| 35 | + abort ".ko.yaml file not found in current directory" |
| 36 | +fi |
| 37 | + |
| 38 | +# Set default KOCACHE if not set |
| 39 | +KOCACHE="${KOCACHE:-${HOME}/.ko}" |
| 40 | + |
| 41 | +# Create output directory if it doesn't exist |
| 42 | +IMG_DIR="${KOCACHE}/img" |
| 43 | +mkdir -p "${IMG_DIR}" |
| 44 | + |
| 45 | +# Extract images with digests from .ko.yaml |
| 46 | +# Look for lines containing @sha256: or @sha512: followed by a digest |
| 47 | +# Extract the image reference (everything from start of image to end of digest, ignoring comments) |
| 48 | +DIGEST_IMAGES=() |
| 49 | + |
| 50 | +while IFS= read -r line; do |
| 51 | + # Skip comments and empty lines |
| 52 | + line_trimmed=$(echo "${line}" | sed 's/#.*$//' | sed 's/^[[:space:]]*//' | sed 's/[[:space:]]*$//') |
| 53 | + if [[ -z "${line_trimmed}" ]]; then |
| 54 | + continue |
| 55 | + fi |
| 56 | + |
| 57 | + # Extract image references with digests |
| 58 | + # Pattern: match image refs like registry/repo:tag@sha256:digest or registry/repo@sha256:digest |
| 59 | + if echo "${line_trimmed}" | grep -qE '@sha(256|512):[a-f0-9]+'; then |
| 60 | + # Extract the value part after the colon (for YAML key: value) |
| 61 | + if echo "${line_trimmed}" | grep -qE '^[^:]+:[[:space:]]+'; then |
| 62 | + # YAML key: value format - extract the value |
| 63 | + image=$(echo "${line_trimmed}" | sed -E 's/^[^:]+:[[:space:]]+//' | sed 's/#.*$//' | sed 's/[[:space:]]*$//') |
| 64 | + # Check if it's an image with digest |
| 65 | + if echo "${image}" | grep -qE '@sha(256|512):[a-f0-9]+'; then |
| 66 | + DIGEST_IMAGES+=("${image}") |
| 67 | + fi |
| 68 | + else |
| 69 | + # Might be standalone or in a different format, try to extract directly |
| 70 | + image=$(echo "${line_trimmed}" | grep -oE '[^[:space:]]+@sha(256|512):[a-f0-9]+' | head -1) |
| 71 | + if [[ -n "${image}" ]]; then |
| 72 | + DIGEST_IMAGES+=("${image}") |
| 73 | + fi |
| 74 | + fi |
| 75 | + fi |
| 76 | +done <.ko.yaml |
| 77 | + |
| 78 | +if [[ ${#DIGEST_IMAGES[@]} -eq 0 ]]; then |
| 79 | + echo "No images with digests found in .ko.yaml" |
| 80 | + exit 0 |
| 81 | +fi |
| 82 | + |
| 83 | +# Remove duplicates |
| 84 | +IFS=$'\n' sorted_unique_images=($(sort -u <<<"${DIGEST_IMAGES[*]}")) |
| 85 | +unset IFS |
| 86 | + |
| 87 | +echo "Found ${#sorted_unique_images[@]} unique image(s) with digests in .ko.yaml:" |
| 88 | +for img in "${sorted_unique_images[@]}"; do |
| 89 | + echo " - ${img}" |
| 90 | +done |
| 91 | +echo "" |
| 92 | + |
| 93 | +# Pull each image |
| 94 | +for image in "${sorted_unique_images[@]}"; do |
| 95 | + echo "Pulling ${image}..." |
| 96 | + if crane pull --format oci "${image}" "${IMG_DIR}"; then |
| 97 | + echo "Successfully pulled ${image} to ${IMG_DIR}" |
| 98 | + else |
| 99 | + abort "Failed to pull ${image}" |
| 100 | + fi |
| 101 | +done |
| 102 | + |
| 103 | +echo "" |
| 104 | +echo "All images have been pulled to ${IMG_DIR}" |
0 commit comments