|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2025 The KCP 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 -euo pipefail |
| 18 | + |
| 19 | +# make git available |
| 20 | +if ! [ -x "$(command -v git)" ]; then |
| 21 | + echo "Installing git…" |
| 22 | + dnf install -y git |
| 23 | +fi |
| 24 | + |
| 25 | +# in CI, make use of the registry mirror to avoid getting rate limited |
| 26 | +if [ -n "${DOCKER_REGISTRY_MIRROR_ADDR:-}" ]; then |
| 27 | + # remove "http://" or "https://" prefix |
| 28 | + mirror="$(echo "$DOCKER_REGISTRY_MIRROR_ADDR" | awk -F// '{print $NF}')" |
| 29 | + |
| 30 | + echo "Configuring registry mirror for docker.io…" |
| 31 | + |
| 32 | + cat <<EOF > /etc/containers/registries.conf.d/mirror.conf |
| 33 | +[[registry]] |
| 34 | +prefix = "docker.io" |
| 35 | +insecure = true |
| 36 | +location = "$mirror" |
| 37 | +EOF |
| 38 | +fi |
| 39 | + |
| 40 | +repository=ghcr.io/kcp-dev/api-syncagent |
| 41 | +architectures="${ARCHITECTURES:-amd64 arm64}" |
| 42 | + |
| 43 | +# when building locally, just tag with the current HEAD hash |
| 44 | +version="${IMAGE_TAG:-}" |
| 45 | +branchName="" |
| 46 | + |
| 47 | +if [ -z "$version" ]; then |
| 48 | + version="$(git rev-parse --short HEAD)" |
| 49 | + |
| 50 | + # deduce the tag from the Prow job metadata |
| 51 | + if [ -n "${PULL_BASE_REF:-}" ]; then |
| 52 | + version="$(git tag --list "$PULL_BASE_REF")" |
| 53 | + |
| 54 | + if [ -z "$version" ]; then |
| 55 | + # if the base ref did not point to a tag, it's a branch name |
| 56 | + version="$(git rev-parse --short "$PULL_BASE_REF")" |
| 57 | + branchName="$PULL_BASE_REF" |
| 58 | + else |
| 59 | + # If PULL_BASE_REF is a tag, there is no branch available locally, plus |
| 60 | + # there is no guarantee that vX.Y.Z is tagged _only_ in the release-X.Y |
| 61 | + # branch; because of this we have to deduce the branch name from the tag |
| 62 | + branchName="$(echo "$version" | sed -E 's/^v([0-9]+)\.([0-9]+)\..*/release-\1.\2/')" |
| 63 | + fi |
| 64 | + fi |
| 65 | +fi |
| 66 | + |
| 67 | +image="$repository:$version" |
| 68 | +echo "Building container image $image…" |
| 69 | + |
| 70 | +# build image for all architectures |
| 71 | +for arch in $architectures; do |
| 72 | + fullTag="$image-$arch" |
| 73 | + |
| 74 | + echo "Building $version-$arch…" |
| 75 | + buildah build-using-dockerfile \ |
| 76 | + --file Dockerfile \ |
| 77 | + --tag "$fullTag" \ |
| 78 | + --arch "$arch" \ |
| 79 | + --override-arch "$arch" \ |
| 80 | + --build-arg "TARGETOS=linux" \ |
| 81 | + --build-arg "TARGETARCH=$arch" \ |
| 82 | + --format=docker \ |
| 83 | + . |
| 84 | +done |
| 85 | + |
| 86 | +echo "Creating manifest $image…" |
| 87 | +buildah manifest create "$image" |
| 88 | +for arch in $architectures; do |
| 89 | + buildah manifest add "$image" "$image-$arch" |
| 90 | +done |
| 91 | + |
| 92 | +# Additionally to an image tagged with the Git tag, we also |
| 93 | +# release images tagged with the current branch name, which |
| 94 | +# is somewhere between a blanket "latest" tag and a specific |
| 95 | +# tag. |
| 96 | +if [ -n "$branchName" ]; then |
| 97 | + branchImage="$repository:$branchName" |
| 98 | + |
| 99 | + echo "Creating manifest $branchImage…" |
| 100 | + buildah manifest create "$branchImage" |
| 101 | + for arch in $architectures; do |
| 102 | + buildah manifest add "$branchImage" "$image-$arch" |
| 103 | + done |
| 104 | +fi |
| 105 | + |
| 106 | +# push manifest, except in presubmits |
| 107 | +if [ -z "${DRY_RUN:-}" ]; then |
| 108 | + echo "Logging into GHCR…" |
| 109 | + buildah login --username "$KCP_GHCR_USERNAME" --password "$KCP_GHCR_PASSWORD" ghcr.io |
| 110 | + |
| 111 | + echo "Pushing manifest and images…" |
| 112 | + buildah manifest push --all "$image" "docker://$image" |
| 113 | + |
| 114 | + if [ -n "${branchImage:-}" ]; then |
| 115 | + buildah manifest push --all "$branchImage" "docker://$branchImage" |
| 116 | + fi |
| 117 | +else |
| 118 | + echo "Not pushing images because \$DRY_RUN is set." |
| 119 | +fi |
| 120 | + |
| 121 | +echo "Done." |
0 commit comments