BlueGit

timfa minecart-trains

Files

minecart-trains / scripts / release.sh

release.sh

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