Skip to content

Commit b545f36

Browse files
committed
fix: introduce a script for updating the version for the hotfix branch
1 parent f7a4428 commit b545f36

2 files changed

Lines changed: 133 additions & 0 deletions

File tree

.github/workflows/pr-precommit.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ on:
2626
- main
2727
- develop
2828
- release-candidate
29+
- hotfix-*
2930

3031
jobs:
3132
pre-commit:

tools/hotfix-version-update.sh

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#!/bin/bash
2+
# Copyright 2025 "Google LLC"
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# Example usage:
17+
#
18+
# bash hotfix-version-update.sh -b hotfix-branch-name
19+
#
20+
21+
set -e -o pipefail
22+
23+
usage() {
24+
echo "Usage: $0 -b <hotfix-branch-name>"
25+
echo " -b: The name of the hotfix branch (must start with 'hotfix-' and be based on main)"
26+
exit 1
27+
}
28+
29+
if ! type -P git 1>/dev/null; then
30+
echo "Must install git!"
31+
exit 1
32+
fi
33+
34+
if ! type -P git-sed 1>/dev/null; then
35+
echo "Must install git-extras package for git sed functionality"
36+
exit 1
37+
fi
38+
39+
if ! type -P gh 1>/dev/null; then
40+
echo "Must install GitHub CLI tool for command line API access"
41+
exit 1
42+
fi
43+
44+
if ! type -P jq 1>/dev/null; then
45+
echo "Must install jq for JSON parsing"
46+
exit 1
47+
fi
48+
49+
if ! gh auth status; then
50+
echo 'Must authenticate using "gh auth login"'
51+
exit 1
52+
fi
53+
54+
GITDIR=$(mktemp -d)
55+
trap 'rm -rf ${GITDIR}' EXIT
56+
57+
BRANCH_NAME=""
58+
59+
while getopts "b:" opt; do
60+
case "${opt}" in
61+
b) BRANCH_NAME="${OPTARG}" ;;
62+
*) usage ;;
63+
esac
64+
done
65+
66+
if [ -z "$BRANCH_NAME" ]; then
67+
usage
68+
fi
69+
70+
if [[ ! "$BRANCH_NAME" =~ ^hotfix- ]]; then
71+
echo "Branch name for hotfix process should start with hotfix-"
72+
exit 1
73+
fi
74+
75+
# Check if the branch exist and shares a history with main
76+
# If the API returns a non-zero exit code, or the response is empty,
77+
# it usually means the branch doesn't exist or is an orphan branch (no common history).
78+
API_RESPONSE=$(gh api "repos/GoogleCloudPlatform/hpc-toolkit/compare/main...$BRANCH_NAME")
79+
EXIT_CODE=$?
80+
81+
if [ $EXIT_CODE -ne 0 ] || [ -z "$API_RESPONSE" ]; then
82+
echo "hotfix branch $BRANCH_NAME should be branched out of main"
83+
exit 1
84+
fi
85+
86+
BEHIND_BY=$(echo "$API_RESPONSE" | jq -r .behind_by)
87+
88+
# Check if the branch is behind main
89+
# If 'behind_by' is greater than 0, main has moved forward since the branch was created.
90+
if [ "$BEHIND_BY" -gt 0 ]; then
91+
echo "hotfix branch $BRANCH_NAME is behind main"
92+
exit 1
93+
fi
94+
95+
OLD_TAG=$(gh release list -R GoogleCloudPlatform/hpc-toolkit -L 1 --json tagName --jq '.[] | .tagName')
96+
OLD_MAJOR=$(echo "${OLD_TAG}" | cut -f1 -d. | sed 's,v,,')
97+
OLD_MINOR=$(echo "${OLD_TAG}" | cut -f2 -d.)
98+
OLD_PATCH=$(echo "${OLD_TAG}" | cut -f3 -d.)
99+
100+
NEW_MAJOR="${OLD_MAJOR}"
101+
NEW_MINOR="${OLD_MINOR}"
102+
NEW_PATCH=$((OLD_PATCH + 1))
103+
104+
NEW_VERSION="${NEW_MAJOR}.${NEW_MINOR}.${NEW_PATCH}"
105+
NEW_TAG="v${NEW_VERSION}"
106+
107+
V_BRANCH="version/${NEW_TAG}"
108+
REMOTE_NAME=origin
109+
110+
gh repo clone GoogleCloudPlatform/hpc-toolkit "${GITDIR}" -- --single-branch --branch "${BRANCH_NAME}" --depth 1 --origin "${REMOTE_NAME}"
111+
cd "${GITDIR}"
112+
git switch -c "${V_BRANCH}" "${BRANCH_NAME}"
113+
echo "Creating new Toolkit version branch"
114+
echo "converting old v${OLD_MAJOR}.${OLD_MINOR}.${OLD_PATCH} to new ${NEW_TAG}"
115+
git sed "v${OLD_MAJOR}\.${OLD_MINOR}\.${OLD_PATCH}" "${NEW_TAG}" -- **/*.go **/versions.tf
116+
git add -u
117+
echo "Creating new branch with version update to ${NEW_VERSION}"
118+
git commit -m "Increase version to ${NEW_VERSION}"
119+
git push -u "${REMOTE_NAME}" "${V_BRANCH}"
120+
echo "Opening pull request to update ${BRANCH_NAME} to version ${NEW_VERSION}"
121+
gh pr create --base "${BRANCH_NAME}" --head "${V_BRANCH}" \
122+
--label "release-chore" \
123+
--title "Update Toolkit release to ${NEW_TAG}" \
124+
--body "Update hotfix ${BRANCH_NAME} to ${NEW_VERSION}"
125+
echo
126+
echo
127+
echo
128+
echo
129+
echo
130+
echo "Consider running the test babysitter using the pull request number from above:"
131+
echo
132+
echo "tools/cloud-build/babysit/run --pr <PR_NUM> --all -c 1"

0 commit comments

Comments
 (0)