Skip to content
39 changes: 26 additions & 13 deletions .buildkite/commands/checkout-release-branch.sh
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
#!/bin/bash -eu

# Script to checkout a specific release branch.
# Usage: ./checkout-release-branch.sh <RELEASE_VERSION>
# Checks out the `release/*` branch for a given release version.
#
# Usage: checkout-release-branch.sh [<RELEASE_VERSION>]
#
# The release version is taken from the first of these that is set and non-empty:
# 1. the first argument
# 2. the `RELEASE_VERSION` environment variable
# 3. the `release/*` branch the build is running on, via `BUILDKITE_BRANCH`
#
# Buildkite, by default, checks out a specific commit, ending up in a detached HEAD state.
# But in some cases, we need to ensure to be checked out on the `release/*` branch instead, namely:
# - When a `release-pipelines/*.yml` will end up needing to do a `git push` to the `release/*` branch (for version bumps)
# - When doing a new build from a job that was `pipeline upload`'d by such a pipeline,
# to ensure that the job doing the build would include that recent extra commit before starting the build.
# But some release steps need to be on the `release/*` branch instead, namely:
# - when a `release-pipelines/*.yml` needs to `git push` to the `release/*` branch (for version bumps)
# - when a job `pipeline upload`'d by such a pipeline builds from that branch, so that the build
# includes the commit that the version bump just added.

echo "--- :git: Checkout Release Branch"

if [[ -n "${1:-}" ]]; then
RELEASE_VERSION="$1"
elif [[ "${BUILDKITE_BRANCH:-}" =~ ^release/ ]]; then
RELEASE_VERSION="${1:-${RELEASE_VERSION:-}}"

if [[ -z "$RELEASE_VERSION" && "${BUILDKITE_BRANCH:-}" =~ ^release/ ]]; then
RELEASE_VERSION="${BUILDKITE_BRANCH#release/}"
else
echo "Error: RELEASE_VERSION parameter missing and BUILDKITE_BRANCH is not a release branch"
fi

if [[ -z "$RELEASE_VERSION" ]]; then
echo "Error: no release version. Pass it as \$1, set RELEASE_VERSION, or run on a release/* branch." >&2
exit 1
fi
BRANCH_NAME="release/${RELEASE_VERSION}"

BRANCH_NAME="release/${RELEASE_VERSION}"
git fetch origin "$BRANCH_NAME"
git checkout -B "$BRANCH_NAME" "origin/$BRANCH_NAME"
git checkout "$BRANCH_NAME"
# Buildkite can reuse a working copy where "$BRANCH_NAME" was left at a different commit by a previous job,
# so force the local branch to the fetched commit.
# - `reset --hard` rather than `git pull`, to avoid merging if the two diverged
# - `FETCH_HEAD` rather than `origin/$BRANCH_NAME`, which `git fetch <branch>` only updates opportunistically.
git reset --hard FETCH_HEAD
Loading