Skip to content

ci: simplify auto release workflow guard (#6) #6

ci: simplify auto release workflow guard (#6)

ci: simplify auto release workflow guard (#6) #6

Workflow file for this run

name: Auto Release
"on":
push:
branches: [main]
workflow_dispatch:
permissions:
contents: read
concurrency:
group: auto-release-main
cancel-in-progress: false
jobs:
decide:
name: Decide release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Decide next beta version
id: version
shell: bash
env:
EVENT_NAME: ${{ github.event_name }}
HEAD_COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
run: |
set -euo pipefail
if [[ "$EVENT_NAME" == "push" ]]; then
case "$HEAD_COMMIT_MESSAGE" in
"chore(release): prepare "*)
echo "Release preparation commit detected; auto release skipped."
echo "publish=false" >> "$GITHUB_OUTPUT"
exit 0
;;
esac
fi
latest_tag=$(git tag --list 'v*-beta.*' --sort=-version:refname | head -n 1)
if [[ -z "$latest_tag" ]]; then
echo "No beta tag found; starting from v0.1-beta.1."
latest_tag="v0.0-beta.0"
range="HEAD"
else
range="${latest_tag}..HEAD"
fi
subjects=$(git log --format=%s "$range" | grep -Ev '^(chore\(release\): prepare v|Merge pull request)' || true)
bodies=$(git log --format=%B "$range")
if [[ -z "$subjects" ]]; then
echo "No non-release commits found since $latest_tag."
echo "publish=false" >> "$GITHUB_OUTPUT"
exit 0
fi
bump=""
if printf '%s\n' "$subjects" | grep -Eq '^[a-z]+(\([^)]+\))?!:' || printf '%s\n' "$bodies" | grep -Eq '^BREAKING CHANGE:'; then
bump="major"
elif printf '%s\n' "$subjects" | grep -Eq '^feat(\([^)]+\))?:'; then
bump="minor"
elif printf '%s\n' "$subjects" | grep -Eq '^(fix|perf)(\([^)]+\))?:'; then
bump="patch"
fi
if [[ -z "$bump" ]]; then
echo "No releasable Conventional Commit type found since $latest_tag."
echo "publish=false" >> "$GITHUB_OUTPUT"
exit 0
fi
if [[ ! "$latest_tag" =~ ^v([0-9]+)\.([0-9]+)-beta\.([0-9]+)$ ]]; then
echo "Latest beta tag has unsupported format: $latest_tag"
exit 1
fi
major="${BASH_REMATCH[1]}"
minor="${BASH_REMATCH[2]}"
beta="${BASH_REMATCH[3]}"
case "$bump" in
major)
major=$((major + 1))
minor=0
beta=1
;;
minor)
minor=$((minor + 1))
beta=1
;;
patch)
beta=$((beta + 1))
;;
esac
tag="v${major}.${minor}-beta.${beta}"
package_version="${major}.${minor}.0-beta.${beta}"
if git ls-remote --exit-code --tags origin "refs/tags/$tag" >/dev/null 2>&1; then
echo "Computed tag already exists: $tag"
echo "publish=false" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "publish=true" >> "$GITHUB_OUTPUT"
echo "tag=$tag" >> "$GITHUB_OUTPUT"
echo "package_version=$package_version" >> "$GITHUB_OUTPUT"
echo "bump=$bump" >> "$GITHUB_OUTPUT"
echo "latest_tag=$latest_tag" >> "$GITHUB_OUTPUT"
- name: Validate release token
if: ${{ steps.version.outputs.publish == 'true' }}
env:
RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
run: |
if [[ -z "$RELEASE_TOKEN" ]]; then
echo "RELEASE_TOKEN secret is required to dispatch the Release workflow."
exit 1
fi
- name: Dispatch release workflow
if: ${{ steps.version.outputs.publish == 'true' }}
env:
GH_TOKEN: ${{ secrets.RELEASE_TOKEN }}
RELEASE_TAG: ${{ steps.version.outputs.tag }}
PACKAGE_VERSION: ${{ steps.version.outputs.package_version }}
run: |
gh workflow run release.yml \
--ref main \
-f tag="$RELEASE_TAG" \
-f package_version="$PACKAGE_VERSION" \
-f prerelease=true
- name: Report skipped release
if: ${{ steps.version.outputs.publish != 'true' }}
run: echo "Auto release skipped."