|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2022 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 file is run by cloudbuild, from cloudbuild.yaml, using `make release-staging`. |
| 18 | + |
| 19 | +set -o errexit |
| 20 | +set -o nounset |
| 21 | +set -o pipefail |
| 22 | + |
| 23 | +if [[ -z "${GIT_TAG-}" ]]; |
| 24 | +then |
| 25 | + echo "GIT_TAG env var must be set and nonempty." |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +if [[ -z "${BASE_REF-}" ]]; |
| 30 | +then |
| 31 | + echo "BASE_REF env var must be set and nonempty." |
| 32 | + exit 1 |
| 33 | +fi |
| 34 | + |
| 35 | +if [[ -z "${COMMIT-}" ]]; |
| 36 | +then |
| 37 | + echo "COMMIT env var must be set and nonempty." |
| 38 | + exit 1 |
| 39 | +fi |
| 40 | + |
| 41 | +if [[ -z "${REGISTRY-}" ]]; |
| 42 | +then |
| 43 | + echo "REGISTRY env var must be set and nonempty." |
| 44 | + exit 1 |
| 45 | +fi |
| 46 | + |
| 47 | + |
| 48 | +LATEST=false |
| 49 | + |
| 50 | +VERSION_TAG=$GIT_TAG |
| 51 | + |
| 52 | +BINARY_VERSION=$COMMIT |
| 53 | + |
| 54 | +# $BASE_REF has only two things that it can be set to by cloudbuild and Prow, |
| 55 | +# `master`, or a semver tag. |
| 56 | +# This is controlled by k8s.io/test-infra/config/jobs/image-pushing/k8s-staging-gateway-api.yaml. |
| 57 | +if [[ "${BASE_REF}" != "master" ]] |
| 58 | +then |
| 59 | + # Since we know this is built from a tag or release branch, we can set the VERSION_TAG |
| 60 | + VERSION_TAG="${BASE_REF}" |
| 61 | + # We want the binary version to show up correctly too. |
| 62 | + BINARY_VERSION="${BASE_REF}" |
| 63 | + # Use some bash magic to check if the semver does not end with -sometext, that |
| 64 | + # would indicate a prerelease version. If this is not a prerelease, then we want to set |
| 65 | + # the `latest` tag too. |
| 66 | + if [[ ! "${BASE_REF}" =~ -(.+)$ ]]; |
| 67 | + then |
| 68 | + LATEST=true |
| 69 | + fi |
| 70 | +fi |
| 71 | + |
| 72 | +# First, build the image, with the version info passed in. |
| 73 | +# Note that an image will *always* be built tagged with the GIT_TAG, so we know when it was built. |
| 74 | +docker build --build-arg COMMIT=${BINARY_VERSION} --build-arg TAG=${VERSION_TAG} \ |
| 75 | + -t ${REGISTRY}/admission-server:${GIT_TAG} . |
| 76 | + |
| 77 | +docker push ${REGISTRY}/admission-server:${GIT_TAG} |
| 78 | + |
| 79 | +# Then, we add extra tags if required. |
| 80 | +if [[ $VERSION_TAG != $GIT_TAG ]] |
| 81 | +then |
| 82 | + docker tag ${REGISTRY}/admission-server:${GIT_TAG} ${REGISTRY}/admission-server:${VERSION_TAG} |
| 83 | + docker push ${REGISTRY}/admission-server:${VERSION_TAG} |
| 84 | +fi |
| 85 | + |
| 86 | +if [[ $LATEST == true ]] |
| 87 | +then |
| 88 | + docker tag ${REGISTRY}/admission-server:${GIT_TAG} ${REGISTRY}/admission-server:latest |
| 89 | + docker push ${REGISTRY}/admission-server:latest |
| 90 | +fi |
0 commit comments