|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -xeuo pipefail |
| 3 | + |
| 4 | +# |
| 5 | +# Builds and creates multi-arch manifest; optionally pushes. Required variables: |
| 6 | +# |
| 7 | +# FROM_REF - base ref (e.g. quay.io/fedora/fedora-bootc:43) |
| 8 | +# DST_REF - destination ref image without image type suffix (e.g. quay.io/osbuild/fedora-bootc:43) |
| 9 | +# CONTAINERFILE - containerfile suffix without type (e.g. fedora → Containerfile.fedora-xxx) |
| 10 | +# |
| 11 | +# Optional variables: |
| 12 | +# |
| 13 | +# ARCHES, TYPES - space-separated lists (default from .gitlab-ci.yml: x86_64, qcow2) |
| 14 | +# FROM_CREDS - colon-separated username:password for source registry (none for public registries) |
| 15 | +# DST_CREDS - colon-separated username:password for destination registry (skip push if missing) |
| 16 | +# CACHE_REF, CACHE_USERNAME, CACHE_PASSWORD - sidecar cache registry (optional) |
| 17 | + |
| 18 | +. "$(dirname "$0")/gitlab-utils.sh" |
| 19 | + |
| 20 | +# Parse colon-separated username:password (password may contain colons) |
| 21 | +if [ -n "${FROM_CREDS:-}" ] && [[ "${FROM_CREDS}" == *:* ]]; then |
| 22 | + FROM_USER="${FROM_CREDS%%:*}" |
| 23 | + FROM_PASS="${FROM_CREDS#*:}" |
| 24 | + section_start login_from_registry "Logging in to $FROM_REF" |
| 25 | + TO_REGISTRY=$(echo "$FROM_REF" | cut -d/ -f1) |
| 26 | + echo "$FROM_PASS" | buildah login -u "$FROM_USER" --password-stdin "$TO_REGISTRY" |
| 27 | + section_end login_from_registry |
| 28 | +fi |
| 29 | + |
| 30 | +if [ -n "${DST_CREDS:-}" ] && [[ "${DST_CREDS}" == *:* ]]; then |
| 31 | + DST_USER="${DST_CREDS%%:*}" |
| 32 | + DST_PASS="${DST_CREDS#*:}" |
| 33 | + section_start login_dst_registry "Logging in to $DST_REF" |
| 34 | + TO_REGISTRY=$(echo "$DST_REF" | cut -d/ -f1) |
| 35 | + echo "$DST_PASS" | buildah login -u "$DST_USER" --password-stdin "$TO_REGISTRY" |
| 36 | + section_end login_dst_registry |
| 37 | +fi |
| 38 | + |
| 39 | +if [ -n "${CACHE_REF:-}" ] && [ -n "${CACHE_USERNAME:-}" ] && [ -n "${CACHE_PASSWORD:-}" ]; then |
| 40 | + section_start login_cache_ref "Logging in to $CACHE_REF (cache will be used)" |
| 41 | + CACHE_REGISTRY=$(echo "$CACHE_REF" | cut -d/ -f1) |
| 42 | + echo "$CACHE_PASSWORD" | buildah login -u "$CACHE_USERNAME" --password-stdin "$CACHE_REGISTRY" |
| 43 | + section_end login_cache_ref |
| 44 | + |
| 45 | + section_start pull_cache "Pulling from cache ref ${CACHE_REF}" |
| 46 | + echo "Using cache $CACHE_REF" |
| 47 | + buildah pull "$CACHE_REF" || true |
| 48 | + buildah pull "$FROM_REF" |
| 49 | + section_end pull_cache |
| 50 | + |
| 51 | + section_start push_to_cache "Pushing to cache ref ${CACHE_REF}" |
| 52 | + buildah push "$FROM_REF" "$CACHE_REF" || true |
| 53 | + section_end push_to_cache |
| 54 | +fi |
| 55 | + |
| 56 | +NOPUSH= |
| 57 | +if [ -z "${DST_REF:-}" ]; then |
| 58 | + DST_REF="local" |
| 59 | + NOPUSH=1 |
| 60 | +fi |
| 61 | + |
| 62 | +ARCHES=${ARCHES:-x86_64} |
| 63 | +TYPES=${TYPES:-qcow2} |
| 64 | +DST_REF=${DST_REF:-local} |
| 65 | + |
| 66 | +for ARCH in $ARCHES; do |
| 67 | + for TYPE in $TYPES; do |
| 68 | + section_start prepare_containerfile "Preparing Containerfile with FROM ${FROM_REF}" |
| 69 | + cp "Containerfile.comment" "Containerfile" |
| 70 | + { |
| 71 | + echo "FROM ${FROM_REF}" |
| 72 | + echo "ARG BUILD_DATE=$(date +%Y-%m-%d)" |
| 73 | + tail -n +2 "Containerfile.${CONTAINERFILE}-${TYPE}" |
| 74 | + } >> "Containerfile" |
| 75 | + section_end prepare_containerfile |
| 76 | + |
| 77 | + section_start "build_${ARCH}_${TYPE}" "Building $FROM_REF arch $ARCH type $TYPE" |
| 78 | + buildah build --layers --arch="$ARCH" \ |
| 79 | + --build-arg CONTAINERFILE="Containerfile" \ |
| 80 | + --from "${FROM_REF}" \ |
| 81 | + -f "Containerfile" \ |
| 82 | + -t "$DST_REF-$ARCH-$TYPE" . |
| 83 | + section_end "build_${ARCH}_${TYPE}" |
| 84 | + done |
| 85 | +done |
| 86 | + |
| 87 | +for TYPE in $TYPES; do |
| 88 | + for ARCH in $ARCHES; do |
| 89 | + section_start "push_${ARCH}_${TYPE}" "Pushing $DST_REF-$ARCH-$TYPE to registry" |
| 90 | + if [ -n "${DST_CREDS:-}" ] && [ -z "${NOPUSH:-}" ]; then |
| 91 | + echo "Pushing $DST_REF-$ARCH-$TYPE to registry" |
| 92 | + buildah push "$DST_REF-$ARCH-$TYPE" |
| 93 | + else |
| 94 | + echo "Push skipped: DST_CREDS missing or NOPUSH set" |
| 95 | + fi |
| 96 | + section_end "push_${ARCH}_${TYPE}" |
| 97 | + done |
| 98 | +done |
| 99 | + |
| 100 | +section_start create_manifest "Creating manifest for $DST_REF" |
| 101 | +buildah manifest create "$DST_REF" |
| 102 | +section_end create_manifest |
| 103 | + |
| 104 | +for TYPE in $TYPES; do |
| 105 | + section_start "add_to_manifest_${TYPE}" "Adding $TYPE to $DST_REF manifest" |
| 106 | + for ARCH in $ARCHES; do |
| 107 | + buildah manifest add "$DST_REF" "$DST_REF-$ARCH-$TYPE" |
| 108 | + done |
| 109 | + section_end "add_to_manifest_${TYPE}" |
| 110 | + |
| 111 | + if [ -n "${DST_CREDS:-}" ] && [ -z "${NOPUSH:-}" ]; then |
| 112 | + section_start "push_manifest_${TYPE}" "Pushing manifest $DST_REF to registry" |
| 113 | + buildah manifest push --all "$DST_REF" "docker://$DST_REF" |
| 114 | + section_end "push_manifest_${TYPE}" |
| 115 | + else |
| 116 | + echo "Push skipped: DST_CREDS missing or NOPUSH set" |
| 117 | + fi |
| 118 | +done |
0 commit comments