-
Notifications
You must be signed in to change notification settings - Fork 0
218 lines (182 loc) · 8.78 KB
/
prepare_release.yml
File metadata and controls
218 lines (182 loc) · 8.78 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
name: Prepare Release
# Run manually from the Actions tab.
# Creates a branch + PR that bumps the version, builds the changelog,
# and updates the docs switcher — ready to review before tagging.
on:
workflow_dispatch:
inputs:
bump:
description: "Version component to bump"
required: true
type: choice
options:
- minor
- bugfix
- major
- pre-release # increments the bN counter on the current base version
beta:
description: "Mark as beta pre-release (adds bN suffix; always true for pre-release)"
required: false
type: boolean
default: false
permissions:
contents: write # push branch
pull-requests: write # open PR
jobs:
prepare:
name: Prepare release PR
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up uv
uses: astral-sh/setup-uv@v5
with:
python-version: "3.13"
enable-cache: true
- name: Install dev dependencies
run: uv sync
# ── Compute the new version ──────────────────────────────────────────
- name: Compute new version
id: version
env:
BUMP: ${{ inputs.bump }}
IS_BETA: ${{ inputs.beta }}
run: |
CURRENT=$(grep '^version = ' pyproject.toml | sed 's/version = "\(.*\)"/\1/')
export CURRENT_VERSION="$CURRENT"
NEW_VERSION=$(python3 - <<'PYEOF'
import re, os
current = os.environ["CURRENT_VERSION"]
bump = os.environ["BUMP"]
is_beta = os.environ["IS_BETA"].lower() == "true"
m = re.match(r"^(\d+)\.(\d+)\.(\d+)(?:b(\d+))?", current)
major = int(m.group(1))
minor = int(m.group(2))
patch = int(m.group(3))
beta_n = int(m.group(4)) if m.group(4) else None
if bump == "major":
major, minor, patch = major + 1, 0, 0
elif bump == "minor":
minor, patch = minor + 1, 0
elif bump == "bugfix":
patch += 1
elif bump == "pre-release":
# Keep the same base; just walk the beta counter forward.
is_beta = True
beta_n = (beta_n or 0) + 1
if is_beta:
if bump != "pre-release":
beta_n = 1 # fresh beta series for the new base
print(f"{major}.{minor}.{patch}b{beta_n}", end="")
else:
print(f"{major}.{minor}.{patch}", end="")
PYEOF
)
echo "new_version=$NEW_VERSION" >> "$GITHUB_OUTPUT"
echo "tag=v$NEW_VERSION" >> "$GITHUB_OUTPUT"
echo "branch=release/v$NEW_VERSION" >> "$GITHUB_OUTPUT"
echo "is_beta=${{ inputs.beta }}" >> "$GITHUB_OUTPUT"
echo "Bumping (${{ inputs.bump }}): $CURRENT → $NEW_VERSION"
# ── Bump version strings ─────────────────────────────────────────────
- name: Bump version in pyproject.toml
run: |
sed -i 's/^version = ".*"/version = "${{ steps.version.outputs.new_version }}"/' pyproject.toml
- name: Bump version in docs/conf.py
run: |
sed -i 's/^release = ".*"/release = "${{ steps.version.outputs.new_version }}"/' docs/conf.py
# ── Build changelog ──────────────────────────────────────────────────
- name: Build changelog with towncrier
run: |
FRAGMENT_COUNT=$(find upcoming_changes -maxdepth 1 -name "*.rst" \
! -name "README.rst" | wc -l)
if [ "$FRAGMENT_COUNT" -eq 0 ]; then
echo "⚠ No news fragments found — skipping towncrier (CHANGELOG.rst unchanged)."
else
uvx towncrier build --yes --version "${{ steps.version.outputs.new_version }}"
fi
# ── Update docs switcher.json ────────────────────────────────────────
- name: Update docs/switcher.json
env:
VERSION_TAG: ${{ steps.version.outputs.tag }}
IS_BETA: ${{ inputs.beta }}
shell: python
run: |
import json, re, pathlib, os
version = os.environ["VERSION_TAG"]
is_beta = os.environ["IS_BETA"].lower() == "true"
path = pathlib.Path("docs/_root/switcher.json")
text = path.read_text()
# The file may contain a trailing comma; strip it before parsing.
text_clean = re.sub(r",(\s*[\]\}])", r"\1", text)
entries = json.loads(text_clean)
# Remove any existing entry for this version (makes the step idempotent).
entries = [e for e in entries if e.get("version") != version]
label = f"{version} (beta)" if is_beta else f"{version} (stable)"
url = f"https://cssfrancis.github.io/anyplotlib/{version}/"
# Insert right after the "dev" entry so newest stable floats to top.
entries.insert(1, {"name": label, "version": version, "url": url})
path.write_text(json.dumps(entries, indent=2) + "\n")
# ── Update root redirect for stable releases ─────────────────────────
- name: Update root redirect (stable releases only)
if: ${{ inputs.beta == false && inputs.bump != 'pre-release' }}
env:
VERSION_TAG: ${{ steps.version.outputs.tag }}
shell: python
run: |
import re, pathlib, os
version = os.environ["VERSION_TAG"]
path = pathlib.Path("docs/_root/index.html")
text = path.read_text()
text = re.sub(r'(content="0; url=)[^"]+(")', rf"\g<1>{version}/\2", text)
text = re.sub(r'(rel="canonical" href=")[^"]+(")', rf"\g<1>{version}/\2", text)
text = re.sub(r'(<a href=")[^"]+(">[^<]*</a>)', rf"\g<1>{version}/\2", text)
text = re.sub(r'(Redirecting to <a href="[^"]+">)[^<]*(</a>)',
rf"\g<1>{version} documentation\2", text)
path.write_text(text)
# ── Commit and push ──────────────────────────────────────────────────
- name: Configure git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Commit release changes
run: |
git checkout -b "${{ steps.version.outputs.branch }}"
# Stage version bumps, updated changelog, and consumed fragments.
git add pyproject.toml docs/conf.py CHANGELOG.rst
git add docs/_root/switcher.json docs/_root/index.html
git add -A upcoming_changes/ # stages deleted fragment files
git commit -m "chore: prepare release ${{ steps.version.outputs.tag }}"
git push origin "${{ steps.version.outputs.branch }}"
# ── Open pull request ────────────────────────────────────────────────
- name: Open pull request
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAG: ${{ steps.version.outputs.tag }}
BRANCH: ${{ steps.version.outputs.branch }}
run: |
gh pr create \
--title "Release ${TAG}" \
--base main \
--head "${BRANCH}" \
--body "## Release ${TAG}
> Auto-generated by the **Prepare Release** workflow.
### What changed
- Version bumped to \`${TAG}\` in \`pyproject.toml\` and \`docs/conf.py\`
- \`CHANGELOG.rst\` updated from towncrier fragments
- \`docs/_root/switcher.json\` updated with the new version entry
$([ '${{ inputs.beta }}' = 'false' ] && echo '- Root redirect updated to point to this release' || echo '')
### Review checklist
- [ ] \`CHANGELOG.rst\` reads well — edit the fragment text directly if needed
- [ ] Version strings are correct in \`pyproject.toml\` and \`docs/conf.py\`
- [ ] \`switcher.json\` has the right label and URL
- [ ] CI passes
### After merging
Create and push the tag to trigger the Release and Docs workflows:
\`\`\`bash
git fetch origin
git tag ${TAG} origin/main
git push origin ${TAG}
\`\`\`"