| 1 |
#!/bin/bash |
| 2 |
# Copyright 2022 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 |
|
| 17 |
# This script performs a source release on GitHub for a given repo, that is: |
| 18 |
# - Creates a release branch (if it does not yet exist), |
| 19 |
# - Creates a release tag. |
| 20 |
set -eo pipefail |
| 21 |
|
| 22 |
# Parameters and arguments. These will be populated/modified by args parsing. |
| 23 |
|
| 24 |
# Options. |
| 25 |
# Whether to actually create a release. This is false by default and meant to |
| 26 |
# prevent accidental releases. |
| 27 |
DO_RUN_ACTION="false" |
| 28 |
# Commit at which to make the release. If unspecified, the release is made from |
| 29 |
# HEAD. |
| 30 |
COMMIT_HASH= |
| 31 |
# Optional personal access token. |
| 32 |
ACCESS_TOKEN= |
| 33 |
|
| 34 |
# Arguments. |
| 35 |
# Action to be performed. |
| 36 |
ACTION= |
| 37 |
# This must be of the form `MAJOR.MINOR.PATCH`. |
| 38 |
VERSION= |
| 39 |
# Repo name after github.com/tink-crypto/, e.g., tink-cc. |
| 40 |
REPO_NAME= |
| 41 |
|
| 42 |
# Derived variables. |
| 43 |
GITHUB_REPO_URL= |
| 44 |
RELEASE_BRANCH= |
| 45 |
TAG= |
| 46 |
GITHUB_REFS= |
| 47 |
BRANCH_EXISTS="false" |
| 48 |
|
| 49 |
# Constants. |
| 50 |
readonly GITHUB_ORG_URL="github.com/tink-crypto" |
| 51 |
|
| 52 |
usage() { |
| 53 |
echo "Usage: $0 [-rh] [-c <commit hash>] [-t <access token>] <action> \\" |
| 54 |
echo " <version> <repository>" |
| 55 |
echo " <action>: The action to be performed (crete_branch|create_tag)." |
| 56 |
echo " <version>: The version identifier in MAJOR.MINOR.PATCH format." |
| 57 |
echo " <repository>: The name of the repository (e.g. \"tink-cc\")." |
| 58 |
echo " -c: Commit hash to use as HEAD of the release branch (optional)." |
| 59 |
echo " -t: Access token. Without this, the default is SSH (optional)." |
| 60 |
echo " -r: Whether to actually create a release; this is false by default." |
| 61 |
echo " -h: Show this help message." |
| 62 |
exit 1 |
| 63 |
} |
| 64 |
|
| 65 |
process_params() { |
| 66 |
while getopts "rhc:t:" opt; do |
| 67 |
case "${opt}" in |
| 68 |
r) DO_RUN_ACTION="true" ;; |
| 69 |
c) COMMIT_HASH="${OPTARG}" ;; |
| 70 |
t) ACCESS_TOKEN="${OPTARG}" ;; |
| 71 |
*) usage ;; |
| 72 |
esac |
| 73 |
done |
| 74 |
shift $((OPTIND - 1)) |
| 75 |
readonly DO_RUN_ACTION |
| 76 |
readonly COMMIT_HASH |
| 77 |
readonly ACCESS_TOKEN |
| 78 |
|
| 79 |
ACTION="$1" |
| 80 |
if [[ ! "${ACTION}" =~ create_branch|create_tag ]]; then |
| 81 |
echo "ERROR: Expected (create_branch|create_tag) got ${ACTION}" >&2 |
| 82 |
usage |
| 83 |
fi |
| 84 |
readonly ACTION |
| 85 |
|
| 86 |
VERSION="$2" |
| 87 |
if [[ ! "${VERSION}" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9a-z]*)?$ ]]; then |
| 88 |
echo "ERROR: Invalid version format: expected MAJOR.MINOR.PATCH or MAJOR.MINOR.PATCH-pre, got \${VERSION}" >&2 |
| 89 |
usage |
| 90 |
fi |
| 91 |
readonly VERSION |
| 92 |
|
| 93 |
REPO_NAME="$3" |
| 94 |
if [[ -z "${REPO_NAME}" ]]; then |
| 95 |
echo "ERROR: Repo name must be specified." >&2 |
| 96 |
usage |
| 97 |
fi |
| 98 |
readonly REPO_NAME |
| 99 |
|
| 100 |
# Use SSH by default. |
| 101 |
local protocol_and_credentials="ssh://git" |
| 102 |
if [[ -n "${ACCESS_TOKEN}" ]]; then |
| 103 |
protocol_and_credentials="https://ise-crypto:${ACCESS_TOKEN}" |
| 104 |
fi |
| 105 |
readonly protocol_and_credentials |
| 106 |
GITHUB_REPO_URL="${protocol_and_credentials}@${GITHUB_ORG_URL}/${REPO_NAME}" |
| 107 |
readonly GITHUB_REPO_URL |
| 108 |
|
| 109 |
# Release branch is only MAJOR.MINOR. |
| 110 |
readonly RELEASE_BRANCH="$(echo "${VERSION}" | cut -d'.' -f1,2)" |
| 111 |
|
| 112 |
# Splitting declaration and assignment guarantees correct propagation of the |
| 113 |
# exit code of the subshell. |
| 114 |
local GITHUB_REFS |
| 115 |
GITHUB_REFS="$(git ls-remote "${GITHUB_REPO_URL}")" |
| 116 |
readonly GITHUB_REFS |
| 117 |
|
| 118 |
local -r expected_release_branch="refs/heads/${RELEASE_BRANCH}" |
| 119 |
if echo "${GITHUB_REFS}" | grep "${expected_release_branch}" > /dev/null; then |
| 120 |
BRANCH_EXISTS="true" |
| 121 |
fi |
| 122 |
readonly BRANCH_EXISTS |
| 123 |
|
| 124 |
if [[ "${ACTION}" == "create_tag" ]]; then |
| 125 |
if [[ "${BRANCH_EXISTS}" == "false" ]]; then |
| 126 |
echo "ERROR: The release branch does not exist in \ |
| 127 |
${GITHUB_ORG_URL}/${REPO_NAME}." >&2 |
| 128 |
return 1 |
| 129 |
fi |
| 130 |
local -r release_tag="v${VERSION}" |
| 131 |
local -r expected_release_tag="refs/tags/${release_tag}" |
| 132 |
if echo "${GITHUB_REFS}" | grep "${expected_release_tag}" > /dev/null; then |
| 133 |
echo "ERROR The tag \"${release_tag}\" already exists in \ |
| 134 |
${GITHUB_ORG_URL}/${REPO_NAME}." >&2 |
| 135 |
return 1 |
| 136 |
fi |
| 137 |
|
| 138 |
fi |
| 139 |
} |
| 140 |
|
| 141 |
####################################### |
| 142 |
# Prints a command |
| 143 |
# |
| 144 |
# Args: |
| 145 |
# Command to execute. |
| 146 |
# |
| 147 |
####################################### |
| 148 |
print_command() { |
| 149 |
printf '%q ' '+' "$@" |
| 150 |
echo |
| 151 |
} |
| 152 |
|
| 153 |
####################################### |
| 154 |
# Runs a command if DO_RUN_ACTION is true. |
| 155 |
# |
| 156 |
# Args: |
| 157 |
# Command to execute. |
| 158 |
# Globals: |
| 159 |
# DO_RUN_ACTION |
| 160 |
# |
| 161 |
####################################### |
| 162 |
run_command() { |
| 163 |
if [[ "${DO_RUN_ACTION}" == "false" ]]; then |
| 164 |
echo " *** Dry run, command not executed. ***" |
| 165 |
return 0 |
| 166 |
fi |
| 167 |
# Actually run the command. |
| 168 |
"$@" |
| 169 |
return $? |
| 170 |
} |
| 171 |
|
| 172 |
####################################### |
| 173 |
# Prints and runs a command. |
| 174 |
# |
| 175 |
# Args: |
| 176 |
# Command to execute. |
| 177 |
# |
| 178 |
####################################### |
| 179 |
print_and_run_command() { |
| 180 |
print_command "$@" |
| 181 |
run_command "$@" |
| 182 |
} |
| 183 |
|
| 184 |
####################################### |
| 185 |
# Creates and checks out to the release branch. |
| 186 |
# |
| 187 |
# If COMMIT_HASH is specified, use COMMIT_HASH as HEAD for the branch. |
| 188 |
# |
| 189 |
# Globals: |
| 190 |
# RELEASE_BRANCH |
| 191 |
# COMMIT_HASH |
| 192 |
# |
| 193 |
####################################### |
| 194 |
git_create_release_branch() { |
| 195 |
if [[ "${BRANCH_EXISTS}" == "true" ]]; then |
| 196 |
echo "WARNING: The release branch already exists. Nothing to do." |
| 197 |
return 0 |
| 198 |
fi |
| 199 |
# Target branch does not exist so we create the release branch. |
| 200 |
if [[ -n "${COMMIT_HASH:-}" ]]; then |
| 201 |
# Use COMMIT_HASH as HEAD for this branch. |
| 202 |
print_and_run_command git branch "${RELEASE_BRANCH}" "${COMMIT_HASH}" |
| 203 |
else |
| 204 |
print_and_run_command git branch "${RELEASE_BRANCH}" |
| 205 |
fi |
| 206 |
print_and_run_command git push origin "${RELEASE_BRANCH}" |
| 207 |
} |
| 208 |
|
| 209 |
####################################### |
| 210 |
# Creates a release tag. |
| 211 |
# |
| 212 |
# Globals: |
| 213 |
# RELEASE_BRANCH |
| 214 |
# REPO_NAME |
| 215 |
# VERSION |
| 216 |
# |
| 217 |
####################################### |
| 218 |
git_create_release_tag() { |
| 219 |
if [[ "${BRANCH_EXISTS}" == "false" ]]; then |
| 220 |
echo "ERROR: The release branch does not exist in \ |
| 221 |
${GITHUB_ORG_URL}/${REPO_NAME}." >&2 |
| 222 |
return 1 |
| 223 |
fi |
| 224 |
local -r release_tag="v${VERSION}" |
| 225 |
local -r expected_release_tag="refs/tags/${release_tag}" |
| 226 |
if echo "${GITHUB_REFS}" | grep "${expected_release_tag}" > /dev/null; then |
| 227 |
echo "ERROR The tag \"${release_tag}\" already exists in \ |
| 228 |
${GITHUB_ORG_URL}/${REPO_NAME}." >&2 |
| 229 |
return 1 |
| 230 |
fi |
| 231 |
print_and_run_command git checkout "${RELEASE_BRANCH}" |
| 232 |
print_and_run_command git tag -a "${release_tag}" \ |
| 233 |
-m "${REPO_NAME} version ${VERSION}" |
| 234 |
print_and_run_command git push origin "${release_tag}" |
| 235 |
} |
| 236 |
|
| 237 |
main() { |
| 238 |
process_params "$@" |
| 239 |
# Avoid logging the full URL; replace GIT_URL with a version that omits user |
| 240 |
# and access token. |
| 241 |
local -r protocol="$(echo "${GITHUB_REPO_URL}" | cut -d':' -f1)" |
| 242 |
local -r github_repo="$(echo "${GITHUB_REPO_URL}" | cut -d'@' -f2)" |
| 243 |
print_command git clone "${protocol}://...@${github_repo}" |
| 244 |
run_command git clone "${GITHUB_REPO_URL}" |
| 245 |
print_and_run_command cd "${REPO_NAME}" |
| 246 |
|
| 247 |
case "${ACTION}" in |
| 248 |
create_branch) git_create_release_branch ;; |
| 249 |
create_tag) git_create_release_tag ;; |
| 250 |
esac |
| 251 |
} |
| 252 |
|
| 253 |
main "$@" |