Skip to content

Commit a3f1cea

Browse files
authored
Add workflow to publish tagged releases to PyPI (#128)
Signed-off-by: Scott Dyer <sdyer@oscars.org>
1 parent fb2608d commit a3f1cea

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

.github/workflows/publish.yml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: Publish to PyPI
2+
3+
on:
4+
push:
5+
tags:
6+
- "v*" # Only run when a version tag is pushed
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
publish:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Check out repository
17+
uses: actions/checkout@v4
18+
with:
19+
persist-credentials: false
20+
21+
- name: Set up Python
22+
uses: actions/setup-python@v5
23+
with:
24+
python-version: "3.11"
25+
26+
- name: Install dependencies for build and publish
27+
run: |
28+
python -m pip install --upgrade pip
29+
pip install build twine toml
30+
31+
- name: Extract version number from tag
32+
id: get_version
33+
run: echo "version=$(echo ${{ github.ref_name }} | sed 's/^v//')" >> $GITHUB_OUTPUT
34+
35+
- name: Verify all version numbers match
36+
run: |
37+
TAG_VERSION="${{ steps.get_version.outputs.version }}"
38+
39+
PYPROJECT_VERSION=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['version'])")
40+
41+
APP_VERSION=$(python -c """
42+
import re
43+
44+
with open('app.py', 'r') as f:
45+
content = f.read()
46+
47+
version_block = '\n'.join(
48+
line for line in content.splitlines()
49+
if re.match(r'^__\w+__\s*=', line)
50+
)
51+
52+
version_namespace = {}
53+
exec(version_block, version_namespace)
54+
print(version_namespace['__version__'])
55+
""")
56+
57+
echo "Tag version: $TAG_VERSION"
58+
echo "pyproject.toml version: $PYPROJECT_VERSION"
59+
echo "app.py version: $APP_VERSION"
60+
61+
if [ "$PYPROJECT_VERSION" != "$TAG_VERSION" ]; then
62+
echo "Error: pyproject.toml version ($PYPROJECT_VERSION) does not match tag version ($TAG_VERSION)."
63+
exit 1
64+
fi
65+
66+
if [ "$APP_VERSION" != "$TAG_VERSION" ]; then
67+
echo "Error: app.py version ($APP_VERSION) does not match tag version ($TAG_VERSION)."
68+
exit 1
69+
fi
70+
71+
echo "All versions match: $TAG_VERSION"
72+
73+
- name: Build package
74+
run: python -m build
75+
76+
- name: Publish to PyPI
77+
env:
78+
TWINE_USERNAME: __token__
79+
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
80+
run: twine upload dist/* --verbose
81+
82+
- name: Create Github release
83+
uses: softprops/action-gh-release@v2
84+
with:
85+
tag_name: ${{ github.ref_name }}
86+
generate_release_notes: true

0 commit comments

Comments
 (0)