BlueGit

timfa copper-key

Files

copper-key / scripts / release.sh

release.sh

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