Skip to content

Commit 4d1b0fa

Browse files
committed
ci: harden PyPI publish workflow
1 parent 29b4873 commit 4d1b0fa

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

.github/workflows/publish.yml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,33 @@ jobs:
2020
with:
2121
python-version: "3.11"
2222

23+
- name: Verify tag matches package version
24+
run: |
25+
TAG_VERSION="${GITHUB_REF_NAME#v}"
26+
PACKAGE_VERSION="$(python - <<'PY'
27+
import tomllib
28+
with open("pyproject.toml", "rb") as f:
29+
data = tomllib.load(f)
30+
print(data["project"]["version"])
31+
PY
32+
)"
33+
if [ "$TAG_VERSION" != "$PACKAGE_VERSION" ]; then
34+
echo "Tag version ($TAG_VERSION) does not match package version ($PACKAGE_VERSION)."
35+
exit 1
36+
fi
37+
2338
- name: Build distributions
2439
run: |
2540
python -m pip install -U pip build
2641
python -m build
2742
28-
- name: Publish to PyPI
43+
- name: Publish to PyPI (API token)
44+
if: ${{ secrets.PYPI_API_TOKEN != '' }}
45+
uses: pypa/gh-action-pypi-publish@release/v1
46+
with:
47+
user: __token__
48+
password: ${{ secrets.PYPI_API_TOKEN }}
49+
50+
- name: Publish to PyPI (trusted publisher)
51+
if: ${{ secrets.PYPI_API_TOKEN == '' }}
2952
uses: pypa/gh-action-pypi-publish@release/v1

0 commit comments

Comments
 (0)