Use twine upload for PyPI publishing #3
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Publish to PyPI | |
| on: | |
| push: | |
| tags: | |
| - "v*" | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| steps: | |
| - name: Checkout source | |
| uses: actions/checkout@v5 | |
| - name: Set up Python | |
| uses: actions/setup-python@v6 | |
| with: | |
| python-version: "3.11" | |
| cache: "pip" | |
| - name: Verify tag matches project version | |
| shell: bash | |
| run: | | |
| python - <<'PY' | |
| import os | |
| import pathlib | |
| import tomllib | |
| pyproject = pathlib.Path("pyproject.toml").read_text(encoding="utf-8") | |
| version = tomllib.loads(pyproject)["project"]["version"] | |
| tag = os.environ["GITHUB_REF_NAME"] | |
| expected = f"v{version}" | |
| if tag != expected: | |
| raise SystemExit( | |
| f"Refusing to publish: tag {tag!r} does not match expected {expected!r}" | |
| ) | |
| print(f"Publishing version {version} from tag {tag}") | |
| PY | |
| - name: Install build dependencies | |
| run: python -m pip install --upgrade pip build twine pytest | |
| - name: Run tests | |
| run: python -m pytest -q | |
| - name: Build distributions | |
| run: python -m build | |
| - name: Check distribution metadata | |
| run: python -m twine check dist/* | |
| - name: Publish package to PyPI | |
| env: | |
| TWINE_USERNAME: __token__ | |
| TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} | |
| run: python -m twine upload dist/* |