-
Notifications
You must be signed in to change notification settings - Fork 8
178 lines (153 loc) · 7.1 KB
/
release.yml
File metadata and controls
178 lines (153 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
name: Release — Version Bump & Publish
# Manually triggered release workflow (via "Run workflow" in GitHub Actions).
# Use the `branch` input to select the source branch for the release (defaults to `main`).
on:
workflow_dispatch:
inputs:
branch:
description: 'Default Branch Name'
required: true
default: 'main'
permissions:
contents: write # needed to push the release branch and create tags
pull-requests: write # needed to open the version-bump PR
jobs:
version-bump:
name: Bump patch version, update docs, open PR & tag
runs-on: ubuntu-latest
# Skip commits that were already made by this workflow (or any bot) to
# avoid triggering an infinite bump loop.
# Also skip merges of the automated release/vX.Y.Z PRs: a regular merge
# produces a commit message starting with "Merge pull request" that also
# references the release/vX.Y.Z branch name.
if: >-
github.actor != 'github-actions[bot]' &&
!contains(github.event.head_commit.message || '', '[skip ci]') &&
!(startsWith(github.event.head_commit.message || '', 'Merge pull request') &&
contains(github.event.head_commit.message || '', 'release/v'))
steps:
- name: Checkout repository (full history for tagging)
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
ref: ${{ inputs.branch }}
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
# ── 1. Compute new patch version ─────────────────────────────────────
- name: Compute and apply patch version bump
id: bump
run: |
python - <<'PYEOF'
import re, sys
path = "cli/__version__.py"
with open(path) as fh:
content = fh.read()
m = re.search(r'__version__\s*=\s*"(\d+)\.(\d+)\.(\d+)"', content)
if not m:
print("ERROR: could not find __version__ in cli/__version__.py", file=sys.stderr)
sys.exit(1)
major, minor, patch = int(m.group(1)), int(m.group(2)), int(m.group(3))
new_version = f"{major}.{minor}.{patch + 1}"
new_content = re.sub(
r'(__version__\s*=\s*)"[^"]+"',
f'\\1"{new_version}"',
content,
)
with open(path, "w") as fh:
fh.write(new_content)
print(f"Bumped {major}.{minor}.{patch} → {new_version}")
# Expose new version to subsequent steps via GITHUB_OUTPUT
import os
with open(os.environ["GITHUB_OUTPUT"], "a") as out:
out.write(f"new_version={new_version}\n")
PYEOF
# ── 2. Update CHANGELOG.md ────────────────────────────────────────────
- name: Prepend CHANGELOG entry for new version
env:
NEW_VERSION: ${{ steps.bump.outputs.new_version }}
run: |
python - <<'PYEOF'
import os
from datetime import date
new_version = os.environ["NEW_VERSION"]
today = date.today().isoformat()
entry = (
f"\n## [{new_version}] - {today}\n\n"
f"### Changed\n"
f"- Automated patch release — version bump to {new_version}.\n\n"
f"---\n"
)
with open("CHANGELOG.md") as fh:
content = fh.read()
# Insert the new entry before the first existing release section
marker = "\n## ["
if marker not in content:
print("WARNING: no existing release section found in CHANGELOG.md; appending entry")
new_content = content + entry
else:
insert_at = content.index(marker)
new_content = content[:insert_at] + entry + content[insert_at:]
with open("CHANGELOG.md", "w") as fh:
fh.write(new_content)
print(f"CHANGELOG updated with {new_version} entry")
PYEOF
# ── 3. Update version badge in Hugo docs index ────────────────────────
- name: Update version badge in Hugo docs
env:
NEW_VERSION: ${{ steps.bump.outputs.new_version }}
run: |
# Replace any existing version-X.Y.Z-blue badge with the new version
sed -i -E \
"s|version-[0-9]+\.[0-9]+\.[0-9]+-blue|version-${NEW_VERSION}-blue|g" \
hugo-docs/content/_index.md
# ── 4. Update version badge in root README ────────────────────────────
- name: Update version badge in root README
env:
NEW_VERSION: ${{ steps.bump.outputs.new_version }}
run: |
# Replace any existing version-X.Y.Z-blue badge with the new version
sed -i -E \
"s|version-[0-9]+\.[0-9]+\.[0-9]+-blue|version-${NEW_VERSION}-blue|g" \
README.md
# ── 5. Open a PR with the version-bump changes ────────────────────────
# peter-evans/create-pull-request creates commits via the GitHub API so
# they are automatically verified (signed), satisfying the branch
# protection rule that requires signed commits. It also opens a PR
# instead of pushing directly to main, satisfying the rule that all
# changes must go through a pull request.
- name: Create pull request for version bump
id: cpr
uses: peter-evans/create-pull-request@v7
with:
token: ${{ secrets.GITHUB_TOKEN }}
branch: release/v${{ steps.bump.outputs.new_version }}
commit-message: "chore: bump version to v${{ steps.bump.outputs.new_version }} [skip ci]"
title: "chore: bump version to v${{ steps.bump.outputs.new_version }}"
body: |
Automated patch version bump to `v${{ steps.bump.outputs.new_version }}`.
- Updates `cli/__version__.py`
- Prepends entry to `CHANGELOG.md`
- Updates version badge in `README.md`
- Updates version badge in `hugo-docs/content/_index.md`
labels: |
release
automated
add-paths: |
cli/__version__.py
CHANGELOG.md
README.md
hugo-docs/content/_index.md
# ── 6. Tag the PR branch's head commit ──────────────────────────────────
# Tag the commit on the release branch (not the local main checkout) so
# the tag always points to the exact commit that carries the version bump.
- name: Create and push git tag
if: steps.cpr.outputs.pull-request-number != ''
env:
NEW_VERSION: ${{ steps.bump.outputs.new_version }}
TAG_SHA: ${{ steps.cpr.outputs.pull-request-head-sha }}
run: |
git tag "v${NEW_VERSION}" "${TAG_SHA}"
git push origin "v${NEW_VERSION}"