|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# ============================================================================ |
| 4 | +# release-modules.sh |
| 5 | +# ============================================================================ |
| 6 | +# Releases several modules of a multi-module repository in succession. |
| 7 | +# |
| 8 | +# For each module, in the order given: runs release-version.sh without |
| 9 | +# pushing, pushes only the new release tag, waits for CI to build and deploy |
| 10 | +# that tag, and waits for the release to appear on Maven Central. The |
| 11 | +# "Bump ... to next development cycle" commits stay local until every module |
| 12 | +# is released, then the branch is pushed once, so that CI builds the branch |
| 13 | +# only after all modules are released and consistent. |
| 14 | +# |
| 15 | +# Modules whose release tag already exists on the remote are not released |
| 16 | +# again, but their CI and Maven Central checks still happen, so a failed run |
| 17 | +# can be resumed by repeating the same command. |
| 18 | +# |
| 19 | +# Authors: Curtis Rueden |
| 20 | +# ============================================================================ |
| 21 | + |
| 22 | +# -- Avoid localized output that might confuse the script -- |
| 23 | + |
| 24 | +export LC_ALL=C |
| 25 | + |
| 26 | +# -- Constants -- |
| 27 | + |
| 28 | +CENTRAL=https://repo1.maven.org/maven2 |
| 29 | +CI_APPEAR_TIMEOUT=600 |
| 30 | +CENTRAL_POLL_INTERVAL=120 |
| 31 | +CENTRAL_TIMEOUT=7200 |
| 32 | + |
| 33 | +# -- Functions -- |
| 34 | + |
| 35 | +log() { |
| 36 | + echo "[release-modules] $(date '+%H:%M:%S') $*" |
| 37 | +} |
| 38 | + |
| 39 | +die() { |
| 40 | + echo "[release-modules] FAILED: $*" >&2 |
| 41 | + test "$released" && |
| 42 | + echo "[release-modules] Note: Bump commits for $released are not yet pushed. |
| 43 | +Repeat the same command to resume, or push them with: git push $remote HEAD" >&2 |
| 44 | + exit 1 |
| 45 | +} |
| 46 | + |
| 47 | +# Prints the given POM expression for the project in the current directory. |
| 48 | +pom_eval() { |
| 49 | + mvn -B -q -N help:evaluate -Dexpression="$1" -DforceStdout 2>/dev/null |
| 50 | +} |
| 51 | + |
| 52 | +# Waits for the CI run of the given tag to complete successfully, rerunning |
| 53 | +# its failed jobs after a delay if they failed due to Maven Central throttling. |
| 54 | +wait_for_ci() { |
| 55 | + log "Waiting for CI run on $1" |
| 56 | + run= |
| 57 | + elapsed=0 |
| 58 | + while test -z "$run" |
| 59 | + do |
| 60 | + run=$(gh run list --branch "$1" --event push --limit 1 \ |
| 61 | + --json databaseId -q '.[0].databaseId' 2>/dev/null) |
| 62 | + test "$run" && break |
| 63 | + test "$elapsed" -lt "$CI_APPEAR_TIMEOUT" || die "No CI run found for $1" |
| 64 | + sleep 10 |
| 65 | + elapsed=$((elapsed + 10)) |
| 66 | + done |
| 67 | + retries=0 |
| 68 | + while : |
| 69 | + do |
| 70 | + log "Watching CI run $run" |
| 71 | + gh run watch "$run" --exit-status --interval 60 >/dev/null && break |
| 72 | + gh run view "$run" --log-failed 2>/dev/null | grep -q 'status code: 429' || |
| 73 | + die "CI run $run failed for $1 (see: gh run view $run --log-failed)" |
| 74 | + test "$retries" -lt "$MAX_RETRIES" || |
| 75 | + die "CI run $run for $1 was throttled by Maven Central $((retries + 1)) times" |
| 76 | + retries=$((retries + 1)) |
| 77 | + log "CI run $run was throttled by Maven Central; rerunning in $RETRY_DELAY seconds" |
| 78 | + sleep "$RETRY_DELAY" |
| 79 | + gh run rerun "$run" --failed || die "Could not rerun CI run $run" |
| 80 | + # Note: Give the rerun time to register, so that we do not watch |
| 81 | + # the previous attempt, which has already completed. |
| 82 | + sleep 30 |
| 83 | + done |
| 84 | + log "CI succeeded for $1" |
| 85 | +} |
| 86 | + |
| 87 | +# Waits for the given G:A:V to appear on Maven Central. |
| 88 | +wait_for_central() { |
| 89 | + url="$CENTRAL/$(echo "$1" | tr . /)/$2/$3/$2-$3.pom" |
| 90 | + log "Waiting for $1:$2:$3 on Maven Central" |
| 91 | + elapsed=0 |
| 92 | + until curl -fsI "$url" >/dev/null |
| 93 | + do |
| 94 | + test "$elapsed" -lt "$CENTRAL_TIMEOUT" || |
| 95 | + die "$1:$2:$3 did not appear on Maven Central after $CENTRAL_TIMEOUT seconds" |
| 96 | + sleep "$CENTRAL_POLL_INTERVAL" |
| 97 | + elapsed=$((elapsed + CENTRAL_POLL_INTERVAL)) |
| 98 | + done |
| 99 | + log "$1:$2:$3 is on Maven Central" |
| 100 | +} |
| 101 | + |
| 102 | +# -- Parse command line -- |
| 103 | + |
| 104 | +DELAY=600 |
| 105 | +RETRY_DELAY=1800 |
| 106 | +MAX_RETRIES=2 |
| 107 | +SKIP_PUSH= |
| 108 | +USAGE= |
| 109 | +modules= |
| 110 | +while test $# -gt 0 |
| 111 | +do |
| 112 | + case "$1" in |
| 113 | + --delay=*) DELAY=${1#--*=};; |
| 114 | + --retry-delay=*) RETRY_DELAY=${1#--*=};; |
| 115 | + --max-retries=*) MAX_RETRIES=${1#--*=};; |
| 116 | + --skip-push) SKIP_PUSH=t;; |
| 117 | + --help) USAGE=t; break;; |
| 118 | + -*) echo "Unknown option: $1" >&2; USAGE=t; break;; |
| 119 | + *) modules="$modules $1";; |
| 120 | + esac |
| 121 | + shift |
| 122 | +done |
| 123 | + |
| 124 | +test -z "$modules" && USAGE=t |
| 125 | +test "$USAGE" && { |
| 126 | + echo "Usage: $0 [options] <module>=<version> [<module>=<version> ...] |
| 127 | +
|
| 128 | +Where each <module> is a module directory of the multi-module repository |
| 129 | +containing the current directory, or . for the root (aggregator) project, |
| 130 | +and <version> is the version to release. The version is required, so that |
| 131 | +repeating the same command after a failure resumes rather than releasing |
| 132 | +already released modules again. |
| 133 | +
|
| 134 | +Options include: |
| 135 | + --delay=<seconds> - Wait between modules, to spare Maven Central |
| 136 | + (default: 600). |
| 137 | + --retry-delay=<seconds> - Wait before rerunning CI that was throttled by |
| 138 | + Maven Central (default: 1800). |
| 139 | + --max-retries=<n> - Rerun throttled CI at most this many times per |
| 140 | + module (default: 2). |
| 141 | + --skip-push - Do not push the branch after the last module. |
| 142 | +" >&2 |
| 143 | + exit 1 |
| 144 | +} |
| 145 | + |
| 146 | +# -- Sanity checks -- |
| 147 | + |
| 148 | +command -v gh >/dev/null || die "The gh command is required." |
| 149 | +rootDir=$(git rev-parse --show-toplevel) || die "Not in a git repository." |
| 150 | +cd "$rootDir" || die "Could not change to $rootDir" |
| 151 | +upstreamBranch=$(git rev-parse --abbrev-ref --symbolic-full-name '@{u}') || |
| 152 | + die "The current branch has no upstream branch." |
| 153 | +remote=${upstreamBranch%/*} |
| 154 | +releaseVersion="$(cd "$(dirname "$0")" && pwd)/release-version.sh" |
| 155 | +test -x "$releaseVersion" || die "Missing helper script at '$releaseVersion'" |
| 156 | +for spec in $modules |
| 157 | +do |
| 158 | + case "$spec" in |
| 159 | + *=?*) ;; |
| 160 | + *) die "No version given for module '$spec'" ;; |
| 161 | + esac |
| 162 | + test -f "${spec%%=*}/pom.xml" || die "No pom.xml in module '${spec%%=*}'" |
| 163 | +done |
| 164 | + |
| 165 | +# -- Release the modules -- |
| 166 | + |
| 167 | +released= |
| 168 | +first=t |
| 169 | +for spec in $modules |
| 170 | +do |
| 171 | + module=${spec%%=*} |
| 172 | + version=${spec#*=} |
| 173 | + |
| 174 | + cd "$rootDir/$module" || die "Could not change to module '$module'" |
| 175 | + groupId=$(pom_eval project.groupId) |
| 176 | + artifactId=$(pom_eval project.artifactId) |
| 177 | + test "$groupId" -a "$artifactId" || die "Could not read coordinates of module '$module'" |
| 178 | + tag=$artifactId-$version |
| 179 | + |
| 180 | + if git ls-remote --exit-code --tags "$remote" "refs/tags/$tag" >/dev/null |
| 181 | + then |
| 182 | + log "$tag already exists on $remote; skipping its release" |
| 183 | + else |
| 184 | + test "$first" || { |
| 185 | + log "Waiting $DELAY seconds before the next release" |
| 186 | + sleep "$DELAY" |
| 187 | + } |
| 188 | + if git rev-parse -q --verify "refs/tags/$tag" >/dev/null |
| 189 | + then |
| 190 | + log "$tag already exists locally; pushing it" |
| 191 | + else |
| 192 | + log "=== Releasing $artifactId $version ===" |
| 193 | + "$releaseVersion" --skip-push "$version" </dev/null || |
| 194 | + die "release-version.sh failed for $artifactId $version" |
| 195 | + git rev-parse -q --verify "refs/tags/$tag" >/dev/null || |
| 196 | + die "release-version.sh did not create tag $tag" |
| 197 | + released="$released $artifactId" |
| 198 | + fi |
| 199 | + git push "$remote" "refs/tags/$tag" || die "Could not push tag $tag" |
| 200 | + |
| 201 | + # Note: Later modules depending on this one need its new SNAPSHOT |
| 202 | + # version to be resolvable, since release-version.sh reads their |
| 203 | + # project details before pinning their sibling dependencies to |
| 204 | + # releases. And the enforcer rejects SNAPSHOT dependencies, which |
| 205 | + # do not matter for this local install. |
| 206 | + log "Installing next SNAPSHOT of $artifactId locally" |
| 207 | + mvn -B -q -N install -DskipTests -Denforcer.skip || |
| 208 | + die "Local install of the next SNAPSHOT of $artifactId failed" |
| 209 | + fi |
| 210 | + first= |
| 211 | + |
| 212 | + wait_for_ci "$tag" |
| 213 | + wait_for_central "$groupId" "$artifactId" "$version" |
| 214 | +done |
| 215 | + |
| 216 | +# -- Push the branch -- |
| 217 | + |
| 218 | +cd "$rootDir" || die "Could not change to $rootDir" |
| 219 | +if test "$SKIP_PUSH" |
| 220 | +then |
| 221 | + log "All modules released; skipping push of the branch, as requested." |
| 222 | +else |
| 223 | + log "All modules released; pushing the branch" |
| 224 | + git push "$remote" HEAD || die "Could not push the branch" |
| 225 | +fi |
0 commit comments