|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# Usage: ./create-manifest.sh <flavor> <registry_image> <mirror_image> <mirror_enable_bool> |
| 5 | +# Example: ./create-manifest.sh alpine ghcr.io/owner/repo docker.io/owner/repo true |
| 6 | + |
| 7 | +FLAVOR="$1" |
| 8 | +REGISTRY_IMAGE="$2" |
| 9 | +MIRROR_IMAGE="$3" |
| 10 | +ENABLE_MIRROR="$4" |
| 11 | + |
| 12 | +# Validate inputs |
| 13 | +if [[ -z "$FLAVOR" ]]; then |
| 14 | + echo "Error: Flavor argument is missing." |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +if [[ -z "$REGISTRY_IMAGE" ]]; then |
| 19 | + echo "Error: Registry image argument is missing." |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +# Define architectures for each flavor |
| 24 | +# Default to amd64 and arm64 |
| 25 | +ARCHS="amd64 arm64" |
| 26 | + |
| 27 | +# Add s390x for Ubuntu flavors |
| 28 | +if [[ "$FLAVOR" == "bionic" || "$FLAVOR" == "focal" || "$FLAVOR" == "jammy" || "$FLAVOR" == "noble" ]]; then |
| 29 | + ARCHS="$ARCHS s390x" |
| 30 | +fi |
| 31 | + |
| 32 | +# Fedora only supports amd64 in this setup |
| 33 | +if [[ "$FLAVOR" == "fedora" ]]; then |
| 34 | + ARCHS="amd64" |
| 35 | +fi |
| 36 | + |
| 37 | +echo "Creating manifest for flavor: $FLAVOR (Architectures: $ARCHS)" |
| 38 | + |
| 39 | +# Construct tag prefixes (handling git tags and aliases) |
| 40 | +declare -a PREFIXES=() |
| 41 | +if [[ "$GITHUB_REF_TYPE" == "tag" ]]; then |
| 42 | + TAG_NAME="$GITHUB_REF_NAME" |
| 43 | + PREFIXES+=("${TAG_NAME}-") |
| 44 | + |
| 45 | + # Aliasing logic: Matches tags ending in single digit revision (e.g. 1.2.1-1 -> 1.2.1) |
| 46 | + # If the tag matches the pattern (.*)-[0-9]$, we creates an alias for the base (group 1) |
| 47 | + if [[ "$TAG_NAME" =~ ^(.*)-[0-9]$ ]]; then |
| 48 | + TAG_BASE="${BASH_REMATCH[1]}" |
| 49 | + if [[ "$TAG_BASE" != "$TAG_NAME" ]]; then |
| 50 | + PREFIXES+=("${TAG_BASE}-") |
| 51 | + fi |
| 52 | + fi |
| 53 | +else |
| 54 | + # For master branch or other non-tags, we use an empty prefix to just tag as "flavor" |
| 55 | + PREFIXES+=("") |
| 56 | +fi |
| 57 | + |
| 58 | +# Loop through each calculated tag prefix and create manifests |
| 59 | +for TAG_PREFIX in "${PREFIXES[@]}"; do |
| 60 | + SOURCES="" |
| 61 | + MIRROR_SOURCES="" |
| 62 | + |
| 63 | + # Collect source images for all architectures |
| 64 | + for ARCH in $ARCHS; do |
| 65 | + SOURCES="$SOURCES ${REGISTRY_IMAGE}:${TAG_PREFIX}${FLAVOR}-${ARCH}" |
| 66 | + if [[ "$ENABLE_MIRROR" == "true" ]]; then |
| 67 | + MIRROR_SOURCES="$MIRROR_SOURCES ${MIRROR_IMAGE}:${TAG_PREFIX}${FLAVOR}-${ARCH}" |
| 68 | + fi |
| 69 | + done |
| 70 | + |
| 71 | + # Target Manifest Tag |
| 72 | + TARGET_TAG="${TAG_PREFIX}${FLAVOR}" |
| 73 | + |
| 74 | + # 1. Create GHCR Manifest |
| 75 | + echo "Creating manifest ${REGISTRY_IMAGE}:$TARGET_TAG" |
| 76 | + # Note: docker buildx imagetools create automatically pushes the manifest to the registry. |
| 77 | + docker buildx imagetools create -t "${REGISTRY_IMAGE}:$TARGET_TAG" $SOURCES |
| 78 | + |
| 79 | + # 2. Create Mirror Manifest (if enabled) |
| 80 | + if [[ "$ENABLE_MIRROR" == "true" ]]; then |
| 81 | + echo "Creating mirror manifest ${MIRROR_IMAGE}:$TARGET_TAG" |
| 82 | + if [[ "$DRY_RUN" != "true" ]]; then |
| 83 | + docker buildx imagetools create -t "${MIRROR_IMAGE}:$TARGET_TAG" $MIRROR_SOURCES |
| 84 | + else |
| 85 | + echo "DRY RUN: docker buildx imagetools create -t \"${MIRROR_IMAGE}:$TARGET_TAG\" $MIRROR_SOURCES" |
| 86 | + fi |
| 87 | + fi |
| 88 | + |
| 89 | + # 3. Handle specific "latest" tag logic for bookworm on master |
| 90 | + if [[ "$FLAVOR" == "bookworm" && "$GITHUB_REF" == "refs/heads/master" && "$TAG_PREFIX" == "" ]]; then |
| 91 | + echo "Tagging bookworm as latest" |
| 92 | + if [[ "$DRY_RUN" != "true" ]]; then |
| 93 | + docker buildx imagetools create -t "${REGISTRY_IMAGE}:latest" "${REGISTRY_IMAGE}:bookworm" |
| 94 | + if [[ "$ENABLE_MIRROR" == "true" ]]; then |
| 95 | + docker buildx imagetools create -t "${MIRROR_IMAGE}:latest" "${MIRROR_IMAGE}:bookworm" |
| 96 | + fi |
| 97 | + else |
| 98 | + echo "DRY RUN: docker buildx imagetools create -t \"${REGISTRY_IMAGE}:latest\" \"${REGISTRY_IMAGE}:bookworm\"" |
| 99 | + if [[ "$ENABLE_MIRROR" == "true" ]]; then |
| 100 | + echo "DRY RUN: docker buildx imagetools create -t \"${MIRROR_IMAGE}:latest\" \"${MIRROR_IMAGE}:bookworm\"" |
| 101 | + fi |
| 102 | + fi |
| 103 | + fi |
| 104 | + |
| 105 | + # 4. Create compatibility aliases (centos-rpm -> centos, etc.) |
| 106 | + # Note: Logic slightly adjusted from YAML to be generic if needed, |
| 107 | + # but sticking to specific requested aliases for now. |
| 108 | + |
| 109 | + if [[ "$FLAVOR" == "centos" ]]; then |
| 110 | + ALIAS_TAG="${TAG_PREFIX}centos-rpm" |
| 111 | + echo "Creating alias $ALIAS_TAG -> $TARGET_TAG" |
| 112 | + if [[ "$DRY_RUN" != "true" ]]; then |
| 113 | + docker buildx imagetools create -t "${REGISTRY_IMAGE}:$ALIAS_TAG" "${REGISTRY_IMAGE}:$TARGET_TAG" |
| 114 | + if [[ "$ENABLE_MIRROR" == "true" ]]; then |
| 115 | + docker buildx imagetools create -t "${MIRROR_IMAGE}:$ALIAS_TAG" "${MIRROR_IMAGE}:$TARGET_TAG" |
| 116 | + fi |
| 117 | + else |
| 118 | + echo "DRY RUN: docker buildx imagetools create -t \"${REGISTRY_IMAGE}:$ALIAS_TAG\" \"${REGISTRY_IMAGE}:$TARGET_TAG\"" |
| 119 | + if [[ "$ENABLE_MIRROR" == "true" ]]; then |
| 120 | + echo "DRY RUN: docker buildx imagetools create -t \"${MIRROR_IMAGE}:$ALIAS_TAG\" \"${MIRROR_IMAGE}:$TARGET_TAG\"" |
| 121 | + fi |
| 122 | + fi |
| 123 | + fi |
| 124 | + |
| 125 | + if [[ "$FLAVOR" == "fedora" ]]; then |
| 126 | + ALIAS_TAG="${TAG_PREFIX}fedora-rpm" |
| 127 | + echo "Creating alias $ALIAS_TAG -> $TARGET_TAG" |
| 128 | + if [[ "$DRY_RUN" != "true" ]]; then |
| 129 | + docker buildx imagetools create -t "${REGISTRY_IMAGE}:$ALIAS_TAG" "${REGISTRY_IMAGE}:$TARGET_TAG" |
| 130 | + if [[ "$ENABLE_MIRROR" == "true" ]]; then |
| 131 | + docker buildx imagetools create -t "${MIRROR_IMAGE}:$ALIAS_TAG" "${MIRROR_IMAGE}:$TARGET_TAG" |
| 132 | + fi |
| 133 | + else |
| 134 | + echo "DRY RUN: docker buildx imagetools create -t \"${REGISTRY_IMAGE}:$ALIAS_TAG\" \"${REGISTRY_IMAGE}:$TARGET_TAG\"" |
| 135 | + if [[ "$ENABLE_MIRROR" == "true" ]]; then |
| 136 | + echo "DRY RUN: docker buildx imagetools create -t \"${MIRROR_IMAGE}:$ALIAS_TAG\" \"${MIRROR_IMAGE}:$TARGET_TAG\"" |
| 137 | + fi |
| 138 | + fi |
| 139 | + fi |
| 140 | +done |
| 141 | + |
| 142 | +echo "Manifest creation complete for $FLAVOR" |
0 commit comments