Skip to content

ci: harden PyPI publish workflow (#9) #3

ci: harden PyPI publish workflow (#9)

ci: harden PyPI publish workflow (#9) #3

Workflow file for this run

name: Publish to PyPI
on:
push:
tags:
- "v*"
permissions:
contents: read
id-token: write
jobs:
publish:
runs-on: ubuntu-latest
environment: release
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Verify tag matches package version
run: |
TAG_VERSION="${GITHUB_REF_NAME#v}"
PACKAGE_VERSION="$(python - <<'PY'
import tomllib

Check failure on line 27 in .github/workflows/publish.yml

View workflow run for this annotation

GitHub Actions / .github/workflows/publish.yml

Invalid workflow file

You have an error in your yaml syntax on line 27
with open("pyproject.toml", "rb") as f:
data = tomllib.load(f)
print(data["project"]["version"])
PY
)"
if [ "$TAG_VERSION" != "$PACKAGE_VERSION" ]; then
echo "Tag version ($TAG_VERSION) does not match package version ($PACKAGE_VERSION)."
exit 1
fi
- name: Build distributions
run: |
python -m pip install -U pip build
python -m build
- name: Publish to PyPI (API token)
if: ${{ secrets.PYPI_API_TOKEN != '' }}
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
- name: Publish to PyPI (trusted publisher)
if: ${{ secrets.PYPI_API_TOKEN == '' }}
uses: pypa/gh-action-pypi-publish@release/v1