BlueGit

timfa birdcourier

Files

birdcourier / scripts / release.sh

release.sh

Passed 2a19b3da History
release.sh 6.4 KiB Download Raw
1#!/usr/bin/env bash
2# Release helpers for GitLab CI.
3set -euo pipefail
4
5VERSION_REGEX='^v?(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$'
6
7normalize_version() {
8 echo "${1#v}"
9}
10
11is_valid_version_tag() {
12 [[ "$1" =~ $VERSION_REGEX ]]
13}
14
15# True if $1 is strictly greater than $2 (semver X.Y.Z).
16is_newer_version() {
17 local a b
18 a="$(normalize_version "$1")"
19 b="$(normalize_version "$2")"
20 [[ "$a" != "$b" ]] && [[ "$(printf '%s\n' "$a" "$b" | sort -V | tail -n1)" == "$a" ]]
21}
22
23write_version_env() {
24 local version="$1"
25 local tag="$2"
26 {
27 echo "VERSION=${version}"
28 echo "TAG=${tag}"
29 } > version.env
30}
31
32validate() {
33 local tag="${CI_COMMIT_TAG:?CI_COMMIT_TAG is required}"
34 local version newest
35
36 if ! is_valid_version_tag "$tag"; then
37 echo "ERROR: Tag '${tag}' has an invalid format."
38 echo "Expected: v1.7.0, 1.7.10, 1.8.0, ..."
39 exit 1
40 fi
41
42 version="$(normalize_version "$tag")"
43 echo "Validated release tag: ${tag} (version ${version})"
44
45 mapfile -t tags < <(
46 git tag -l \
47 | grep -E "$VERSION_REGEX" \
48 | grep -vxF "$tag" \
49 || true
50 )
51
52 if ((${#tags[@]} == 0)); then
53 echo "No previous release tags found; accepting ${version} as first release."
54 write_version_env "$version" "$tag"
55 return 0
56 fi
57
58 newest="$(printf '%s\n' "${tags[@]}" | sed 's/^v//' | sort -V | tail -n1)"
59 echo "Previous release version: ${newest}"
60
61 if ! is_newer_version "$version" "$newest"; then
62 echo "ERROR: Release version ${version} is not greater than previous release ${newest}."
63 exit 1
64 fi
65
66 write_version_env "$version" "$tag"
67 echo "Version check passed: ${version} > ${newest}"
68}
69
70bump_files() {
71 local version="${1:?version required}"
72
73 sed -i "s/^version = \".*\"/version = \"${version}\"/" build.gradle.kts
74 sed -i -E "s/^version: ['\"]?[0-9]+\.[0-9]+\.[0-9]+['\"]?/version: ${version}/" \
75 src/main/resources/plugin.yml
76 sed -i "1s/.*/# BirdCourier ${version}/" README.md
77 sed -i -E "s/BirdCourier [0-9]+\.[0-9]+\.[0-9]+ enabled/BirdCourier ${version} enabled/" \
78 src/main/java/nl/timfalken/birdcourier/BirdCourierPlugin.java
79
80 echo "Updated project files to version ${version}"
81}
82
83require_push_token() {
84 if [[ -n "${CI_PUSH_TOKEN:-}" ]]; then
85 return 0
86 fi
87
88 cat <<'EOF' >&2
89ERROR: CI_PUSH_TOKEN is not available in this job.
90
91The variable may exist in Settings → CI/CD → Variables but still be hidden here if:
92 1. It is Protected, while this tag/branch is not protected
93 → Protect tag pattern (e.g. *) or uncheck Protected on the variable
94 2. Its Environment scope is not "*"
95 3. The key is misspelled (must be exactly CI_PUSH_TOKEN)
96
97EOF
98 exit 1
99}
100
101bump_and_push() {
102 local version="${1:?version required}"
103 local tag="${2:?tag required}"
104 local token branch
105
106 require_push_token
107 token="${CI_PUSH_TOKEN}"
108 branch="${CI_DEFAULT_BRANCH:?CI_DEFAULT_BRANCH is required}"
109
110 git config user.email "gitlab-ci@${CI_SERVER_HOST:-gitlab.com}"
111 git config user.name "GitLab CI"
112
113 bump_files "$version"
114
115 # Stage only versioned project files (never CI artifacts like version.env).
116 git add -- \
117 build.gradle.kts \
118 src/main/resources/plugin.yml \
119 README.md \
120 src/main/java/nl/timfalken/birdcourier/BirdCourierPlugin.java
121
122 if git diff --cached --quiet; then
123 echo "No version file changes to commit (already at ${version})."
124 else
125 git commit -m "chore: bump version to ${version}"
126 fi
127
128 git remote set-url origin "https://oauth2:${token}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git"
129
130 # Push version bump only. Protected tags cannot be moved/updated.
131 git push origin "HEAD:refs/heads/${branch}" -o ci.skip
132
133 echo "Pushed version bump for ${tag} to ${branch} (ci.skip). Tag left unchanged."
134}
135
136upload_package() {
137 local version="${1:?version required}"
138 local jar="build/libs/BirdCourier-${version}.jar"
139 local package_url
140
141 if [[ ! -f "$jar" ]]; then
142 echo "ERROR: Expected jar not found: ${jar}"
143 ls -la build/libs/ || true
144 exit 1
145 fi
146
147 package_url="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/birdcourier/${version}/BirdCourier-${version}.jar"
148 curl --fail --show-error --header "JOB-TOKEN: ${CI_JOB_TOKEN}" \
149 --upload-file "$jar" \
150 "$package_url"
151
152 echo "Uploaded ${jar}"
153 echo "PACKAGE_URL=${package_url}" > package.env
154}
155
156# Self-rotate CI_PUSH_TOKEN and write the new value back to the CI/CD variable.
157# Requires scopes: write_repository + self_rotate (or api), and api to update variables.
158# Project access token role should be Maintainer (needed to edit CI/CD variables).
159rotate_push_token() {
160 local old_token expires_at response new_token var_meta protected masked raw scope
161
162 require_push_token
163 old_token="${CI_PUSH_TOKEN}"
164
165 expires_at="$(date -u -d '+365 days' +%Y-%m-%d)"
166 echo "Rotating CI_PUSH_TOKEN (new expiry ${expires_at})..."
167
168 response="$(curl --fail --silent --show-error \
169 --request POST \
170 --header "PRIVATE-TOKEN: ${old_token}" \
171 --header "Content-Type: application/json" \
172 --data "{\"expires_at\":\"${expires_at}\"}" \
173 "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/access_tokens/self/rotate")"
174
175 new_token="$(jq -er '.token' <<<"${response}")"
176 if [[ -z "${new_token}" || "${new_token}" == "null" ]]; then
177 echo "ERROR: Token rotation response did not include a new token."
178 echo "${response}"
179 exit 1
180 fi
181
182 var_meta="$(curl --fail --silent --show-error \
183 --header "PRIVATE-TOKEN: ${new_token}" \
184 "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/variables/CI_PUSH_TOKEN")"
185
186 protected="$(jq -r '.protected' <<<"${var_meta}")"
187 masked="$(jq -r '.masked' <<<"${var_meta}")"
188 raw="$(jq -r '.raw // false' <<<"${var_meta}")"
189 scope="$(jq -r '.environment_scope // "*"' <<<"${var_meta}")"
190
191 curl --fail --silent --show-error \
192 --request PUT \
193 --header "PRIVATE-TOKEN: ${new_token}" \
194 --form "value=${new_token}" \
195 --form "protected=${protected}" \
196 --form "masked=${masked}" \
197 --form "raw=${raw}" \
198 --form "environment_scope=${scope}" \
199 "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/variables/CI_PUSH_TOKEN" \
200 >/dev/null
201
202 echo "CI_PUSH_TOKEN rotated and CI/CD variable updated."
203}
204
205case "${1:-}" in
206 validate) validate ;;
207 bump)
208 # shellcheck disable=SC1091
209 source version.env
210 bump_and_push "$VERSION" "$TAG"
211 ;;
212 upload)
213 # shellcheck disable=SC1091
214 source version.env
215 upload_package "$VERSION"
216 ;;
217 rotate-token) rotate_push_token ;;
218 *)
219 echo "Usage: $0 {validate|bump|upload|rotate-token}"
220 exit 1
221 ;;
222esac