|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Cut a release tag for the Go sub-module. |
| 4 | +# |
| 5 | +# Usage: |
| 6 | +# script/release <patch|minor|major> |
| 7 | +# script/release <patch|minor|major> --rc |
| 8 | +# |
| 9 | +# The next version is computed from the latest vX.Y.Z tag and the chosen |
| 10 | +# bump. Maintainers run this locally to validate and push an annotated tag. |
| 11 | +# The tag workflow publishes it. |
| 12 | +# |
| 13 | +# Environment: |
| 14 | +# RELEASE_DRY_RUN=1 Compute and print the next version, then stop before |
| 15 | +# tagging or pushing. |
| 16 | +# |
| 17 | +# Requires: git, go, make. |
| 18 | + |
| 19 | +set -euo pipefail |
| 20 | + |
| 21 | +TAG_PREFIX="v" |
| 22 | +STABLE_TAG_RE='^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$' |
| 23 | +TAG_RE='^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-rc\.[1-9][0-9]*)?$' |
| 24 | + |
| 25 | +die() { |
| 26 | + echo "error: $*" >&2 |
| 27 | + exit 1 |
| 28 | +} |
| 29 | + |
| 30 | +# Run from the repository root regardless of the caller's cwd. |
| 31 | +cd "$(dirname "$0")/.." |
| 32 | + |
| 33 | +validate_tag() { |
| 34 | + [[ "$1" =~ $TAG_RE ]] || |
| 35 | + die "invalid release tag '$1'; expected vX.Y.Z or vX.Y.Z-rc.N" |
| 36 | +} |
| 37 | + |
| 38 | +fetch_main() { |
| 39 | + git fetch --quiet --no-tags origin refs/heads/main:refs/remotes/origin/main |
| 40 | +} |
| 41 | + |
| 42 | +require_current_main() { |
| 43 | + local commit="$1" |
| 44 | + local main |
| 45 | + fetch_main |
| 46 | + main="$(git rev-parse refs/remotes/origin/main)" |
| 47 | + [ "$commit" = "$main" ] || |
| 48 | + die "release commit ${commit} is not current main ${main}" |
| 49 | +} |
| 50 | + |
| 51 | +require_clean() { |
| 52 | + if [ -n "$(git status --porcelain)" ]; then |
| 53 | + git --no-pager status --short >&2 |
| 54 | + die "working tree is not clean" |
| 55 | + fi |
| 56 | +} |
| 57 | + |
| 58 | +require_remote_annotated_tag() { |
| 59 | + local tag="$1" |
| 60 | + local commit="$2" |
| 61 | + local remote_commit |
| 62 | + remote_commit="$(git ls-remote --tags origin "refs/tags/${tag}^{}" | awk 'NR == 1 { print $1 }')" |
| 63 | + [ "$remote_commit" = "$commit" ] || |
| 64 | + die "remote tag ${tag} is missing, is not annotated, or does not point to ${commit}" |
| 65 | +} |
| 66 | + |
| 67 | +remote_tags() { |
| 68 | + git ls-remote --tags --refs origin "refs/tags/$1" | |
| 69 | + awk '{ sub("refs/tags/", "", $2); print $2 }' | |
| 70 | + sort -Vr |
| 71 | +} |
| 72 | + |
| 73 | +cut_tag() { |
| 74 | + local bump="$1" |
| 75 | + local release_candidate="$2" |
| 76 | + |
| 77 | + local branch |
| 78 | + branch="$(git symbolic-ref --quiet --short HEAD || true)" |
| 79 | + [ "$branch" = "main" ] || die "releases must be cut from main (got ${branch:-detached HEAD})" |
| 80 | + require_clean |
| 81 | + require_current_main "$(git rev-parse HEAD)" |
| 82 | + |
| 83 | + make fmt-check vet test test-stub |
| 84 | + |
| 85 | + local latest="" t |
| 86 | + while IFS= read -r t; do |
| 87 | + [ -n "$t" ] || continue |
| 88 | + if [[ "$t" =~ $STABLE_TAG_RE ]]; then |
| 89 | + latest="$t" |
| 90 | + break |
| 91 | + fi |
| 92 | + done < <(remote_tags "${TAG_PREFIX}*") |
| 93 | + |
| 94 | + local base |
| 95 | + if [ -z "$latest" ]; then |
| 96 | + base="0.0.0" |
| 97 | + echo "No existing ${TAG_PREFIX}* release tag; basing bump on v0.0.0." |
| 98 | + else |
| 99 | + base="${latest#"$TAG_PREFIX"}" |
| 100 | + fi |
| 101 | + |
| 102 | + local major rest minor patch version tag |
| 103 | + major="${base%%.*}" |
| 104 | + rest="${base#*.}" |
| 105 | + minor="${rest%%.*}" |
| 106 | + patch="${rest#*.}" |
| 107 | + case "$bump" in |
| 108 | + major) major=$((major + 1)); minor=0; patch=0 ;; |
| 109 | + minor) minor=$((minor + 1)); patch=0 ;; |
| 110 | + patch) patch=$((patch + 1)) ;; |
| 111 | + esac |
| 112 | + |
| 113 | + version="v${major}.${minor}.${patch}" |
| 114 | + tag="${TAG_PREFIX}${major}.${minor}.${patch}" |
| 115 | + if [ "$release_candidate" = "1" ]; then |
| 116 | + local candidate rc=0 n |
| 117 | + while IFS= read -r candidate; do |
| 118 | + [[ "$candidate" =~ $TAG_RE ]] || continue |
| 119 | + n="${candidate##*.}" |
| 120 | + [ "$n" -gt "$rc" ] && rc="$n" |
| 121 | + done < <(remote_tags "${tag}-rc.*") |
| 122 | + tag="${tag}-rc.$((rc + 1))" |
| 123 | + fi |
| 124 | + validate_tag "$tag" |
| 125 | + git rev-parse -q --verify "refs/tags/${tag}" >/dev/null && |
| 126 | + die "tag ${tag} already exists" |
| 127 | + |
| 128 | + echo "Cutting ${tag} (module version ${version}) at $(git rev-parse --short HEAD)" |
| 129 | + if [ "${RELEASE_DRY_RUN:-}" = "1" ]; then |
| 130 | + echo "RELEASE_DRY_RUN=1 set; stopping before tag and push." |
| 131 | + return |
| 132 | + fi |
| 133 | + |
| 134 | + git tag -a "$tag" -m "$tag" |
| 135 | + local commit |
| 136 | + commit="$(git rev-parse "${tag}^{commit}")" |
| 137 | + require_current_main "$commit" |
| 138 | + git push origin "refs/tags/${tag}" |
| 139 | + require_remote_annotated_tag "$tag" "$commit" |
| 140 | + echo "Pushed ${tag}; the tag workflow will create the release." |
| 141 | +} |
| 142 | + |
| 143 | +case "${1:-}" in |
| 144 | + patch | minor | major) |
| 145 | + if [ "$#" -eq 1 ]; then |
| 146 | + cut_tag "$1" 0 |
| 147 | + elif [ "$#" -eq 2 ] && [ "$2" = "--rc" ]; then |
| 148 | + cut_tag "$1" 1 |
| 149 | + else |
| 150 | + die "usage: script/release <patch|minor|major> [--rc]" |
| 151 | + fi |
| 152 | + ;; |
| 153 | + *) |
| 154 | + die "usage: script/release <patch|minor|major> [--rc]" |
| 155 | + ;; |
| 156 | +esac |
0 commit comments