Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Commit 22b57c0

Browse files
authored
feat: release script accepts new_version arg (#264)
* feat: release script accepts new_version arg This PR teaches the release.sh script how to deal with version numbers that don't start with a leading 0. This is important since Spanner is now 1.0.0. release.sh now accepts an optional "new_version" argument to let the user specify the desired new version number on the command line rather than computing it from previous tags. This is useful when we want to jump to a non-sequential version number. For example, this would have worked for the Spanner 1.0.0 release we did a few days ago. * PR comments + some refactoring * pr comments
1 parent 8cdfae4 commit 22b57c0

File tree

1 file changed

+77
-30
lines changed

1 file changed

+77
-30
lines changed

release/release.sh

Lines changed: 77 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
#!/bin/bash
22
#
33
# Usage:
4-
# $ release.sh [-f] <organization/project-name>
4+
# $ release.sh [-f] <organization/project-name> [<new-version>]
5+
#
6+
# Args:
7+
# organization/project-name Required. The GitHub repo to release.
8+
#
9+
# new-version Optional. The new version number to use,
10+
# specified as M.N.0. If not specified, the
11+
# new version will be computed from existing
12+
# git tags. This flag should only be needed
13+
# when jumping to a non-sequential version
14+
# number.
515
#
616
# Options:
717
# -f Force; actually make and push the changes
818
# -h Print help message
919
#
10-
# Example:
11-
# # Shows what commands would be run. NO CHANGES ARE PUSHED
12-
# $ release.sh googleapis/google-cloud-cpp-spanner
13-
#
14-
# # Shows commands AND PUSHES changes when -f is specified
15-
# $ release.sh -f googleapis/google-cloud-cpp-spanner
16-
#
1720
# This script creates a "release" on github by doing the following:
18-
# 1. Computes the next version to use
21+
#
22+
# 1. Computes the next version to use, if not specified on the command line
1923
# 2. Creates and pushes the tag w/ the new version
2024
# 3. Creates and pushes a new branch w/ the new version
2125
# 4. Creates the "Pre-Release" in the GitHub UI.
@@ -25,37 +29,65 @@
2529
# happen. Then run this script. After running this script, the user must still
2630
# go to the GH UI where the new release will exist as a "pre-release", and edit
2731
# the release notes.
32+
#
33+
# Examples:
34+
#
35+
# # NO CHANGES ARE PUSHED. Shows what commands would be run.
36+
# $ release.sh googleapis/google-cloud-cpp-spanner
37+
#
38+
# # NO CHANGES ARE PUSHED. Shows what commands would be run.
39+
# $ release.sh googleapis/google-cloud-cpp-spanner 2.0.0
40+
#
41+
# # PUSHES CHANGES to release -spanner
42+
# $ release.sh -f googleapis/google-cloud-cpp-spanner
43+
#
44+
# # PUSHES CHANGES to release -spanner, setting its new version to 2.0.0
45+
# $ release.sh -f googleapis/google-cloud-cpp-spanner 2.0.0
2846

2947
set -eu
3048

3149
# Extracts all the documentation at the top of this file as the usage text.
3250
readonly USAGE="$(sed -n '3,/^$/s/^# \?//p' "$0")"
3351

52+
# Takes an optional list of strings to be printed with a trailing newline and
53+
# exits the program with code 1
54+
function die_with_message() {
55+
for m in "$@"; do
56+
echo "$m" 1>&2
57+
done
58+
exit 1
59+
}
60+
3461
FORCE_FLAG="no"
3562
while getopts "fh" opt "$@"; do
3663
case "$opt" in
3764
[f])
3865
FORCE_FLAG="yes";;
3966
[h])
40-
echo "$USAGE"
67+
echo "${USAGE}"
4168
exit 0;;
4269
*)
43-
echo "$USAGE"
44-
exit 1;;
70+
die_with_message "${USAGE}";;
4571
esac
4672
done
4773
shift $((OPTIND - 1))
4874
declare -r FORCE_FLAG
4975

