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