From 69fe519f7a9da123ad580d5ddcb2886fb441b833 Mon Sep 17 00:00:00 2001 From: Torsten Ueberschar Date: Sat, 14 Mar 2026 17:56:08 +0100 Subject: [PATCH] fix version bump strategy --- .github/actions/bump-version/action.yaml | 156 ++++++++++++++++++----- README.md | 52 ++++++++ 2 files changed, 178 insertions(+), 30 deletions(-) diff --git a/.github/actions/bump-version/action.yaml b/.github/actions/bump-version/action.yaml index 76875ea..33b56ec 100644 --- a/.github/actions/bump-version/action.yaml +++ b/.github/actions/bump-version/action.yaml @@ -5,6 +5,10 @@ inputs: description: Node.js version to use required: false default: "24" + release_line: + description: Release line for dev builds (e.g. 2.4) when it cannot be derived from the branch or PR target + required: false + default: "" gitea_token: description: Token for checkout/push (optional) required: false @@ -34,18 +38,114 @@ runs: with: node-version: ${{ inputs.node_version }} - - name: Bump patch version and tag + - name: Bump version and tag id: bump shell: bash + env: + INPUT_RELEASE_LINE: ${{ inputs.release_line }} run: | - REF="${GITHUB_REF:-${GITEA_REF}}" + set -euo pipefail + + REF="${GITHUB_HEAD_REF:-${GITEA_HEAD_REF:-${GITHUB_REF:-${GITEA_REF:-}}}}" + BASE_REF="${GITHUB_BASE_REF:-${GITEA_BASE_REF:-}}" BRANCH="${REF#refs/heads/}" + BASE_BRANCH="${BASE_REF#refs/heads/}" + + if [[ -z "${BRANCH}" ]]; then + echo "Unable to determine branch from workflow context." >&2 + exit 1 + fi + git fetch origin "${BRANCH}" --tags git checkout "${BRANCH}" git pull --ff-only origin "${BRANCH}" - BRANCH_SAFE="$(echo "${BRANCH}" | tr '/[:space:].' '-' | tr -cd '[:alnum:]_-')" - BRANCH_SAFE="$(echo "${BRANCH_SAFE}" | sed 's/^-*//;s/-*$//')" - BRANCH_SAFE="${BRANCH_SAFE:-unknown}" + + set_version() { + npm version "$1" --no-git-tag-version --allow-same-version >/dev/null + } + + read_package_version() { + node -p "require('./package.json').version" + } + + validate_release_line() { + local line="$1" + [[ "${line}" =~ ^[0-9]+\.[0-9]+$ ]] + } + + extract_release_line() { + local ref_name="$1" + if [[ "${ref_name}" =~ ^release/v?([0-9]+)\.([0-9]+)$ ]]; then + echo "${BASH_REMATCH[1]}.${BASH_REMATCH[2]}" + return 0 + fi + return 1 + } + + resolve_release_line() { + if [[ -n "${INPUT_RELEASE_LINE}" ]]; then + if ! validate_release_line "${INPUT_RELEASE_LINE}"; then + echo "Invalid release_line input '${INPUT_RELEASE_LINE}'. Expected format: ." >&2 + exit 1 + fi + echo "${INPUT_RELEASE_LINE}" + return 0 + fi + + if extract_release_line "${BRANCH}" >/dev/null; then + extract_release_line "${BRANCH}" + return 0 + fi + + if [[ -n "${BASE_BRANCH}" ]] && extract_release_line "${BASE_BRANCH}" >/dev/null; then + extract_release_line "${BASE_BRANCH}" + return 0 + fi + + if [[ "${BRANCH}" == "develop" ]]; then + local package_version + package_version="$(read_package_version)" + if [[ "${package_version}" =~ ^([0-9]+)\.([0-9]+)\.[0-9]+([-.].*)?$ ]]; then + echo "${BASH_REMATCH[1]}.${BASH_REMATCH[2]}" + return 0 + fi + fi + + echo "Unable to determine release line for branch '${BRANCH}'. Use release/., run in a PR targeting such a branch, or provide inputs.release_line." >&2 + exit 1 + } + + next_beta_version() { + local release_base="$1" + local escaped_base + local highest_beta + escaped_base="$(printf '%s' "${release_base}" | sed 's/\./\\./g')" + highest_beta="$( + git tag --list "v${release_base}-beta.*" \ + | sed -nE "s/^v${escaped_base}-beta\.([0-9]+)$/\\1/p" \ + | sort -n \ + | tail -n 1 + )" + if [[ -z "${highest_beta}" ]]; then + echo "${release_base}-beta.0" + else + echo "${release_base}-beta.$((highest_beta + 1))" + fi + } + + next_dev_version() { + local release_base="$1" + local timestamp + while true; do + timestamp="$(date -u +%Y%m%d%H%M%S)" + if ! git rev-parse -q --verify "refs/tags/v${release_base}-dev.${timestamp}" >/dev/null; then + echo "${release_base}-dev.${timestamp}" + return 0 + fi + sleep 1 + done + } + case "${BRANCH}" in main) COMMIT_MSG="$(git log -1 --pretty=%B)" @@ -59,42 +159,38 @@ runs: fi if [[ -n "${TARGET_VERSION}" ]]; then - npm version "${TARGET_VERSION}" --no-git-tag-version --allow-same-version - BUMP_CMD="npm version patch --no-git-tag-version" + set_version "${TARGET_VERSION}" else - npm version patch --no-git-tag-version - BUMP_CMD="npm version patch --no-git-tag-version" + npm version patch --no-git-tag-version >/dev/null fi ;; - develop) - npm version prerelease --preid=dev --no-git-tag-version - BUMP_CMD="npm version prerelease --preid=dev --no-git-tag-version" - ;; - release*) - RELEASE_VERSION="" - if [[ "${BRANCH}" =~ ^release/v?([0-9]+\.[0-9]+\.[0-9]+)$ ]]; then - RELEASE_VERSION="${BASH_REMATCH[1]}" - fi - - if [[ -n "${RELEASE_VERSION}" ]]; then - npm version "${RELEASE_VERSION}-beta.0" --no-git-tag-version --allow-same-version - BUMP_CMD="npm version prerelease --preid=beta --no-git-tag-version" - else - npm version prerelease --preid=beta-${BRANCH_SAFE} --no-git-tag-version - BUMP_CMD="npm version prerelease --preid=beta-${BRANCH_SAFE} --no-git-tag-version" - fi + release/*) + RELEASE_LINE="$(resolve_release_line)" + RELEASE_BASE="${RELEASE_LINE}.0" + set_version "$(next_beta_version "${RELEASE_BASE}")" ;; *) - npm version prerelease --preid=nightly-${BRANCH_SAFE} --no-git-tag-version - BUMP_CMD="npm version prerelease --preid=nightly-${BRANCH_SAFE} --no-git-tag-version" + RELEASE_LINE="$(resolve_release_line)" + RELEASE_BASE="${RELEASE_LINE}.0" + set_version "$(next_dev_version "${RELEASE_BASE}")" ;; esac VERSION="$(node -p "require('./package.json').version")" while git rev-parse -q --verify "refs/tags/v${VERSION}" >/dev/null; do echo "Tag v${VERSION} already exists; trying next version." - ${BUMP_CMD} - VERSION="$(node -p "require('./package.json').version")" + case "${BRANCH}" in + main) + npm version patch --no-git-tag-version >/dev/null + ;; + release/*) + set_version "$(next_beta_version "${RELEASE_BASE}")" + ;; + *) + set_version "$(next_dev_version "${RELEASE_BASE}")" + ;; + esac + VERSION="$(read_package_version)" done if git diff --quiet; then diff --git a/README.md b/README.md index b3fb320..ac9f344 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,43 @@ This repo provides reusable Gitea Actions as composite actions that can be refer ### Bump Version +Creates or updates the version in `package.json`, commits it, creates a matching Git tag, and pushes both. + +Inputs: + +- `node_version`: Node.js version to use. Default: `24` +- `release_line`: optional release line for dev builds when it cannot be derived from the branch or PR target. Format: `.` +- `gitea_token`: optional token used for checkout and push + +Branch and tag rules: + +- `main`: creates release tags from the latest commit message (`release/x.y.z`, `release/x.y`, `hotfix/x.y.z`) or falls back to a patch bump. +- `release/.`: creates the next beta tag for that release line, e.g. `v2.4.0-beta.3`. +- all other branches: create a dev tag for the resolved release line using a UTC timestamp, e.g. `v2.4.0-dev.20260314153045`. + +Dev release line resolution order: + +- `inputs.release_line` +- current branch if it matches `release/.` +- PR target branch if it matches `release/.` +- on `develop`, fallback to `major.minor` from `package.json` + +If no release line can be resolved for a dev build, the action fails intentionally. + +Examples: + +Commit message examples on `main`: + +- `release/2.4.0` -> `v2.4.0` +- `release/2.4` -> `v2.4.0` +- `hotfix/2.4.1` -> `v2.4.1` + +Branch examples: + +- `release/2.4` -> next `v2.4.0-beta.N` +- feature branch in a PR to `release/2.4` -> `v2.4.0-dev.` +- feature branch outside a PR with `release_line: 2.4` -> `v2.4.0-dev.` + Example: ```yaml @@ -18,6 +55,21 @@ jobs: gitea_token: ${{ secrets.GITEA_TOKEN }} ``` +Feature branch outside a PR: + +```yaml +jobs: + bump-version: + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: tanztee/ci-cd/.github/actions/bump-version@main + with: + gitea_token: ${{ secrets.GITEA_TOKEN }} + release_line: 2.4 +``` + ### Build and Push Docker Image Example: