BlueGit

timfa copper-key

Files

copper-key / scripts / release.sh

release.sh

release.sh 7.0 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/.*/# CopperKeys ${version}/" README.md
77 sed -i -E "s/CopperKeys [0-9]+\.[0-9]+\.[0-9]+ enabled/CopperKeys ${version} enabled/" \
78 src/main/java/nl/timfalken/copperkeys/CopperKeysPlugin.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/copperkeys/CopperKeysPlugin.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_file() {
137 local file="$1"
138 local package_url="$2"
139
140 if [[ ! -f "$file" ]]; then
141 echo "ERROR: Expected file not found: ${file}"
142 return 1
143 fi
144
145 curl --fail --show-error --header "JOB-TOKEN: ${CI_JOB_TOKEN}" \
146 --upload-file "$file" \
147 "$package_url"
148
149 echo "Uploaded ${file}"
150 echo " -> ${package_url}"
151}
152
153upload_package() {
154 local version="${1:?version required}"
155 local jar="build/libs/CopperKeys-${version}.jar"
156 local pack="build/dist/CopperKeys-resourcepack-${version}.zip"
157 local base_url package_jar_url package_pack_url
158
159 if [[ ! -f "$jar" ]]; then
160 echo "ERROR: Expected jar not found: ${jar}"
161 ls -la build/libs/ || true
162 exit 1
163 fi
164 if [[ ! -f "$pack" ]]; then
165 echo "ERROR: Expected resource pack zip not found: ${pack}"
166 ls -la build/dist/ || true
167 exit 1
168 fi
169
170 base_url="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/copperkeys/${version}"
171 package_jar_url="${base_url}/CopperKeys-${version}.jar"
172 package_pack_url="${base_url}/CopperKeys-resourcepack-${version}.zip"
173
174 upload_file "$jar" "$package_jar_url"
175 upload_file "$pack" "$package_pack_url"
176
177 {
178 echo "PACKAGE_URL=${package_jar_url}"
179 echo "RESOURCE_PACK_URL=${package_pack_url}"
180 } > package.env
181}
182
183# Self-rotate CI_PUSH_TOKEN and write the new value back to the CI/CD variable.
184# Requires scopes: write_repository + self_rotate (or api), and api to update variables.
185# Project access token role should be Maintainer (needed to edit CI/CD variables).
186rotate_push_token() {
187 local old_token expires_at response new_token var_meta protected masked raw scope
188
189 require_push_token
190 old_token="${CI_PUSH_TOKEN}"
191
192 expires_at="$(date -u -d '+365 days' +%Y-%m-%d)"
193 echo "Rotating CI_PUSH_TOKEN (new expiry ${expires_at})..."
194
195 response="$(curl --fail --silent --show-error \
196 --request POST \
197 --header "PRIVATE-TOKEN: ${old_token}" \
198 --header "Content-Type: application/json" \
199 --data "{\"expires_at\":\"${expires_at}\"}" \
200 "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/access_tokens/self/rotate")"
201
202 new_token="$(jq -er '.token' <<<"${response}")"
203 if [[ -z "${new_token}" || "${new_token}" == "null" ]]; then
204 echo "ERROR: Token rotation response did not include a new token."
205 echo "${response}"
206 exit 1
207 fi
208
209 var_meta="$(curl --fail --silent --show-error \
210 --header "PRIVATE-TOKEN: ${new_token}" \
211 "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/variables/CI_PUSH_TOKEN")"
212
213 protected="$(jq -r '.protected' <<<"${var_meta}")"
214 masked="$(jq -r '.masked' <<<"${var_meta}")"
215 raw="$(jq -r '.raw // false' <<<"${var_meta}")"
216 scope="$(jq -r '.environment_scope // "*"' <<<"${var_meta}")"
217
218 curl --fail --silent --show-error \
219 --request PUT \
220 --header "PRIVATE-TOKEN: ${new_token}" \
221 --form "value=${new_token}" \
222 --form "protected=${protected}" \
223 --form "masked=${masked}" \
224 --form "raw=${raw}" \
225 --form "environment_scope=${scope}" \
226 "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/variables/CI_PUSH_TOKEN" \
227 >/dev/null
228
229 echo "CI_PUSH_TOKEN rotated and CI/CD variable updated."
230}
231
232case "${1:-}" in
233 validate) validate ;;
234 bump)
235 # shellcheck disable=SC1091
236 source version.env
237 bump_and_push "$VERSION" "$TAG"
238 ;;
239 upload)
240 # shellcheck disable=SC1091
241 source version.env
242 upload_package "$VERSION"
243 ;;
244 rotate-token) rotate_push_token ;;
245 *)
246 echo "Usage: $0 {validate|bump|upload|rotate-token}"
247 exit 1
248 ;;
249esac