Skip to content

Commit bfabee5

Browse files
Carreauclaude
andcommitted
Add a manual release workflow
release.yml is started from the Actions tab (or `gh workflow run release.yml -f version=X.Y.Z`) on main. It: - validates the version (X.Y.Z, optionally aN/bN/rcN, no 'v' prefix) and that the tag does not exist yet - sets __version__ in ipython_pygments_lexers/__init__.py - commits "Version number -> X.Y.Z" as github-actions[bot] and adds an annotated X.Y.Z tag, matching the existing conventions - pushes main and the tag atomically - starts publish.yml on the new tag Pushes made with GITHUB_TOKEN do not trigger other workflows, so the tag push alone would not start publish.yml. workflow_dispatch is the exception, so publish.yml gains a workflow_dispatch trigger. Both its jobs only run on tag refs, and the tag/__version__ check plus the pypi environment's tag rule still apply, so dispatching it on a branch cannot publish. If main is protected against direct pushes, GitHub Actions must be allowed to bypass that rule for this to work. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 7c43517 commit bfabee5

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

‎.github/workflows/publish.yml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ name: Publish
66
on:
77
push:
88
tags: [ '[0-9]+.[0-9]+.[0-9]+*' ]
9+
# Used by release.yml: a tag pushed with GITHUB_TOKEN does not trigger the
10+
# push event above, but a workflow_dispatch does. Run it on a tag ref.
11+
workflow_dispatch:
912

1013
permissions: {}
1114

1215
jobs:
1316
# Lint, tests, the tag/__version__ check and the build, from test.yml.
1417
ci:
18+
if: ${{ startsWith(github.ref, 'refs/tags/') }}
1519
# A job-level call resolves from this commit, not the runner's files, so
1620
# the newer "$/" self-repository syntax buys nothing here.
1721
uses: ./.github/workflows/test.yml # zizmor: ignore[self-repository]
@@ -23,6 +27,7 @@ jobs:
2327
# Must stay a job of this workflow: PyPI trusted publishing does not
2428
# accept uploads from inside a reusable (called) workflow.
2529
needs: ci
30+
if: ${{ startsWith(github.ref, 'refs/tags/') }}
2631
runs-on: ubuntu-latest
2732
environment:
2833
name: pypi

‎.github/workflows/release.yml‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: Release
2+
3+
# Manually started from the Actions tab (or `gh workflow run release.yml -f
4+
# version=X.Y.Z`). Sets __version__, commits "Version number -> X.Y.Z" on
5+
# main, tags it X.Y.Z, pushes both, then starts publish.yml on the new tag.
6+
on:
7+
workflow_dispatch:
8+
inputs:
9+
version:
10+
description: "Version to release, e.g. 1.2.0 or 1.2.0rc1 (no 'v' prefix)"
11+
required: true
12+
type: string
13+
14+
permissions: {}
15+
16+
concurrency:
17+
group: release
18+
cancel-in-progress: false
19+
20+
jobs:
21+
release:
22+
name: Bump, tag and trigger publish
23+
if: ${{ github.ref == 'refs/heads/main' }}
24+
runs-on: ubuntu-latest
25+
permissions:
26+
contents: write # push the version commit and tag
27+
actions: write # start publish.yml
28+
env:
29+
VERSION: ${{ inputs.version }}
30+
steps:
31+
- name: Checkout
32+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
33+
with:
34+
persist-credentials: false
35+
36+
- name: Validate version
37+
env:
38+
GH_TOKEN: ${{ github.token }}
39+
run: |
40+
if ! [[ "${VERSION}" =~ ^[0-9]+\.[0-9]+\.[0-9]+((a|b|rc)[0-9]+)?$ ]]; then
41+
echo "::error::'${VERSION}' is not a version like 1.2.0 or 1.2.0rc1"
42+
exit 1
43+
fi
44+
if gh api "repos/${GITHUB_REPOSITORY}/git/ref/tags/${VERSION}" --silent 2>/dev/null; then
45+
echo "::error::Tag ${VERSION} already exists"
46+
exit 1
47+
fi
48+
49+
- name: Set __version__
50+
run: |
51+
sed -i -E "s/^__version__ = \".*\"$/__version__ = \"${VERSION}\"/" ipython_pygments_lexers/__init__.py
52+
grep -qx "__version__ = \"${VERSION}\"" ipython_pygments_lexers/__init__.py
53+
if git diff --quiet; then
54+
echo "::error::__version__ is already ${VERSION}"
55+
exit 1
56+
fi
57+
58+
- name: Commit, tag and push
59+
env:
60+
GH_TOKEN: ${{ github.token }}
61+
run: |
62+
git config user.name "github-actions[bot]"
63+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
64+
git commit -am "Version number -> ${VERSION}"
65+
git tag -a "${VERSION}" -m "Release ${VERSION}"
66+
git push --atomic "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" \
67+
HEAD:refs/heads/main "refs/tags/${VERSION}"
68+
69+
- name: Start publish workflow
70+
env:
71+
GH_TOKEN: ${{ github.token }}
72+
run: |
73+
gh workflow run publish.yml --repo "${GITHUB_REPOSITORY}" --ref "${VERSION}"
74+
echo "Publishing ${VERSION}: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/workflows/publish.yml" >> "${GITHUB_STEP_SUMMARY}"

0 commit comments

Comments
 (0)