BlueGit

timfa containerpipes

Files

containerpipes / scripts / release.sh

release.sh

import 1.7.0
timfa Tim Falken · pushed by timfa · 3 months ago
b32cd771 History
release.sh 4.2 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
140case "${1:-}" in
141 validate) validate ;;
142 bump)
143 # shellcheck disable=SC1091
144 source version.env
145 bump_and_push "$VERSION" "$TAG"
146 ;;
147 upload)
148 # shellcheck disable=SC1091
149 source version.env
150 upload_package "$VERSION"
151 ;;
152 *)
153 echo "Usage: $0 {validate|bump|upload}"
154 exit 1
155 ;;
156esac