velero/hack/docker-push.sh

102 lines
3.2 KiB
Bash
Raw Normal View History

#!/bin/bash
# Copyright 2020 the Velero contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# docker-push is invoked by the CI/CD system to deploy docker images to Docker Hub.
# It will build images for all commits to main and all git tags.
# The highest, non-prerelease semantic version will also be given the `latest` tag.
set +x
if [[ -z "$CI" ]]; then
echo "This script is intended to be run only on Github Actions." >&2
exit 1
fi
# Return value is written into HIGHEST
HIGHEST=""
function highest_release() {
# Loop through the tags since pre-release versions come before the actual versions.
# Iterate til we find the first non-pre-release
# This is not necessarily the most recently made tag; instead, we want it to be the highest semantic version.
# The most recent tag could potentially be a lower semantic version, made as a point release for a previous series.
# As an example, if v1.3.0 exists and we create v1.2.2, v1.3.0 should still be `latest`.
# `git describe --tags $(git rev-list --tags --max-count=1)` would return the most recently made tag.
for t in $(git tag -l --sort=-v:refname);
do
# If the tag has alpha, beta or rc in it, it's not "latest"
if [[ "$t" == *"beta"* || "$t" == *"alpha"* || "$t" == *"rc"* ]]; then
continue
fi
HIGHEST="$t"
break
done
}
triggeredBy=$(echo $GITHUB_REF | cut -d / -f 2)
if [[ "$triggeredBy" == "heads" ]]; then
BRANCH=$(echo $GITHUB_REF | cut -d / -f 3)
TAG=
elif [[ "$triggeredBy" == "tags" ]]; then
BRANCH=
TAG=$(echo $GITHUB_REF | cut -d / -f 3)
fi
if [[ "$BRANCH" == "main" ]]; then
VERSION="$BRANCH"
elif [[ ! -z "$TAG" ]]; then
# Explicitly checkout tags when building from a git tag.
# This is not needed when building from main
git fetch --tags
# Calculate the latest release if there's a tag.
highest_release
VERSION="$TAG"
else
echo "We're not on main and we're not building a tag, exit early."
exit 0
fi
# Assume we're not tagging `latest` by default, and never on main.
TAG_LATEST=false
if [[ "$BRANCH" == "main" ]]; then
echo "Building main, not tagging latest."
elif [[ "$TAG" == "$HIGHEST" ]]; then
TAG_LATEST=true
fi
if [[ -z "$BUILDX_PLATFORMS" ]]; then
BUILDX_PLATFORMS="linux/amd64,linux/arm64,linux/arm/v7,linux/ppc64le"
fi
# Debugging info
echo "Highest tag found: $HIGHEST"
echo "BRANCH: $BRANCH"
echo "TAG: $TAG"
echo "TAG_LATEST: $TAG_LATEST"
echo "BUILDX_PLATFORMS: $BUILDX_PLATFORMS"
echo "Building and pushing container images."
Allow registry to be configured at build time This adds a new `buildinfo` variable `ImageRegistry` that can set at build time like the `Version` variable. This allows us to customise the Velero binary to use different registries. If the variable is set, this variable wille be used when creating the URIs for both the main `velero` and `velero-restic-restore-helper` images. If it is not set, default to using Dockerhub (`velero/velero`, `velero/velero-restic-restore-helper`). There are numerous ways in which the Velero binary can be built so all of them have been updated to add the new link time flag to set the variable: * `make local` (used for local developer builds to build for the local OS and ARCH) * `make build` (used by developers and also VMware internal builds to build a specific OS and ARCH) * Goreleaser config (used when creating OSS release binaries) * Dockerfile (used to build the Velero binary used within the image) All of these workflows are currently triggered from our Makefile where the variable `REGISTRY` is already available with the default value of `velero` and used to build the image tag. Where the new `ImageRegistry` build variable is needed, we pass through this Makefile variable to those tasks so it can be used accordingly. The GitHub action and the `./hack/docker-push.sh` script used to push container images has not been modified. This will continue to use the default registry specified in the Makefile and will not explicitly pass it in. Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
2021-07-06 20:14:46 +00:00
# The use of "registry" as the buildx output type below instructs
# Docker to push the image
VERSION="$VERSION" \
TAG_LATEST="$TAG_LATEST" \
BUILDX_PLATFORMS="$BUILDX_PLATFORMS" \
BUILDX_OUTPUT_TYPE="registry" \
make all-containers