This commit is contained in:
Agel committed 2026-11-19 13:54:40 +01:00
1 parent 4b3a959654
commit b9dc1fe515
3 files changed
+64 -81

No files matched your search

+43 -71
View File
@@ -3,8 +3,8 @@
# 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, commits with [skip ci], and pushes
# to the target branch (retrying on concurrent pushes).
# 3. Increments patch version in VERSION on the target branch via GitLab
# Repository Commits API with [skip ci].
set -euo pipefail
@@ -36,21 +36,11 @@ API_ROOT="${CI_API_V4_URL:-https://gitlab.com/api/v4}"
PROJECT_API="${API_ROOT}/projects/${CI_PROJECT_ID}"
WORK_TMP="$(mktemp -d)"
ASKPASS_SCRIPT="${WORK_TMP}/git-askpass.sh"
cleanup() {
rm -rf "$WORK_TMP"
}
trap cleanup EXIT
cat > "$ASKPASS_SCRIPT" <<'EOF'
#!/bin/sh
case "$1" in
*Username*|*username*) printf '%s\n' "oauth2" ;;
*) printf '%s\n' "${GITLAB_TOKEN}" ;;
esac
EOF
chmod 700 "$ASKPASS_SCRIPT"
parse_semver() {
local v="$1"
if ! [[ "$v" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
@@ -253,76 +243,58 @@ create_release_tag() {
fi
}
bump_and_commit_version() {
commit_version_bump() {
local built_version="$1"
local encoded_branch
encoded_branch="$(jq -rn --arg b "${TARGET_BRANCH}" '$b|@uri')"
local remote_url="${BUMP_GIT_REMOTE:-}"
if [ -z "$remote_url" ]; then
if [ -z "${CI_SERVER_HOST:-}" ] || [ -z "${CI_PROJECT_PATH:-}" ]; then
echo "ERROR: CI_SERVER_HOST and CI_PROJECT_PATH must be set for git push" >&2
return 1
fi
remote_url="${CI_SERVER_PROTOCOL:-https}://${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git"
fi
git config user.name "${GITLAB_USER_NAME:-Asteria CI}"
git config user.email "${GITLAB_USER_EMAIL:-ci@asteria-bookreader.local}"
local push_opts=()
if [[ "$remote_url" == http://* ]] || [[ "$remote_url" == https://* ]]; then
push_opts+=(-o ci.skip)
fi
local next_version
next_version="$(increment_patch "$built_version")"
printf '%s\n' "$next_version" > VERSION
if git diff --quiet VERSION; then
echo "VERSION already at ${next_version}; nothing to commit."
return 0
fi
git add VERSION
git commit -m "chore: bump version to ${next_version} [skip ci]"
if GIT_ASKPASS="$ASKPASS_SCRIPT" GIT_TERMINAL_PROMPT=0 \
git push ${push_opts[@]+"${push_opts[@]}"} "$remote_url" "HEAD:refs/heads/${TARGET_BRANCH}"; then
echo "Bumped VERSION from ${built_version} to ${next_version} and pushed to ${TARGET_BRANCH}."
return 0
fi
# Fallback only if origin moved during the pipeline run
local attempt
for attempt in 1 2; do
echo "Push rejected (remote ${TARGET_BRANCH} moved); fetching latest and retrying (${attempt}/2)..."
GIT_ASKPASS="$ASKPASS_SCRIPT" GIT_TERMINAL_PROMPT=0 \
git fetch "$remote_url" "$TARGET_BRANCH"
git checkout -f -B "$TARGET_BRANCH" FETCH_HEAD
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:]' < 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
next_version="$(increment_patch "$remote_version")"
printf '%s\n' "$next_version" > VERSION
git add VERSION
git commit -m "chore: bump version to ${next_version} [skip ci]"
local next_version
next_version="$(increment_patch "$base_version")"
if GIT_ASKPASS="$ASKPASS_SCRIPT" GIT_TERMINAL_PROMPT=0 \
git push ${push_opts[@]+"${push_opts[@]}"} "$remote_url" "HEAD:refs/heads/${TARGET_BRANCH}"; then
echo "Bumped VERSION from ${remote_version} to ${next_version} and pushed to ${TARGET_BRANCH}."
return 0
fi
sleep 2
done
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")
echo "ERROR: Failed to push bumped VERSION to ${TARGET_BRANCH}." >&2
return 1
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)"
@@ -330,4 +302,4 @@ parse_semver "$BUILT_VERSION"
ensure_fresh_token
create_release_tag "$BUILT_VERSION"
bump_and_commit_version "$BUILT_VERSION"
commit_version_bump "$BUILT_VERSION"