306 lines
11 KiB
Bash
Executable File
306 lines
11 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# End-of-pipeline release automation for Asteria Qt (master branch only):
|
|
# 1. Checks GITLAB_TOKEN expiration and self-rotates + updates the GitLab CI/CD
|
|
# variable if expiry is within TOKEN_ROTATE_THRESHOLD_DAYS (default: 30).
|
|
# 2. Creates release Git tag v<BUILT_VERSION> at CI_COMMIT_SHA (if absent).
|
|
# 3. Increments patch version in VERSION on the target branch via GitLab
|
|
# Repository Commits API with [skip ci].
|
|
|
|
set -euo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
PROJECT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
|
|
cd "$PROJECT_DIR"
|
|
|
|
DEFAULT_BRANCH="${CI_DEFAULT_BRANCH:-master}"
|
|
TARGET_BRANCH="${CI_COMMIT_BRANCH:-}"
|
|
TOKEN_ROTATE_THRESHOLD_DAYS="${TOKEN_ROTATE_THRESHOLD_DAYS:-30}"
|
|
TOKEN_ROTATE_LIFETIME_DAYS="${TOKEN_ROTATE_LIFETIME_DAYS:-360}"
|
|
|
|
if [ "$TARGET_BRANCH" != "$DEFAULT_BRANCH" ] && [ "$TARGET_BRANCH" != "master" ]; then
|
|
echo "Skipping version bump: branch is '${TARGET_BRANCH:-unknown}', not '${DEFAULT_BRANCH}'."
|
|
exit 0
|
|
fi
|
|
|
|
if [ -z "${GITLAB_TOKEN:-}" ]; then
|
|
echo "ERROR: GITLAB_TOKEN CI/CD variable is not set" >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [ -z "${CI_PROJECT_ID:-}" ] || [ -z "${CI_COMMIT_SHA:-}" ]; then
|
|
echo "ERROR: CI_PROJECT_ID and CI_COMMIT_SHA must be set" >&2
|
|
exit 1
|
|
fi
|
|
|
|
API_ROOT="${CI_API_V4_URL:-https://gitlab.com/api/v4}"
|
|
PROJECT_API="${API_ROOT}/projects/${CI_PROJECT_ID}"
|
|
|
|
WORK_TMP="$(mktemp -d)"
|
|
cleanup() {
|
|
rm -rf "$WORK_TMP"
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
parse_semver() {
|
|
local v="$1"
|
|
if ! [[ "$v" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
|
|
echo "ERROR: Invalid SemVer '$v' (expected MAJOR.MINOR.PATCH)" >&2
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
semver_gt() {
|
|
local a="$1" b="$2"
|
|
parse_semver "$a"
|
|
local a1="${BASH_REMATCH[1]}" a2="${BASH_REMATCH[2]}" a3="${BASH_REMATCH[3]}"
|
|
parse_semver "$b"
|
|
local b1="${BASH_REMATCH[1]}" b2="${BASH_REMATCH[2]}" b3="${BASH_REMATCH[3]}"
|
|
|
|
if [ "$a1" -ne "$b1" ]; then
|
|
[ "$a1" -gt "$b1" ]
|
|
elif [ "$a2" -ne "$b2" ]; then
|
|
[ "$a2" -gt "$b2" ]
|
|
else
|
|
[ "$a3" -gt "$b3" ]
|
|
fi
|
|
}
|
|
|
|
increment_patch() {
|
|
local v="$1"
|
|
parse_semver "$v"
|
|
local major="${BASH_REMATCH[1]}"
|
|
local minor="${BASH_REMATCH[2]}"
|
|
local patch="${BASH_REMATCH[3]}"
|
|
echo "${major}.${minor}.$((patch + 1))"
|
|
}
|
|
|
|
ensure_fresh_token() {
|
|
local info_file="${WORK_TMP}/token_self.json"
|
|
local info_code
|
|
info_code=$(curl -s -o "$info_file" -w '%{http_code}' \
|
|
--header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
|
|
"${API_ROOT}/personal_access_tokens/self")
|
|
|
|
if [ "$info_code" != "200" ]; then
|
|
echo "WARNING: Could not query token expiry (HTTP ${info_code}); proceeding with current GITLAB_TOKEN."
|
|
return 0
|
|
fi
|
|
|
|
local expires_at
|
|
expires_at="$(jq -r '.expires_at // empty' "$info_file")"
|
|
if [ -z "$expires_at" ]; then
|
|
echo "GITLAB_TOKEN has no expiration date; skipping rotation."
|
|
return 0
|
|
fi
|
|
|
|
local days_left
|
|
days_left="$(python3 - "$expires_at" <<'PY'
|
|
from datetime import date
|
|
import sys
|
|
exp = date.fromisoformat(sys.argv[1][:10])
|
|
print((exp - date.today()).days)
|
|
PY
|
|
)"
|
|
echo "GITLAB_TOKEN expires on ${expires_at} (${days_left} days remaining)."
|
|
|
|
if [ "$days_left" -gt "$TOKEN_ROTATE_THRESHOLD_DAYS" ]; then
|
|
echo "Token valid for more than ${TOKEN_ROTATE_THRESHOLD_DAYS} days; no rotation needed."
|
|
return 0
|
|
fi
|
|
|
|
echo "Token expires within ${TOKEN_ROTATE_THRESHOLD_DAYS} days; checking CI/CD variable write access before rotating..."
|
|
|
|
local var_update_url=""
|
|
local group_id=""
|
|
local proj_var_code
|
|
proj_var_code=$(curl -s -o /dev/null -w '%{http_code}' \
|
|
--header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
|
|
"${PROJECT_API}/variables/GITLAB_TOKEN")
|
|
|
|
if [ "$proj_var_code" = "200" ]; then
|
|
var_update_url="${PROJECT_API}/variables/GITLAB_TOKEN"
|
|
else
|
|
local proj_info="${WORK_TMP}/project.json"
|
|
local proj_code
|
|
proj_code=$(curl -s -o "$proj_info" -w '%{http_code}' \
|
|
--header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
|
|
"${PROJECT_API}")
|
|
if [ "$proj_code" = "200" ]; then
|
|
local ns_kind
|
|
ns_kind="$(jq -r '.namespace.kind // empty' "$proj_info")"
|
|
if [ "$ns_kind" = "group" ]; then
|
|
group_id="$(jq -r '.namespace.id // empty' "$proj_info")"
|
|
if [ -n "$group_id" ]; then
|
|
local grp_var_code
|
|
grp_var_code=$(curl -s -o /dev/null -w '%{http_code}' \
|
|
--header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
|
|
"${API_ROOT}/groups/${group_id}/variables/GITLAB_TOKEN")
|
|
if [ "$grp_var_code" = "200" ]; then
|
|
var_update_url="${API_ROOT}/groups/${group_id}/variables/GITLAB_TOKEN"
|
|
fi
|
|
fi
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
if [ -z "$var_update_url" ]; then
|
|
echo "WARNING: GITLAB_TOKEN CI/CD variable is not accessible via API (project check HTTP ${proj_var_code}). Skipping rotation to avoid losing the rotated token."
|
|
return 0
|
|
fi
|
|
|
|
local new_expires_at
|
|
new_expires_at="$(python3 - "$TOKEN_ROTATE_LIFETIME_DAYS" <<'PY'
|
|
from datetime import date, timedelta
|
|
import sys
|
|
days = int(sys.argv[1])
|
|
print((date.today() + timedelta(days=days)).isoformat())
|
|
PY
|
|
)"
|
|
|
|
local rotate_urls=("${PROJECT_API}/access_tokens/self/rotate?expires_at=${new_expires_at}")
|
|
if [ -n "$group_id" ]; then
|
|
rotate_urls+=("${API_ROOT}/groups/${group_id}/access_tokens/self/rotate?expires_at=${new_expires_at}")
|
|
fi
|
|
rotate_urls+=("${API_ROOT}/personal_access_tokens/self/rotate?expires_at=${new_expires_at}")
|
|
|
|
local rotated_token=""
|
|
local rot_file="${WORK_TMP}/rotate_resp.json"
|
|
local rot_code=""
|
|
local url
|
|
for url in "${rotate_urls[@]}"; do
|
|
rot_code=$(curl -s -o "$rot_file" -w '%{http_code}' -X POST \
|
|
--header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
|
|
"$url")
|
|
if [ "$rot_code" = "200" ] || [ "$rot_code" = "201" ]; then
|
|
rotated_token="$(jq -r '.token // empty' "$rot_file")"
|
|
if [ -n "$rotated_token" ]; then
|
|
break
|
|
fi
|
|
fi
|
|
done
|
|
|
|
if [ -z "$rotated_token" ]; then
|
|
echo "ERROR: Failed to self-rotate GITLAB_TOKEN (last HTTP ${rot_code}):" >&2
|
|
jq 'del(.token)' "$rot_file" >&2 || true
|
|
return 1
|
|
fi
|
|
|
|
export GITLAB_TOKEN="$rotated_token"
|
|
|
|
local put_file="${WORK_TMP}/var_put.json"
|
|
local put_code
|
|
put_code=$(jq -n --arg v "${GITLAB_TOKEN}" '{value: $v}' | \
|
|
curl -s -o "$put_file" -w '%{http_code}' -X PUT \
|
|
--header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
|
|
--header "Content-Type: application/json" \
|
|
--data-binary @- \
|
|
"$var_update_url")
|
|
|
|
if [ "$put_code" != "200" ]; then
|
|
echo "ERROR: Rotated GITLAB_TOKEN, but failed to persist new value to CI/CD variable (HTTP ${put_code}):" >&2
|
|
cat "$put_file" >&2
|
|
return 1
|
|
fi
|
|
|
|
echo "Successfully self-rotated GITLAB_TOKEN (new expiry: ${new_expires_at}) and updated CI/CD variable."
|
|
}
|
|
|
|
create_release_tag() {
|
|
local built_version="$1"
|
|
local tag_name="v${built_version}"
|
|
local encoded_tag
|
|
encoded_tag="$(jq -rn --arg t "${tag_name}" '$t|@uri')"
|
|
|
|
local exists_code
|
|
exists_code=$(curl -s -o /dev/null -w '%{http_code}' \
|
|
--header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
|
|
"${PROJECT_API}/repository/tags/${encoded_tag}")
|
|
|
|
if [ "$exists_code" = "200" ]; then
|
|
echo "Tag ${tag_name} already exists; skipping tag creation."
|
|
return 0
|
|
fi
|
|
|
|
local tag_resp="${WORK_TMP}/tag_resp.json"
|
|
local create_code
|
|
create_code=$(jq -n \
|
|
--arg tag "${tag_name}" \
|
|
--arg ref "${CI_COMMIT_SHA}" \
|
|
--arg msg "Release ${tag_name} (pipeline #${CI_PIPELINE_ID:-0})" \
|
|
'{tag_name: $tag, ref: $ref, message: $msg}' | \
|
|
curl -s -o "$tag_resp" -w '%{http_code}' -X POST \
|
|
--header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
|
|
--header "Content-Type: application/json" \
|
|
--data-binary @- \
|
|
"${PROJECT_API}/repository/tags")
|
|
|
|
if [ "$create_code" = "201" ] || [ "$create_code" = "200" ]; then
|
|
echo "Created release tag ${tag_name} at ${CI_COMMIT_SHA}"
|
|
else
|
|
echo "ERROR: Failed to create tag ${tag_name} (HTTP ${create_code})" >&2
|
|
cat "$tag_resp" >&2
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
commit_version_bump() {
|
|
local built_version="$1"
|
|
local encoded_branch
|
|
encoded_branch="$(jq -rn --arg b "${TARGET_BRANCH}" '$b|@uri')"
|
|
|
|
local base_version="$built_version"
|
|
local remote_ver_file="${WORK_TMP}/remote_version.txt"
|
|
local raw_code
|
|
raw_code=$(curl -s -o "$remote_ver_file" -w '%{http_code}' \
|
|
--header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
|
|
"${PROJECT_API}/repository/files/VERSION/raw?ref=${encoded_branch}")
|
|
|
|
if [ "$raw_code" = "200" ]; then
|
|
local remote_version
|
|
remote_version="$(tr -d '[:space:]' < "$remote_ver_file")"
|
|
parse_semver "$remote_version"
|
|
if semver_gt "$remote_version" "$built_version"; then
|
|
echo "Remote VERSION (${remote_version}) is already ahead of built version (${built_version}); skipping VERSION bump commit."
|
|
return 0
|
|
fi
|
|
base_version="$remote_version"
|
|
fi
|
|
|
|
local next_version
|
|
next_version="$(increment_patch "$base_version")"
|
|
|
|
local commit_resp="${WORK_TMP}/commit_resp.json"
|
|
local commit_code
|
|
commit_code=$(jq -n \
|
|
--arg branch "${TARGET_BRANCH}" \
|
|
--arg msg "chore: bump version to ${next_version} [skip ci]" \
|
|
--arg content "${next_version}"$'\n' \
|
|
'{
|
|
branch: $branch,
|
|
commit_message: $msg,
|
|
actions: [{action: "update", file_path: "VERSION", content: $content}]
|
|
}' | \
|
|
curl -s -o "$commit_resp" -w '%{http_code}' -X POST \
|
|
--header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
|
|
--header "Content-Type: application/json" \
|
|
--data-binary @- \
|
|
"${PROJECT_API}/repository/commits")
|
|
|
|
if [ "$commit_code" = "201" ] || [ "$commit_code" = "200" ]; then
|
|
local commit_sha
|
|
commit_sha="$(jq -r '.short_id // .id // "unknown"' "$commit_resp")"
|
|
echo "Bumped VERSION from ${base_version} to ${next_version} on ${TARGET_BRANCH} (${commit_sha})."
|
|
else
|
|
echo "ERROR: Failed to commit VERSION bump to ${TARGET_BRANCH} (HTTP ${commit_code})" >&2
|
|
cat "$commit_resp" >&2
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
BUILT_VERSION="$(tr -d '[:space:]' < VERSION)"
|
|
parse_semver "$BUILT_VERSION"
|
|
|
|
ensure_fresh_token
|
|
create_release_tag "$BUILT_VERSION"
|
|
commit_version_bump "$BUILT_VERSION"
|