Add a GitLab Maven release pipeline for version tags.
Co-authored-by: Cursor <cursoragent@cursor.com>
@@ -1,2 +1,4 @@
1
1
# Auto detect text files and perform LF normalization
2
2
* text=auto
3
*.sh text eol=lf
4
.gitlab-ci.yml text eol=lf
@@ -1,2 +1,4 @@1
# Auto detect text files and perform LF normalization1
# Auto detect text files and perform LF normalization2
* text=auto2
* text=auto3
*.sh text eol=lf4
.gitlab-ci.yml text eol=lf@@ -0,0 +1,117 @@
1
# Release pipeline: runs when a version tag is created (e.g. v1.5.0 or 1.5.0).
2
#
3
# Required CI/CD variable:
4
# CI_PUSH_TOKEN — Project Access Token (Maintainer) with scopes:
5
# `write_repository`, `self_rotate` (or `api`), and `api`
6
# (api is required to write the rotated token back to this variable).
7
#
8
# Flow:
9
# 1. validate — reject invalid or non-increasing versions
10
# 2. bump — update version in project files and push commit (ci.skip; tag unchanged)
11
# 3. build — produce MinecartTrains-<version>.jar; upload to Package Registry
12
# 4. release — publish a GitLab Release with the jar attached
13
# 5. rotate-token — self-rotate CI_PUSH_TOKEN and update the CI/CD variable
14
15
workflow:
16
rules:
17
- if: $CI_COMMIT_TAG
18
19
variables:
20
GIT_DEPTH: "0"
21
GIT_FETCH_EXTRA_FLAGS: "--tags"
22
MAVEN_OPTS: "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn"
23
PACKAGE_NAME: minecart-trains
24
JAR_NAME: "MinecartTrains-${VERSION}.jar"
25
26
stages:
27
- validate
28
- bump
29
- build
30
- release
31
- rotate
32
33
.validate_image:
34
image: alpine:3.20
35
before_script:
36
- apk add --no-cache bash git grep coreutils
37
- git fetch --tags --force origin
38
- chmod +x scripts/release.sh
39
40
validate:version:
41
extends: .validate_image
42
stage: validate
43
script:
44
- ./scripts/release.sh validate
45
artifacts:
46
reports:
47
dotenv: version.env
48
paths:
49
- version.env
50
51
bump:version:
52
extends: .validate_image
53
stage: bump
54
needs:
55
- job: validate:version
56
artifacts: true
57
script:
58
- ./scripts/release.sh bump
59
artifacts:
60
reports:
61
dotenv: version.env
62
paths:
63
- version.env
64
- pom.xml
65
- README.md
66
67
build:artifacts:
68
stage: build
69
image: maven:3.9.11-eclipse-temurin-25
70
needs:
71
- job: bump:version
72
artifacts: true
73
before_script:
74
- apt-get update -qq && apt-get install -y -qq curl >/dev/null
75
- chmod +x scripts/release.sh
76
script:
77
- test -n "${VERSION}"
78
- mvn -B -DskipTests package
79
- test -f "target/${JAR_NAME}"
80
- ls -la "target/${JAR_NAME}"
81
- ./scripts/release.sh upload
82
artifacts:
83
reports:
84
dotenv: version.env
85
paths:
86
- "target/MinecartTrains-*.jar"
87
- version.env
88
expire_in: 30 days
89
90
release:gitlab:
91
stage: release
92
image: registry.gitlab.com/gitlab-org/release-cli:latest
93
needs:
94
- job: build:artifacts
95
artifacts: true
96
script:
97
- echo "Publishing GitLab Release for ${TAG} (version ${VERSION})"
98
release:
99
name: "Minecart Trains $VERSION"
100
tag_name: "$TAG"
101
description: "Release $VERSION of Minecart Trains."
102
assets:
103
links:
104
- name: "MinecartTrains-$VERSION.jar"
105
url: "$CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic/$PACKAGE_NAME/$VERSION/MinecartTrains-$VERSION.jar"
106
link_type: package
107
108
rotate:push-token:
109
image: alpine:3.20
110
stage: rotate
111
needs:
112
- job: release:gitlab
113
before_script:
114
- apk add --no-cache bash curl jq coreutils
115
- chmod +x scripts/release.sh
116
script:
117
- ./scripts/release.sh rotate-token
@@ -0,0 +1,117 @@1
# Release pipeline: runs when a version tag is created (e.g. v1.5.0 or 1.5.0).2
#3
# Required CI/CD variable:4
# CI_PUSH_TOKEN — Project Access Token (Maintainer) with scopes:5
# `write_repository`, `self_rotate` (or `api`), and `api`6
# (api is required to write the rotated token back to this variable).7
#8
# Flow:9
# 1. validate — reject invalid or non-increasing versions10
# 2. bump — update version in project files and push commit (ci.skip; tag unchanged)11
# 3. build — produce MinecartTrains-<version>.jar; upload to Package Registry12
# 4. release — publish a GitLab Release with the jar attached13
# 5. rotate-token — self-rotate CI_PUSH_TOKEN and update the CI/CD variable14
15
workflow:16
rules:17
- if: $CI_COMMIT_TAG18
19
variables:20
GIT_DEPTH: "0"21
GIT_FETCH_EXTRA_FLAGS: "--tags"22
MAVEN_OPTS: "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn"23
PACKAGE_NAME: minecart-trains24
JAR_NAME: "MinecartTrains-${VERSION}.jar"25
26
stages:27
- validate28
- bump29
- build30
- release31
- rotate32
33
.validate_image:34
image: alpine:3.2035
before_script:36
- apk add --no-cache bash git grep coreutils37
- git fetch --tags --force origin38
- chmod +x scripts/release.sh39
40
validate:version:41
extends: .validate_image42
stage: validate43
script:44
- ./scripts/release.sh validate45
artifacts:46
reports:47
dotenv: version.env48
paths:49
- version.env50
51
bump:version:52
extends: .validate_image53
stage: bump54
needs:55
- job: validate:version56
artifacts: true57
script:58
- ./scripts/release.sh bump59
artifacts:60
reports:61
dotenv: version.env62
paths:63
- version.env64
- pom.xml65
- README.md66
67
build:artifacts:68
stage: build69
image: maven:3.9.11-eclipse-temurin-2570
needs:71
- job: bump:version72
artifacts: true73
before_script:74
- apt-get update -qq && apt-get install -y -qq curl >/dev/null75
- chmod +x scripts/release.sh76
script:77
- test -n "${VERSION}"78
- mvn -B -DskipTests package79
- test -f "target/${JAR_NAME}"80
- ls -la "target/${JAR_NAME}"81
- ./scripts/release.sh upload82
artifacts:83
reports:84
dotenv: version.env85
paths:86
- "target/MinecartTrains-*.jar"87
- version.env88
expire_in: 30 days89
90
release:gitlab:91
stage: release92
image: registry.gitlab.com/gitlab-org/release-cli:latest93
needs:94
- job: build:artifacts95
artifacts: true96
script:97
- echo "Publishing GitLab Release for ${TAG} (version ${VERSION})"98
release:99
name: "Minecart Trains $VERSION"100
tag_name: "$TAG"101
description: "Release $VERSION of Minecart Trains."102
assets:103
links:104
- name: "MinecartTrains-$VERSION.jar"105
url: "$CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic/$PACKAGE_NAME/$VERSION/MinecartTrains-$VERSION.jar"106
link_type: package107
108
rotate:push-token:109
image: alpine:3.20110
stage: rotate111
needs:112
- job: release:gitlab113
before_script:114
- apk add --no-cache bash curl jq coreutils115
- chmod +x scripts/release.sh116
script:117
- ./scripts/release.sh rotate-token@@ -1,21 +1,15 @@
1
[](https://papermc.io/)
2
[]()
3
[]()
4
[]()
5
[](https://github.com/Fridtjof-DE/MinecartTrains/issues)
6
[](https://discord.gg/fT6VJurHCT)
1
## Forked from: [https://github.com/Fridtjof-DE/MinecartTrains/](https://github.com/Fridtjof-DE/MinecartTrains/)
7
2
8
[](https://modrinth.com/plugin/minecart-trains/versions)
3

9
4
10
[](https://bstats.org/plugin/bukkit/Minecart%20Trains/18918)
11
[](https://bstats.org/plugin/bukkit/Minecart%20Trains/18918)
12
13
# <img src="./docs/images/icon.png" data-canonical-src="./icon.png" width="32" height="32" /> Minecart Trains
5
#  Minecart Trains
14
6
15
7
Introducing Minecart Trains! Experience a whole new level of immersion as you connect minecarts together, transforming them into powerful and efficient trains.
16
8
17
9

10
18
11
### Features:
12
19
13
- linking minecarts to form a train
20
14
- a faint coupler line between linked carts (particle packets to nearby players only)
21
15
- freezing furnace minecarts with non-powered booster rails (to stop trains)
@@ -26,7 +20,10 @@ Introducing Minecart Trains! Experience a whole new level of immersion as you co
26
20
- fuel consumption of the furnace minecart is configurable
27
21
- highspeed tracks inspired by hsrails (only on adapted railways)
28
22
23
24
29
25
### How it works:
26
30
27
**Coupling:**
31
28
32
29
The standard coupling tool is the chain. Click two minecarts one after the other to couple them. The click order does not matter: free carts, train ends, and whole trains are connected at the carts you click. Furnace minecarts become the front of a new pair when present.
@@ -38,22 +35,32 @@ Furnace minecarts will, if already going, refuel from a coal cart in front or be
38
35
Fueling a furnace minecart sets its push direction to the way you are looking, even if it already has fuel. Sneak-right-click an empty hand on a furnace minecart to reverse it. Linked carts keep following on both sides of the engine.
39
36
40
37
### What is planned:
38
41
39
- safe chunk unloading
42
40
- auto. chunk loading
43
41
- some kind of fuel gauge
44
42
43
44
45
45
### What is not (yet) possible:
46
46
47
- trains going around 2x2 curves
47
48
49
50
48
51
### Requirements
49
52
50
- Paper 26.2 (Java 25)
51
- No extra plugin dependencies
53
- Paper 26.2 (Java 25)
54
- No extra plugin dependencies
55
56
52
57
53
58
### Relevant Links
54
59
55
- modrinth: https://modrinth.com/plugin/minecart-trains
56
- bStats: https://bstats.org/plugin/bukkit/Minecart%20Trains/18918
60
- modrinth: [https://modrinth.com/plugin/minecart-trains](https://modrinth.com/plugin/minecart-trains)
61
- bStats: [https://bstats.org/plugin/bukkit/Minecart%20Trains/18918](https://bstats.org/plugin/bukkit/Minecart%20Trains/18918)
62
63
57
64
58
65
## How to Build
59
66
@@ -65,9 +72,10 @@ mvn package
65
72
66
73
Results will be in the `target` folder. With version `1.5.0` that is:
67
74
68
- `MinecartTrains-1.5.0.jar`
75
- `MinecartTrains-1.5.0.jar`
69
76
70
77
Copy this file to the plugins folder of a Paper 26.2 server.
71
78
72
79
## bStats
73

80
81

@@ -1,21 +1,15 @@1
[](https://papermc.io/)1
## Forked from: [https://github.com/Fridtjof-DE/MinecartTrains/](https://github.com/Fridtjof-DE/MinecartTrains/)2
[]()3
[]()4
[]()5
[](https://github.com/Fridtjof-DE/MinecartTrains/issues)6
[](https://discord.gg/fT6VJurHCT)7
2
8
[](https://modrinth.com/plugin/minecart-trains/versions)3
9
4
10
[](https://bstats.org/plugin/bukkit/Minecart%20Trains/18918)5
#  Minecart Trains11
[](https://bstats.org/plugin/bukkit/Minecart%20Trains/18918)12
13
# <img src="./docs/images/icon.png" data-canonical-src="./icon.png" width="32" height="32" /> Minecart Trains14
6
15
Introducing Minecart Trains! Experience a whole new level of immersion as you connect minecarts together, transforming them into powerful and efficient trains.7
Introducing Minecart Trains! Experience a whole new level of immersion as you connect minecarts together, transforming them into powerful and efficient trains.16
8
17
9
10
18
### Features:11
### Features:12
19
- linking minecarts to form a train13
- linking minecarts to form a train20
- a faint coupler line between linked carts (particle packets to nearby players only)14
- a faint coupler line between linked carts (particle packets to nearby players only)21
- freezing furnace minecarts with non-powered booster rails (to stop trains)15
- freezing furnace minecarts with non-powered booster rails (to stop trains)@@ -26,7 +20,10 @@ Introducing Minecart Trains! Experience a whole new level of immersion as you co26
- fuel consumption of the furnace minecart is configurable20
- fuel consumption of the furnace minecart is configurable27
- highspeed tracks inspired by hsrails (only on adapted railways)21
- highspeed tracks inspired by hsrails (only on adapted railways)28
22
23
24
29
### How it works:25
### How it works:26
30
**Coupling:**27
**Coupling:**31
28
32
The standard coupling tool is the chain. Click two minecarts one after the other to couple them. The click order does not matter: free carts, train ends, and whole trains are connected at the carts you click. Furnace minecarts become the front of a new pair when present.29
The standard coupling tool is the chain. Click two minecarts one after the other to couple them. The click order does not matter: free carts, train ends, and whole trains are connected at the carts you click. Furnace minecarts become the front of a new pair when present.@@ -38,22 +35,32 @@ Furnace minecarts will, if already going, refuel from a coal cart in front or be38
Fueling a furnace minecart sets its push direction to the way you are looking, even if it already has fuel. Sneak-right-click an empty hand on a furnace minecart to reverse it. Linked carts keep following on both sides of the engine.35
Fueling a furnace minecart sets its push direction to the way you are looking, even if it already has fuel. Sneak-right-click an empty hand on a furnace minecart to reverse it. Linked carts keep following on both sides of the engine.39
36
40
### What is planned:37
### What is planned:38
41
- safe chunk unloading39
- safe chunk unloading42
- auto. chunk loading40
- auto. chunk loading43
- some kind of fuel gauge41
- some kind of fuel gauge44
42
43
44
45
### What is not (yet) possible:45
### What is not (yet) possible:46
46
- trains going around 2x2 curves47
- trains going around 2x2 curves47
48
49
50
48
### Requirements51
### Requirements49
52
50
- Paper 26.2 (Java 25)53
- Paper 26.2 (Java 25)51
- No extra plugin dependencies54
- No extra plugin dependencies55
56
52
57
53
### Relevant Links58
### Relevant Links54
59
55
- modrinth: https://modrinth.com/plugin/minecart-trains60
- modrinth: [https://modrinth.com/plugin/minecart-trains](https://modrinth.com/plugin/minecart-trains)56
- bStats: https://bstats.org/plugin/bukkit/Minecart%20Trains/1891861
- bStats: [https://bstats.org/plugin/bukkit/Minecart%20Trains/18918](https://bstats.org/plugin/bukkit/Minecart%20Trains/18918)62
63
57
64
58
## How to Build65
## How to Build59
66
@@ -65,9 +72,10 @@ mvn package65
72
66
Results will be in the `target` folder. With version `1.5.0` that is:73
Results will be in the `target` folder. With version `1.5.0` that is:67
74
68
- `MinecartTrains-1.5.0.jar`75
- `MinecartTrains-1.5.0.jar`69
76
70
Copy this file to the plugins folder of a Paper 26.2 server.77
Copy this file to the plugins folder of a Paper 26.2 server.71
78
72
## bStats79
## bStats73
80
81
@@ -0,0 +1,239 @@
1
#!/usr/bin/env bash
2
# Release helpers for GitLab CI.
3
set -euo pipefail
4
5
VERSION_REGEX='^v?(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$'
6
7
normalize_version() {
8
echo "${1#v}"
9
}
10
11
is_valid_version_tag() {
12
[[ "$1" =~ $VERSION_REGEX ]]
13
}
14
15
# True if $1 is strictly greater than $2 (semver X.Y.Z).
16
is_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
23
write_version_env() {
24
local version="$1"
25
local tag="$2"
26
{
27
echo "VERSION=${version}"
28
echo "TAG=${tag}"
29
} > version.env
30
}
31
32
validate() {
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
70
bump_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
80
require_push_token() {
81
if [[ -n "${CI_PUSH_TOKEN:-}" ]]; then
82
return 0
83
fi
84
85
cat <<'EOF' >&2
86
ERROR: CI_PUSH_TOKEN is not available in this job.
87
88
The 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
94
EOF
95
exit 1
96
}
97
98
bump_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
135
upload_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
152
upload_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).
176
rotate_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
222
case "${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
;;
239
esac
@@ -0,0 +1,239 @@1
#!/usr/bin/env bash2
# Release helpers for GitLab CI.3
set -euo pipefail4
5
VERSION_REGEX='^v?(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$'6
7
normalize_version() {8
echo "${1#v}"9
}10
11
is_valid_version_tag() {12
[[ "$1" =~ $VERSION_REGEX ]]13
}14
15
# True if $1 is strictly greater than $2 (semver X.Y.Z).16
is_newer_version() {17
local a b18
a="$(normalize_version "$1")"19
b="$(normalize_version "$2")"20
[[ "$a" != "$b" ]] && [[ "$(printf '%s\n' "$a" "$b" | sort -V | tail -n1)" == "$a" ]]21
}22
23
write_version_env() {24
local version="$1"25
local tag="$2"26
{27
echo "VERSION=${version}"28
echo "TAG=${tag}"29
} > version.env30
}31
32
validate() {33
local tag="${CI_COMMIT_TAG:?CI_COMMIT_TAG is required}"34
local version newest35
36
if ! is_valid_version_tag "$tag"; then37
echo "ERROR: Tag '${tag}' has an invalid format."38
echo "Expected: v1.5.0, 1.5.10, 1.6.0, ..."39
exit 140
fi41
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
|| true50
)51
52
if ((${#tags[@]} == 0)); then53
echo "No previous release tags found; accepting ${version} as first release."54
write_version_env "$version" "$tag"55
return 056
fi57
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"; then62
echo "ERROR: Release version ${version} is not greater than previous release ${newest}."63
exit 164
fi65
66
write_version_env "$version" "$tag"67
echo "Version check passed: ${version} > ${newest}"68
}69
70
bump_files() {71
local version="${1:?version required}"72
73
sed -i "/<artifactId>minecarttrains<\/artifactId>/{n;s|<version>.*</version>|<version>${version}</version>|}" pom.xml74
sed -i -E "s/MinecartTrains-[0-9]+\.[0-9]+\.[0-9]+/MinecartTrains-${version}/g" README.md75
sed -i -E "s/version \`[0-9]+\.[0-9]+\.[0-9]+\`/version \`${version}\`/" README.md76
77
echo "Updated project files to version ${version}"78
}79
80
require_push_token() {81
if [[ -n "${CI_PUSH_TOKEN:-}" ]]; then82
return 083
fi84
85
cat <<'EOF' >&286
ERROR: CI_PUSH_TOKEN is not available in this job.87
88
The 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 protected90
→ Protect tag pattern (e.g. *) or uncheck Protected on the variable91
2. Its Environment scope is not "*"92
3. The key is misspelled (must be exactly CI_PUSH_TOKEN)93
94
EOF95
exit 196
}97
98
bump_and_push() {99
local version="${1:?version required}"100
local tag="${2:?tag required}"101
local token branch102
103
require_push_token104
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.md116
117
if git diff --cached --quiet; then118
echo "No version file changes to commit (already at ${version})."119
else120
git commit -m "chore: bump version to ${version}"121
fi122
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; then128
echo "Pushed version bump for ${tag} to ${branch} (ci.skip). Tag left unchanged."129
else130
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
fi133
}134
135
upload_file() {136
local file="$1"137
local package_url="$2"138
139
if [[ ! -f "$file" ]]; then140
echo "ERROR: Expected file not found: ${file}"141
return 1142
fi143
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
152
upload_package() {153
local version="${1:?version required}"154
local jar="target/MinecartTrains-${version}.jar"155
local base_url package_jar_url156
157
if [[ ! -f "$jar" ]]; then158
echo "ERROR: Expected jar not found: ${jar}"159
ls -la target/ || true160
exit 1161
fi162
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.env171
}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).176
rotate_push_token() {177
local old_token expires_at response new_token var_meta protected masked raw scope178
179
require_push_token180
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" ]]; then194
echo "ERROR: Token rotation response did not include a new token."195
echo "${response}"196
exit 1197
fi198
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/null218
219
echo "CI_PUSH_TOKEN rotated and CI/CD variable updated."220
}221
222
case "${1:-}" in223
validate) validate ;;224
bump)225
# shellcheck disable=SC1091226
source version.env227
bump_and_push "$VERSION" "$TAG"228
;;229
upload)230
# shellcheck disable=SC1091231
source version.env232
upload_package "$VERSION"233
;;234
rotate-token) rotate_push_token ;;235
*)236
echo "Usage: $0 {validate|bump|upload|rotate-token}"237
exit 1238
;;239
esac