BlueGit

timfa containerpipes

Files

containerpipes / scripts / release.sh

release.sh

release.sh 6.1 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 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/.*/# ContainerPipes ${version}/" README.md
77 sed -i -E "s/ContainerPipes [0-9]+\.[0-9]+\.[0-9]+ enabled/ContainerPipes ${version} enabled/" \
78 src/main/java/nl/timfalken/containerpipes/ContainerPipesPlugin.java
79
80 shopt -s nullglob
81 for readme in src/README-*.md; do
82 sed -i "1s/.*/# ContainerPipes ${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
92bump_and_push() {
93 local version="${1:?version required}"
94 local tag="${2:?tag required}"
95 local token="${CI_PUSH_TOKEN:?Set CI/CD variable CI_PUSH_TOKEN (Project Access Token with write_repository)}"
96 local branch="${CI_DEFAULT_BRANCH:?CI_DEFAULT_BRANCH is required}"
97
98 git config user.email "gitlab-ci@${CI_SERVER_HOST:-gitlab.com}"
99 git config user.name "GitLab CI"
100
101 bump_files "$version"
102
103 git add -A
104 if git diff --cached --quiet; then
105 echo "No version file changes to commit (already at ${version})."
106 else
107 git commit -m "chore: bump version to ${version}"
108 fi
109
110 git remote set-url origin "https://oauth2:${token}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git"
111
112 # Push commit and retarget the release tag without starting a new pipeline.
113 git push origin "HEAD:refs/heads/${branch}" -o ci.skip
114 git tag -f "$tag"
115 git push origin "refs/tags/${tag}" --force -o ci.skip
116
117 echo "Pushed version bump to ${branch} and moved tag ${tag} (ci.skip)."
118}
119
120upload_package() {
121 local version="${1:?version required}"
122 local jar="build/libs/ContainerPipes-${version}.jar"
123 local package_url
124
125 if [[ ! -f "$jar" ]]; then
126 echo "ERROR: Expected jar not found: ${jar}"
127 ls -la build/libs/ || true
128 exit 1
129 fi
130
131 package_url="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/containerpipes/${version}/ContainerPipes-${version}.jar"
132 curl --fail --show-error --header "JOB-TOKEN: ${CI_JOB_TOKEN}" \
133 --upload-file "$jar" \
134 "$package_url"
135
136 echo "Uploaded ${jar}"
137 echo "PACKAGE_URL=${package_url}" > package.env
138}
139
140# Self-rotate CI_PUSH_TOKEN and write the new value back to the CI/CD variable.
141# Requires scopes: write_repository + self_rotate (or api), and api to update variables.
142# Project access token role should be Maintainer (needed to edit CI/CD variables).
143rotate_push_token() {
144 local old_token="${CI_PUSH_TOKEN:?Set CI/CD variable CI_PUSH_TOKEN}"
145 local expires_at response new_token var_meta protected masked raw scope
146
147 expires_at="$(date -u -d '+365 days' +%Y-%m-%d)"
148 echo "Rotating CI_PUSH_TOKEN (new expiry ${expires_at})..."
149
150 response="$(curl --fail --silent --show-error \
151 --request POST \
152 --header "PRIVATE-TOKEN: ${old_token}" \
153 --header "Content-Type: application/json" \
154 --data "{\"expires_at\":\"${expires_at}\"}" \
155 "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/access_tokens/self/rotate")"
156
157 new_token="$(jq -er '.token' <<<"${response}")"
158 if [[ -z "${new_token}" || "${new_token}" == "null" ]]; then
159 echo "ERROR: Token rotation response did not include a new token."
160 echo "${response}"
161 exit 1
162 fi
163
164 var_meta="$(curl --fail --silent --show-error \
165 --header "PRIVATE-TOKEN: ${new_token}" \
166 "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/variables/CI_PUSH_TOKEN")"
167
168 protected="$(jq -r '.protected' <<<"${var_meta}")"
169 masked="$(jq -r '.masked' <<<"${var_meta}")"
170 raw="$(jq -r '.raw // false' <<<"${var_meta}")"
171 scope="$(jq -r '.environment_scope // "*"' <<<"${var_meta}")"
172
173 curl --fail --silent --show-error \
174 --request PUT \
175 --header "PRIVATE-TOKEN: ${new_token}" \
176 --form "value=${new_token}" \
177 --form "protected=${protected}" \
178 --form "masked=${masked}" \
179 --form "raw=${raw}" \
180 --form "environment_scope=${scope}" \
181 "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/variables/CI_PUSH_TOKEN" \
182 >/dev/null
183
184 echo "CI_PUSH_TOKEN rotated and CI/CD variable updated."
185}
186
187case "${1:-}" in
188 validate) validate ;;
189 bump)
190 # shellcheck disable=SC1091
191 source version.env
192 bump_and_push "$VERSION" "$TAG"
193 ;;
194 upload)
195 # shellcheck disable=SC1091
196 source version.env
197 upload_package "$VERSION"
198 ;;
199 rotate-token) rotate_push_token ;;
200 *)
201 echo "Usage: $0 {validate|bump|upload|rotate-token}"
202 exit 1
203 ;;
204esac