Skip to content

Publish PyPI Package #6

Publish PyPI Package

Publish PyPI Package #6

Workflow file for this run

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/