BlueGit

timfa chestpreviews

Files

chestpreviews / scripts / release.sh

release.sh

release.sh 6.7 KiB Download Raw
1#!/usr/bin/env bash
2# Release helpers for GitLab CI (ChestPreview).
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.0.10, 1.0.11, 1.1.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 local readme
73
74 sed -i "s/^version = \".*\"/version = \"${version}\"/" build.gradle.kts
75 sed -i "s/^version: .*/version: '${version}'/" src/main/resources/plugin.yml
76 sed -i "1s/.*/# ChestPreview ${version}/" README.md
77 sed -i -E "s/ChestPreview [0-9]+\.[0-9]+\.[0-9]+ enabled/ChestPreview ${version} enabled/" \
78 src/main/java/nl/timfalken/chestpreview/ChestPreviewPlugin.java
79
80 shopt -s nullglob
81 for readme in src/README-*.md; do
82 sed -i "1s/.*/# ChestPreview ${version}/" "$readme"
83 if [[ "$readme" != "src/README-${version}.md" ]]; then
84 git mv "$readme" "src/README-${version}.md"
85 fi
86 done
87 shopt -u nullglob
88
89 echo "Updated project files to version ${version}"
90}
91
92require_push_token() {
93 if [[ -n "${CI_PUSH_TOKEN:-}" ]]; then
94 return 0
95 fi
96
97 cat <<'EOF' >&2
98ERROR: CI_PUSH_TOKEN is not available in this job.
99
100The variable may exist in Settings → CI/CD → Variables but still be hidden here if:
101 1. It is Protected, while this tag/branch is not protected
102 → Protect tag pattern (e.g. *) or uncheck Protected on the variable
103 2. Its Environment scope is not "*"
104 3. The key is misspelled (must be exactly CI_PUSH_TOKEN)
105
106EOF
107 exit 1
108}
109
110bump_and_push() {
111 local version="${1:?version required}"
112 local tag="${2:?tag required}"
113 local token branch
114
115 require_push_token
116 token="${CI_PUSH_TOKEN}"
117 branch="${CI_DEFAULT_BRANCH:?CI_DEFAULT_BRANCH is required}"
118
119 git config user.email "gitlab-ci@${CI_SERVER_HOST:-gitlab.com}"
120 git config user.name "GitLab CI"
121
122 bump_files "$version"
123
124 # Stage only versioned project files (never CI artifacts like version.env).
125 git add -- \
126 build.gradle.kts \
127 src/main/resources/plugin.yml \
128 README.md \
129 src/main/java/nl/timfalken/chestpreview/ChestPreviewPlugin.java
130 git add -A -- src/README-*.md 2>/dev/null || true
131
132 if git diff --cached --quiet; then
133 echo "No version file changes to commit (already at ${version})."
134 else
135 git commit -m "chore: bump version to ${version}"
136 fi
137
138 git remote set-url origin "https://oauth2:${token}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git"
139
140 # Push version bump only. Protected tags cannot be moved/updated.
141 git push origin "HEAD:refs/heads/${branch}" -o ci.skip
142
143 echo "Pushed version bump for ${tag} to ${branch} (ci.skip). Tag left unchanged."
144}
145
146upload_package() {
147 local version="${1:?version required}"
148 local jar="build/libs/ChestPreview-${version}.jar"
149 local package_url
150
151 if [[ ! -f "$jar" ]]; then
152 echo "ERROR: Expected jar not found: ${jar}"
153 ls -la build/libs/ || true
154 exit 1
155 fi
156
157 package_url="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/chestpreviews/${version}/ChestPreview-${version}.jar"
158 curl --fail --show-error --header "JOB-TOKEN: ${CI_JOB_TOKEN}" \
159 --upload-file "$jar" \
160 "$package_url"
161
162 echo "Uploaded ${jar}"
163 echo "PACKAGE_URL=${package_url}" > package.env
164}
165
166# Self-rotate CI_PUSH_TOKEN and write the new value back to the CI/CD variable.
167# Requires scopes: write_repository + self_rotate (or api), and api to update variables.
168# Project access token role should be Maintainer (needed to edit CI/CD variables).
169rotate_push_token() {
170 local old_token expires_at response new_token var_meta protected masked raw scope
171
172 require_push_token
173 old_token="${CI_PUSH_TOKEN}"
174
175 expires_at="$(date -u -d '+365 days' +%Y-%m-%d)"
176 echo "Rotating CI_PUSH_TOKEN (new expiry ${expires_at})..."
177
178 response="$(curl --fail --silent --show-error \
179 --request POST \
180 --header "PRIVATE-TOKEN: ${old_token}" \
181 --header "Content-Type: application/json" \
182 --data "{\"expires_at\":\"${expires_at}\"}" \
183 "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/access_tokens/self/rotate")"
184
185 new_token="$(jq -er '.token' <<<"${response}")"
186 if [[ -z "${new_token}" || "${new_token}" == "null" ]]; then
187 echo "ERROR: Token rotation response did not include a new token."
188 echo "${response}"
189 exit 1
190 fi
191
192 var_meta="$(curl --fail --silent --show-error \
193 --header "PRIVATE-TOKEN: ${new_token}" \
194 "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/variables/CI_PUSH_TOKEN")"
195
196 protected="$(jq -r '.protected' <<<"${var_meta}")"
197 masked="$(jq -r '.masked' <<<"${var_meta}")"
198 raw="$(jq -r '.raw // false' <<<"${var_meta}")"
199 scope="$(jq -r '.environment_scope // "*"' <<<"${var_meta}")"
200
201 curl --fail --silent --show-error \
202 --request PUT \
203 --header "PRIVATE-TOKEN: ${new_token}" \
204 --form "value=${new_token}" \
205 --form "protected=${protected}" \
206 --form "masked=${masked}" \
207 --form "raw=${raw}" \
208 --form "environment_scope=${scope}" \
209 "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/variables/CI_PUSH_TOKEN" \
210 >/dev/null
211
212 echo "CI_PUSH_TOKEN rotated and CI/CD variable updated."
213}
214
215case "${1:-}" in
216 validate) validate ;;
217 bump)
218 # shellcheck disable=SC1091
219 source version.env
220 bump_and_push "$VERSION" "$TAG"
221 ;;
222 upload)
223 # shellcheck disable=SC1091
224 source version.env
225 upload_package "$VERSION"
226 ;;
227 rotate-token) rotate_push_token ;;
228 *)
229 echo "Usage: $0 {validate|bump|upload|rotate-token}"
230 exit 1
231 ;;
232esac