|
| 1 | +name: Release — Version Bump & Publish |
| 2 | + |
| 3 | +# Runs on every direct push to main (human or PR merge). |
| 4 | +# Bot-authored commits are skipped to prevent infinite loops. |
| 5 | +on: |
| 6 | + push: |
| 7 | + branches: [main] |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write # needed to commit the bump and push tags |
| 11 | + |
| 12 | +jobs: |
| 13 | + version-bump: |
| 14 | + name: Bump patch version, update docs, tag & push |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + # Skip commits that were already made by this workflow (or any bot) to |
| 18 | + # avoid triggering an infinite bump loop. |
| 19 | + if: >- |
| 20 | + github.actor != 'github-actions[bot]' && |
| 21 | + !contains(github.event.head_commit.message, '[skip ci]') |
| 22 | +
|
| 23 | + steps: |
| 24 | + - name: Checkout repository (full history for tagging) |
| 25 | + uses: actions/checkout@v4 |
| 26 | + with: |
| 27 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 28 | + fetch-depth: 0 |
| 29 | + |
| 30 | + - name: Set up Python |
| 31 | + uses: actions/setup-python@v5 |
| 32 | + with: |
| 33 | + python-version: "3.11" |
| 34 | + |
| 35 | + # ── 1. Compute new patch version ───────────────────────────────────── |
| 36 | + - name: Compute and apply patch version bump |
| 37 | + id: bump |
| 38 | + run: | |
| 39 | + python - <<'PYEOF' |
| 40 | + import re, sys |
| 41 | +
|
| 42 | + path = "cli/__version__.py" |
| 43 | + with open(path) as fh: |
| 44 | + content = fh.read() |
| 45 | +
|
| 46 | + m = re.search(r'__version__\s*=\s*"(\d+)\.(\d+)\.(\d+)"', content) |
| 47 | + if not m: |
| 48 | + print("ERROR: could not find __version__ in cli/__version__.py", file=sys.stderr) |
| 49 | + sys.exit(1) |
| 50 | +
|
| 51 | + major, minor, patch = int(m.group(1)), int(m.group(2)), int(m.group(3)) |
| 52 | + new_version = f"{major}.{minor}.{patch + 1}" |
| 53 | +
|
| 54 | + new_content = re.sub( |
| 55 | + r'(__version__\s*=\s*)"[^"]+"', |
| 56 | + f'\\1"{new_version}"', |
| 57 | + content, |
| 58 | + ) |
| 59 | + with open(path, "w") as fh: |
| 60 | + fh.write(new_content) |
| 61 | +
|
| 62 | + print(f"Bumped {major}.{minor}.{patch} → {new_version}") |
| 63 | +
|
| 64 | + # Expose new version to subsequent steps via GITHUB_OUTPUT |
| 65 | + import os |
| 66 | + with open(os.environ["GITHUB_OUTPUT"], "a") as out: |
| 67 | + out.write(f"new_version={new_version}\n") |
| 68 | + PYEOF |
| 69 | +
|
| 70 | + # ── 2. Update CHANGELOG.md ──────────────────────────────────────────── |
| 71 | + - name: Prepend CHANGELOG entry for new version |
| 72 | + env: |
| 73 | + NEW_VERSION: ${{ steps.bump.outputs.new_version }} |
| 74 | + run: | |
| 75 | + python - <<'PYEOF' |
| 76 | + import os |
| 77 | + from datetime import date |
| 78 | +
|
| 79 | + new_version = os.environ["NEW_VERSION"] |
| 80 | + today = date.today().isoformat() |
| 81 | +
|
| 82 | + entry = ( |
| 83 | + f"\n## [{new_version}] - {today}\n\n" |
| 84 | + f"### Changed\n" |
| 85 | + f"- Automated patch release — version bump to {new_version}.\n\n" |
| 86 | + f"---\n" |
| 87 | + ) |
| 88 | +
|
| 89 | + with open("CHANGELOG.md") as fh: |
| 90 | + content = fh.read() |
| 91 | +
|
| 92 | + # Insert the new entry before the first existing release section |
| 93 | + marker = "\n## [" |
| 94 | + if marker not in content: |
| 95 | + print("WARNING: no existing release section found in CHANGELOG.md; appending entry") |
| 96 | + new_content = content + entry |
| 97 | + else: |
| 98 | + insert_at = content.index(marker) |
| 99 | + new_content = content[:insert_at] + entry + content[insert_at:] |
| 100 | +
|
| 101 | + with open("CHANGELOG.md", "w") as fh: |
| 102 | + fh.write(new_content) |
| 103 | +
|
| 104 | + print(f"CHANGELOG updated with {new_version} entry") |
| 105 | + PYEOF |
| 106 | +
|
| 107 | + # ── 3. Update version badge in Hugo docs index ──────────────────────── |
| 108 | + - name: Update version badge in Hugo docs |
| 109 | + env: |
| 110 | + NEW_VERSION: ${{ steps.bump.outputs.new_version }} |
| 111 | + run: | |
| 112 | + # Replace any existing version-X.Y.Z-blue badge with the new version |
| 113 | + sed -i -E \ |
| 114 | + "s|version-[0-9]+\.[0-9]+\.[0-9]+-blue|version-${NEW_VERSION}-blue|g" \ |
| 115 | + hugo-docs/content/_index.md |
| 116 | +
|
| 117 | + # ── 4. Commit, tag, and push ────────────────────────────────────────── |
| 118 | + - name: Commit version bump and create git tag |
| 119 | + env: |
| 120 | + NEW_VERSION: ${{ steps.bump.outputs.new_version }} |
| 121 | + run: | |
| 122 | + git config user.name "github-actions[bot]" |
| 123 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 124 | +
|
| 125 | + git add cli/__version__.py CHANGELOG.md hugo-docs/content/_index.md |
| 126 | + git commit -m "chore: bump version to v${NEW_VERSION} [skip ci]" |
| 127 | + git tag "v${NEW_VERSION}" |
| 128 | + git push origin main |
| 129 | + git push origin "v${NEW_VERSION}" |
0 commit comments