diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 8aeef92..b1d93d7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -435,11 +435,16 @@ update-wiki: tags: - linux-x86_64 needs: - - package-linux - - package-linux-arm64 - - package-windows - - package-windows-arm64 + - job: package-linux + artifacts: false + - job: package-linux-arm64 + artifacts: false + - job: package-windows + artifacts: false + - job: package-windows-arm64 + artifacts: false - job: package-macos + artifacts: false optional: true script: - | @@ -505,13 +510,19 @@ bump-version: tags: - linux-x86_64 needs: - - package-linux - - package-linux-arm64 - - package-windows - - package-windows-arm64 + - job: package-linux + artifacts: false + - job: package-linux-arm64 + artifacts: false + - job: package-windows + artifacts: false + - job: package-windows-arm64 + artifacts: false - job: package-macos + artifacts: false optional: true - - update-wiki + - job: update-wiki + artifacts: false script: - chmod +x scripts/bump-version.sh - ./scripts/bump-version.sh diff --git a/AGENTS.md b/AGENTS.md index 18e7204..0833029 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -165,7 +165,7 @@ scripts/start-runner-macos.sh - At the end of a successful `master` pipeline (`release` stage, after packaging and `update-wiki`), `bump-version` runs `scripts/bump-version.sh`: 1. Checks `GITLAB_TOKEN` expiration via GitLab API and, if within `TOKEN_ROTATE_THRESHOLD_DAYS` (default `30`) and the `GITLAB_TOKEN` CI/CD variable is API-writable, self-rotates the token and updates the CI/CD variable automatically. 2. Creates release tag `vX.Y.Z` at `CI_COMMIT_SHA` via the GitLab Tags API (if absent). - 3. Increments `PATCH` in `VERSION` (`X.Y.(Z+1)`), commits `chore: bump version to ... [skip ci]`, and pushes to `master` with `-o ci.skip`. Tag pipelines (`$CI_COMMIT_TAG`) are skipped in `workflow: rules:` to prevent loops. + 3. Increments `PATCH` in `VERSION` (`X.Y.(Z+1)`), commits `chore: bump version to ... [skip ci]` on `master` via the GitLab Repository Commits API (skipping if remote `VERSION` is already ahead). Tag pipelines (`$CI_COMMIT_TAG`) are skipped in `workflow: rules:` to prevent loops. ### VS Code & CMake Presets Pre-configured tasks, launch configurations, and CMake presets are available: diff --git a/scripts/bump-version.sh b/scripts/bump-version.sh index ba59cd3..3cceeb4 100755 --- a/scripts/bump-version.sh +++ b/scripts/bump-version.sh @@ -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 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"