|
| 1 | +name: Semantic Versioning |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ develop ] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: write |
| 9 | + |
| 10 | +concurrency: |
| 11 | + group: semver-${{ github.ref }} |
| 12 | + cancel-in-progress: false |
| 13 | + |
| 14 | +env: |
| 15 | + VERSION_FILE: package.json |
| 16 | + |
| 17 | +jobs: |
| 18 | + release: |
| 19 | + # Extra safety: don't run on the bot's own version bump commits (even though [skip ci] should skip already) |
| 20 | + if: github.actor != 'github-actions[bot]' |
| 21 | + runs-on: ubuntu-latest |
| 22 | + |
| 23 | + steps: |
| 24 | + - name: Checkout |
| 25 | + uses: actions/checkout@v4 |
| 26 | + with: |
| 27 | + fetch-depth: 0 # fetch all history + tags |
| 28 | + |
| 29 | + - name: Configure git |
| 30 | + run: | |
| 31 | + git config user.name "github-actions[bot]" |
| 32 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 33 | +
|
| 34 | + - name: Fetch tags |
| 35 | + run: git fetch --force --tags |
| 36 | + |
| 37 | + # Bootstrap only when there are no semver tags yet |
| 38 | + - name: Bootstrap initial tag (only if none exist) |
| 39 | + id: bootstrap |
| 40 | + shell: bash |
| 41 | + run: | |
| 42 | + set -euo pipefail |
| 43 | +
|
| 44 | + latest="$(git tag -l 'v[0-9]*.[0-9]*.[0-9]*' --sort=-v:refname | head -n1 || true)" |
| 45 | + if [[ -n "$latest" ]]; then |
| 46 | + echo "bootstrapped=false" >> "$GITHUB_OUTPUT" |
| 47 | + exit 0 |
| 48 | + fi |
| 49 | +
|
| 50 | + if [[ ! -f "$VERSION_FILE" ]]; then |
| 51 | + echo "VERSION_FILE not found: $VERSION_FILE" >&2 |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | +
|
| 55 | + detected="$(grep -oE "v?[0-9]+\.[0-9]+\.[0-9]+" "$VERSION_FILE" | head -n1 || true)" |
| 56 | + if [[ -z "$detected" ]]; then |
| 57 | + echo "No semver found in $VERSION_FILE" >&2 |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | + [[ "$detected" == v* ]] || detected="v$detected" |
| 61 | +
|
| 62 | + echo "No semver tags found. Bootstrapping tag: $detected" |
| 63 | + git tag -a "$detected" -m "Bootstrap $detected" |
| 64 | + git push origin "$detected" |
| 65 | +
|
| 66 | + echo "bootstrapped=true" >> "$GITHUB_OUTPUT" |
| 67 | +
|
| 68 | + - name: Calculate next version |
| 69 | + if: steps.bootstrap.outputs.bootstrapped != 'true' |
| 70 | + id: semver |
| 71 | + uses: paulhatch/semantic-version@v5.4.0 |
| 72 | + with: |
| 73 | + tag_prefix: "v" |
| 74 | + # Conventional Commits defaults (major if !: or BREAKING CHANGE:, minor if feat:) |
| 75 | + major_pattern: "/!:|BREAKING CHANGE:/" |
| 76 | + minor_pattern: "/feat:/" |
| 77 | + search_commit_body: true |
| 78 | + version_format: "${major}.${minor}.${patch}" |
| 79 | + |
| 80 | + - name: Guard (do nothing if tag already exists) |
| 81 | + if: steps.bootstrap.outputs.bootstrapped != 'true' |
| 82 | + id: guard |
| 83 | + shell: bash |
| 84 | + run: | |
| 85 | + set -euo pipefail |
| 86 | + tag="${{ steps.semver.outputs.version_tag }}" |
| 87 | + if git show-ref --tags --verify --quiet "refs/tags/$tag"; then |
| 88 | + echo "Tag already exists: $tag" |
| 89 | + echo "should_release=false" >> "$GITHUB_OUTPUT" |
| 90 | + exit 0 |
| 91 | + fi |
| 92 | + echo "should_release=true" >> "$GITHUB_OUTPUT" |
| 93 | +
|
| 94 | + - name: Update version file |
| 95 | + if: steps.guard.outputs.should_release == 'true' |
| 96 | + env: |
| 97 | + NEW_TAG: ${{ steps.semver.outputs.version_tag }} |
| 98 | + run: | |
| 99 | + set -euo pipefail |
| 100 | + python - <<'PY' |
| 101 | + import json, os |
| 102 | + from pathlib import Path |
| 103 | +
|
| 104 | + path = Path(os.environ["VERSION_FILE"]) |
| 105 | + new_tag = os.environ["NEW_TAG"] |
| 106 | + # Strip leading 'v' for package.json (uses bare semver) |
| 107 | + version = new_tag.lstrip("v") |
| 108 | +
|
| 109 | + data = json.loads(path.read_text(encoding="utf-8")) |
| 110 | + data["version"] = version |
| 111 | + path.write_text(json.dumps(data, indent=2) + "\n", encoding="utf-8") |
| 112 | + print(f"Updated {path} -> {version}") |
| 113 | + PY |
| 114 | +
|
| 115 | + - name: Commit + tag + push |
| 116 | + if: steps.guard.outputs.should_release == 'true' |
| 117 | + run: | |
| 118 | + set -euo pipefail |
| 119 | + tag="${{ steps.semver.outputs.version_tag }}" |
| 120 | +
|
| 121 | + git add "$VERSION_FILE" |
| 122 | + if ! git diff --cached --quiet; then |
| 123 | + # GitHub supports skipping workflow runs via commit message keywords |
| 124 | + git commit -m "chore(release): ${tag} [skip ci]" |
| 125 | + fi |
| 126 | +
|
| 127 | + git tag -a "$tag" -m "Release $tag" |
| 128 | +
|
| 129 | + # Push commit (if any) and annotated tags together |
| 130 | + git push origin HEAD:main --follow-tags |
| 131 | +
|
| 132 | + - name: Summary |
| 133 | + if: steps.bootstrap.outputs.bootstrapped != 'true' |
| 134 | + run: | |
| 135 | + echo "Previous Version: ${{ steps.semver.outputs.previous_version }}" |
| 136 | + echo "New Tag: ${{ steps.semver.outputs.version_tag }}" |
| 137 | + echo "Version Type: ${{ steps.semver.outputs.version_type }}" |
0 commit comments