fix version bump strategy

This commit is contained in:
2026-03-14 17:56:08 +01:00
parent 6f1913a542
commit 69fe519f7a
2 changed files with 178 additions and 30 deletions

View File

@@ -5,6 +5,10 @@ inputs:
description: Node.js version to use description: Node.js version to use
required: false required: false
default: "24" 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: gitea_token:
description: Token for checkout/push (optional) description: Token for checkout/push (optional)
required: false required: false
@@ -34,18 +38,114 @@ runs:
with: with:
node-version: ${{ inputs.node_version }} node-version: ${{ inputs.node_version }}
- name: Bump patch version and tag - name: Bump version and tag
id: bump id: bump
shell: bash shell: bash
env:
INPUT_RELEASE_LINE: ${{ inputs.release_line }}
run: | 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/}" 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 fetch origin "${BRANCH}" --tags
git checkout "${BRANCH}" git checkout "${BRANCH}"
git pull --ff-only origin "${BRANCH}" git pull --ff-only origin "${BRANCH}"
BRANCH_SAFE="$(echo "${BRANCH}" | tr '/[:space:].' '-' | tr -cd '[:alnum:]_-')"
BRANCH_SAFE="$(echo "${BRANCH_SAFE}" | sed 's/^-*//;s/-*$//')" set_version() {
BRANCH_SAFE="${BRANCH_SAFE:-unknown}" 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: <major>.<minor>" >&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/<major>.<minor>, 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 case "${BRANCH}" in
main) main)
COMMIT_MSG="$(git log -1 --pretty=%B)" COMMIT_MSG="$(git log -1 --pretty=%B)"
@@ -59,42 +159,38 @@ runs:
fi fi
if [[ -n "${TARGET_VERSION}" ]]; then if [[ -n "${TARGET_VERSION}" ]]; then
npm version "${TARGET_VERSION}" --no-git-tag-version --allow-same-version set_version "${TARGET_VERSION}"
BUMP_CMD="npm version patch --no-git-tag-version"
else else
npm version patch --no-git-tag-version npm version patch --no-git-tag-version >/dev/null
BUMP_CMD="npm version patch --no-git-tag-version"
fi fi
;; ;;
develop) release/*)
npm version prerelease --preid=dev --no-git-tag-version RELEASE_LINE="$(resolve_release_line)"
BUMP_CMD="npm version prerelease --preid=dev --no-git-tag-version" RELEASE_BASE="${RELEASE_LINE}.0"
;; set_version "$(next_beta_version "${RELEASE_BASE}")"
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
;; ;;
*) *)
npm version prerelease --preid=nightly-${BRANCH_SAFE} --no-git-tag-version RELEASE_LINE="$(resolve_release_line)"
BUMP_CMD="npm version prerelease --preid=nightly-${BRANCH_SAFE} --no-git-tag-version" RELEASE_BASE="${RELEASE_LINE}.0"
set_version "$(next_dev_version "${RELEASE_BASE}")"
;; ;;
esac esac
VERSION="$(node -p "require('./package.json').version")" VERSION="$(node -p "require('./package.json').version")"
while git rev-parse -q --verify "refs/tags/v${VERSION}" >/dev/null; do while git rev-parse -q --verify "refs/tags/v${VERSION}" >/dev/null; do
echo "Tag v${VERSION} already exists; trying next version." echo "Tag v${VERSION} already exists; trying next version."
${BUMP_CMD} case "${BRANCH}" in
VERSION="$(node -p "require('./package.json').version")" 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 done
if git diff --quiet; then if git diff --quiet; then

View File

@@ -4,6 +4,43 @@ This repo provides reusable Gitea Actions as composite actions that can be refer
### Bump Version ### 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: `<major>.<minor>`
- `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/<major>.<minor>`: 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/<major>.<minor>`
- PR target branch if it matches `release/<major>.<minor>`
- 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.<UTC_TIMESTAMP>`
- feature branch outside a PR with `release_line: 2.4` -> `v2.4.0-dev.<UTC_TIMESTAMP>`
Example: Example:
```yaml ```yaml
@@ -18,6 +55,21 @@ jobs:
gitea_token: ${{ secrets.GITEA_TOKEN }} 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 ### Build and Push Docker Image
Example: Example: