|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2023 The Kubernetes 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 | +# This script is executed in the Update-Bot container. |
| 18 | +# It checks if the Dockerfile for the build container has changed. |
| 19 | +# If so, it uses the version of the main branch as the basis for creating a new image tag. |
| 20 | +# The script also checks if the image tag for the build image exists in the main branch. |
| 21 | +# We assume that only one branch and one PR will be created for the changes in the build container. |
| 22 | +# Therefore, we can update this branch as many times as we want and reuse the next available tag after the one from the main branch. |
| 23 | + |
| 24 | +set -o errexit |
| 25 | +set -o nounset |
| 26 | +set -o pipefail |
| 27 | + |
| 28 | +export BUILDER_IMAGE=ghcr.io/sovereigncloudstack/cspo-builder |
| 29 | + |
| 30 | +REPO_ROOT=$(realpath $(dirname "${BASH_SOURCE[0]}")/..) |
| 31 | +cd "${REPO_ROOT}" || exit 1 |
| 32 | + |
| 33 | +source "${REPO_ROOT}/hack/semver-upgrade.sh" |
| 34 | + |
| 35 | +if git diff --exit-code .builder-image-version.txt images/builder/Dockerfile images/builder/build.sh > /dev/null; then |
| 36 | + echo "nothing seems to have changed." |
| 37 | + exit 0 |
| 38 | +fi |
| 39 | + |
| 40 | +if [ "${CI:-false}" = true ] ; then |
| 41 | + echo "$BUILD_IMAGE_TOKEN" | docker login ghcr.io -u "$BUILD_IMAGE_USER" --password-stdin |
| 42 | +fi |
| 43 | + |
| 44 | +VERSION=$(git fetch --quiet origin main && git show origin/main:.builder-image-version.txt) |
| 45 | +if [ -z "$VERSION" ]; then |
| 46 | + echo "failed to find BUILDER_IMAGE_VERSION in Makefile of origin/main branch" |
| 47 | + exit 1 |
| 48 | +fi |
| 49 | +export VERSION |
| 50 | + |
| 51 | +NEW_VERSION=$(semver_upgrade patch "$VERSION") |
| 52 | +export NEW_VERSION |
| 53 | + |
| 54 | +if ! docker manifest inspect "$BUILDER_IMAGE:$VERSION" > /dev/null; then |
| 55 | + echo "could not find image $BUILDER_IMAGE:$VERSION" |
| 56 | + exit 1 |
| 57 | +fi |
| 58 | +echo "$NEW_VERSION" > .builder-image-version.txt |
| 59 | +echo "Wrote new version $NEW_VERSION to .builder-image-version.txt" |
| 60 | + |
| 61 | +if docker manifest inspect ghcr.io/sovereigncloudstack/cspo-builder:${NEW_VERSION} > /dev/null ; echo $?; then |
| 62 | + |
| 63 | + sed -i -e "/^BUILDER_IMAGE_VERSION /s/:=.*$/:= ${NEW_VERSION}/" Makefile |
| 64 | + grep -r -E 'ghcr.io/sovereigncloudstack/cspo-builder:[0-9].*.*' -l | xargs sed -i -e "s/ghcr.io\/sovereigncloudstack\/cspo-builder:${VERSION}/ghcr.io\/sovereigncloudstack\/cspo-builder:${NEW_VERSION}/g" |
| 65 | + docker build -t ghcr.io/sovereigncloudstack/cspo-builder:${NEW_VERSION} ./images/builder |
| 66 | + docker push ghcr.io/sovereigncloudstack/cspo-builder:${NEW_VERSION} |
| 67 | +else |
| 68 | + exit 1 |
| 69 | +fi |
0 commit comments