Skip to content

Commit e060d87

Browse files
committed
add PyPI publish workflow
1 parent 30ed753 commit e060d87

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

.github/workflows/publish-pypi.yml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: Build and publish to PyPI
2+
3+
on:
4+
release:
5+
types: [published]
6+
workflow_dispatch:
7+
8+
jobs:
9+
build:
10+
name: Build distributions
11+
runs-on: ubuntu-latest
12+
outputs:
13+
version: ${{ steps.version.outputs.version }}
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
- name: Set up Python
18+
uses: actions/setup-python@v5
19+
with:
20+
python-version: "3.11"
21+
22+
- name: Install build tools
23+
run: python -m pip install --upgrade pip build twine
24+
25+
- name: Read package version
26+
id: version
27+
run: |
28+
python - <<'PY'
29+
import os
30+
import pathlib
31+
import tomllib
32+
version = tomllib.loads(pathlib.Path("pyproject.toml").read_bytes())["project"][
33+
"version"
34+
]
35+
print(version)
36+
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as f:
37+
f.write(f"version={version}\n")
38+
PY
39+
40+
- name: Validate release tag matches version
41+
if: github.event_name == 'release'
42+
env:
43+
TAG_NAME: ${{ github.event.release.tag_name }}
44+
run: |
45+
python - <<'PY'
46+
import os
47+
tag = os.environ["TAG_NAME"]
48+
if tag.startswith("v"):
49+
tag = tag[1:]
50+
# `steps.version.outputs.version` is available via GHA expressions only, so re-read here.
51+
import pathlib, tomllib
52+
version = tomllib.loads(pathlib.Path("pyproject.toml").read_bytes())["project"]["version"]
53+
if tag != version:
54+
raise SystemExit(f"Release tag ({tag}) does not match pyproject.toml version ({version})")
55+
print("Tag matches version:", tag)
56+
PY
57+
58+
- name: Build sdist and wheel
59+
run: python -m build --sdist --wheel --outdir dist/
60+
61+
- name: Twine check
62+
run: python -m twine check --strict dist/*
63+
64+
- uses: actions/upload-artifact@v4
65+
with:
66+
name: dist
67+
path: dist/
68+
69+
publish:
70+
name: Publish to PyPI
71+
needs: build
72+
runs-on: ubuntu-latest
73+
if: github.event_name == 'release'
74+
permissions:
75+
id-token: write
76+
contents: read
77+
steps:
78+
- uses: actions/download-artifact@v4
79+
with:
80+
name: dist
81+
path: dist/
82+
83+
- name: Publish
84+
uses: pypa/gh-action-pypi-publish@release/v1
85+
with:
86+
packages-dir: dist/

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@
1111
!pyproject.toml
1212
!LICENSE
1313
!README.md
14+
!.github/
15+
!.github/workflows/
16+
!.github/workflows/*.yml

0 commit comments

Comments
 (0)