#!/usr/bin/env bash # Release helpers for GitLab CI. set -euo pipefail VERSION_REGEX='^v?(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$' normalize_version() { echo "${1#v}" } is_valid_version_tag() { [[ "$1" =~ $VERSION_REGEX ]] } # True if $1 is strictly greater than $2 (semver X.Y.Z). is_newer_version() { local a b a="$(normalize_version "$1")" b="$(normalize_version "$2")" [[ "$a" != "$b" ]] && [[ "$(printf '%s\n' "$a" "$b" | sort -V | tail -n1)" == "$a" ]] } write_version_env() { local version="$1" local tag="$2" { echo "VERSION=${version}" echo "TAG=${tag}" } > version.env } validate() { local tag="${CI_COMMIT_TAG:?CI_COMMIT_TAG is required}" local version newest if ! is_valid_version_tag "$tag"; then echo "ERROR: Tag '${tag}' has an invalid format." echo "Expected: v1.7.0, 1.7.10, 1.8.0, ..." exit 1 fi version="$(normalize_version "$tag")" echo "Validated release tag: ${tag} (version ${version})" mapfile -t tags < <( git tag -l \ | grep -E "$VERSION_REGEX" \ | grep -vxF "$tag" \ || true ) if ((${#tags[@]} == 0)); then echo "No previous release tags found; accepting ${version} as first release." write_version_env "$version" "$tag" return 0 fi newest="$(printf '%s\n' "${tags[@]}" | sed 's/^v//' | sort -V | tail -n1)" echo "Previous release version: ${newest}" if ! is_newer_version "$version" "$newest"; then echo "ERROR: Release version ${version} is not greater than previous release ${newest}." exit 1 fi write_version_env "$version" "$tag" echo "Version check passed: ${version} > ${newest}" } bump_files() { local version="${1:?version required}" local readme sed -i "s/^version = \".*\"/version = \"${version}\"/" build.gradle.kts sed -i "s/^version: '.*/version: '${version}'/" src/main/resources/plugin.yml sed -i "1s/.*/# ContainerPipes ${version}/" README.md sed -i -E "s/ContainerPipes [0-9]+\.[0-9]+\.[0-9]+ enabled/ContainerPipes ${version} enabled/" \ src/main/java/nl/timfalken/containerpipes/ContainerPipesPlugin.java shopt -s nullglob for readme in src/README-*.md; do sed -i "1s/.*/# ContainerPipes ${version}/" "$readme" if [[ "$readme" != "src/README-${version}.md" ]]; then git mv "$readme" "src/README-${version}.md" fi done shopt -u nullglob echo "Updated project files to version ${version}" } bump_and_push() { local version="${1:?version required}" local tag="${2:?tag required}" local token="${CI_PUSH_TOKEN:?Set CI/CD variable CI_PUSH_TOKEN (Project Access Token with write_repository)}" local branch="${CI_DEFAULT_BRANCH:?CI_DEFAULT_BRANCH is required}" git config user.email "gitlab-ci@${CI_SERVER_HOST:-gitlab.com}" git config user.name "GitLab CI" bump_files "$version" git add -A if git diff --cached --quiet; then echo "No version file changes to commit (already at ${version})." else git commit -m "chore: bump version to ${version}" fi git remote set-url origin "https://oauth2:${token}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git" # Push commit and retarget the release tag without starting a new pipeline. git push origin "HEAD:refs/heads/${branch}" -o ci.skip git tag -f "$tag" git push origin "refs/tags/${tag}" --force -o ci.skip echo "Pushed version bump to ${branch} and moved tag ${tag} (ci.skip)." } upload_package() { local version="${1:?version required}" local jar="build/libs/ContainerPipes-${version}.jar" local package_url if [[ ! -f "$jar" ]]; then echo "ERROR: Expected jar not found: ${jar}" ls -la build/libs/ || true exit 1 fi package_url="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/containerpipes/${version}/ContainerPipes-${version}.jar" curl --fail --show-error --header "JOB-TOKEN: ${CI_JOB_TOKEN}" \ --upload-file "$jar" \ "$package_url" echo "Uploaded ${jar}" echo "PACKAGE_URL=${package_url}" > package.env } case "${1:-}" in validate) validate ;; bump) # shellcheck disable=SC1091 source version.env bump_and_push "$VERSION" "$TAG" ;; upload) # shellcheck disable=SC1091 source version.env upload_package "$VERSION" ;; *) echo "Usage: $0 {validate|bump|upload}" exit 1 ;; esac