Publish PyPI Package #6
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 PyPI Package | |
| on: | |
| push: | |
| tags: | |
| - 'py-v*' | |
| workflow_dispatch: | |
| permissions: | |
| contents: read | |
| id-token: write | |
| jobs: | |
| publish-pypi: | |
| runs-on: ubuntu-latest | |
| environment: | |
| name: pypi | |
| steps: | |
| - name: Check out repository | |
| uses: actions/checkout@v5 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.11' | |
| - name: Install build tooling | |
| run: python -m pip install --upgrade pip build twine | |
| - name: Run tests | |
| run: python -m unittest discover -s tests | |
| - name: Verify tag matches package version | |
| if: startsWith(github.ref, 'refs/tags/') | |
| env: | |
| GITHUB_REF_NAME: ${{ github.ref_name }} | |
| run: | | |
| PACKAGE_VERSION=$(python -c "import tomllib; print(tomllib.load(open('pyproject.toml','rb'))['project']['version'])") | |
| EXPECTED_TAG="py-v${PACKAGE_VERSION}" | |
| if [ "$GITHUB_REF_NAME" != "$EXPECTED_TAG" ]; then | |
| echo "Tag $GITHUB_REF_NAME does not match package version $PACKAGE_VERSION" | |
| exit 1 | |
| fi | |
| - name: Build package | |
| run: python -m build | |
| - name: Validate package metadata | |
| run: python -m twine check dist/* | |
| - name: Publish to PyPI | |
| uses: pypa/gh-action-pypi-publish@release/v1 | |
| with: | |
| packages-dir: dist/ |