50-
if [[ $# -ne 1 ]]; then
51-
echo "Missing repo name, example googleapis/google-cloud-cpp-spanner"
52-
echo "$USAGE"
53-
exit 1;
76+
PROJECT_ARG=""
77+
VERSION_ARG=""
78+
if [[ $# -eq 1 ]]; then
79+
PROJECT_ARG="$1"
80+
elif [[ $# -eq 2 ]]; then
81+
PROJECT_ARG="$1"
82+
VERSION_ARG="$2"
83+
else
84+
die_with_message "Invalid arguments" "${USAGE}"
5485
fi
86+
declare -r PROJECT_ARG
87+
declare -r VERSION_ARG
5588

56-
readonly PROJECT="$1"
57-
readonly CLONE_URL="[email protected]:${PROJECT}.git"
58-
readonly TMP_DIR="$(mktemp -d "/tmp/${PROJECT//\//-}-release.XXXXXXXX")"
89+
readonly CLONE_URL="[email protected]:${PROJECT_ARG}.git"
90+
readonly TMP_DIR="$(mktemp -d "/tmp/${PROJECT_ARG//\//-}-release.XXXXXXXX")"
5991
readonly REPO_DIR="${TMP_DIR}/repo"
6092

6193
function banner() {
@@ -87,25 +119,40 @@ trap exit_handler EXIT
87119
# of the release. We also use 'hub' to do the clone so that the user is asked
88120
# to authenticate at the beginning of the process rather than at the end.
89121
if ! command -v hub > /dev/null; then
90-
echo "Can't find 'hub' command"
91-
echo "Maybe run: sudo apt install hub"
92-
echo "Or build it from https://github.com/github/hub"
93-
exit 1
122+
die_with_message \
123+
"Can't find 'hub' command" \
124+
"Maybe run: sudo apt install hub" \
125+
"Or build it from https://github.com/github/hub"
94126
fi
95127

96-
banner "Starting release for ${PROJECT} (${CLONE_URL})"
97-
hub clone "${PROJECT}" "${REPO_DIR}" # May force login to GH at this point
128+
banner "Starting release for ${PROJECT_ARG} (${CLONE_URL})"
129+
hub clone "${PROJECT_ARG}" "${REPO_DIR}" # May force login to GH at this point
98130
cd "${REPO_DIR}"
99131

100132
# Figures out the most recent tagged version, and computes the next version.
101133
readonly TAG="$(git describe --tags --abbrev=0 origin/master)"
102134
readonly CUR_TAG="$(test -n "${TAG}" && echo "${TAG}" || echo "v0.0.0")"
103-
readonly NEW_RELEASE="$(perl -pe 's/v0.(\d+).0/"v0.${\($1+1)}"/e' <<<"${CUR_TAG}")"
104-
readonly NEW_TAG="${NEW_RELEASE}.0"
105-
readonly NEW_BRANCH="${NEW_RELEASE}.x"
135+
readonly CUR_VERSION="${CUR_TAG#v}"
136+
137+
NEW_VERSION=""
138+
if [[ -n "${VERSION_ARG}" ]]; then
139+
NEW_VERSION="${VERSION_ARG}"
140+
else
141+
# No new version specified; compute the new version number
142+
NEW_VERSION="$(perl -pe 's/(\d+).(\d+).(\d+)/"$1.${\($2+1)}.$3"/e' <<<"${CUR_VERSION}")"
143+
fi
144+
declare -r NEW_VERSION
145+
146+
# Avoid handling patch releases for now, because we wouldn't need a new branch
147+
# for those.
148+
if ! grep -P "\d+\.\d+\.0" <<<"${NEW_VERSION}" > /dev/null; then
149+
die_with_message "Sorry, cannot handle patch releases (yet)" "${USAGE}"
150+
fi
151+
152+
readonly NEW_TAG="v${NEW_VERSION}"
153+
readonly NEW_BRANCH="${NEW_TAG%.0}.x"
106154

107-
banner "Release info for ${NEW_RELEASE}"
108-
echo "Current tag: ${CUR_TAG}"
155+
banner "Release info for ${CUR_TAG} -> ${NEW_TAG}"
109156
echo " New tag: ${NEW_TAG}"
110157
echo " New branch: ${NEW_BRANCH}"
111158

0 commit comments

Comments
 (0)