Skip to content

Publish to PyPI (Trusted Publishing) #15

Publish to PyPI (Trusted Publishing)

Publish to PyPI (Trusted Publishing) #15

Workflow file for this run

# .github/workflows/publish-to-pypi.yml
name: Publish to PyPI
on:
release:
types: [published]
workflow_dispatch: # Allows you to manually trigger the workflow
permissions:
contents: read
id-token: write # Required for secure OIDC authentication with PyPI
jobs:
deploy:
runs-on: ubuntu-latest
environment: pypi # Recommended for security
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch full history to ensure we have the latest commits
- name: Clear GitHub Actions cache
run: |
echo "Clearing any cached data..."
rm -rf ~/.cache/pip
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
# Removed pip cache to force fresh install
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build twine toml
- name: Verify package version
run: |
PACKAGE_VERSION=$(python -c "import toml; print(toml.load(open('pyproject.toml'))['project']['version'])")
echo "Detected version from pyproject.toml: $PACKAGE_VERSION"
echo "Expected tag version: ${{ github.ref_name }}"
if [ "$PACKAGE_VERSION" != "${{ github.ref_name }}" ]; then
echo "Error: The version in pyproject.toml does not match the release tag."
exit 1
fi
- name: Clean old builds and build new package
run: |
rm -rf dist/ build/ *.egg-info/ # Clean all build artifacts
python -m build
- name: Publish package to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}