PyPI Publishing¶
The generated app includes a publish.yml workflow that automatically builds and uploads your package to PyPI whenever you publish a GitHub Release. This requires a one-time setup.
Steps¶
1. Create a PyPI account¶
Go to pypi.org and create an account if you don't have one.
2. Generate an API token¶
- Log in to PyPI and go to Account settings → API tokens
- Click Add API token
- Give it a name (e.g.
github-actions-<your-repo-name>) - For scope, choose Entire account for the first publish; you can scope it to the project afterward
- Click Create token and copy the token (it starts with
pypi-)
Warning
Copy the token now — PyPI will not show it again.
3. Create a release environment in GitHub¶
The publish.yml workflow uses a GitHub Environment called release for extra protection:
- Go to your GitHub repository → Settings → Environments
- Click New environment
- Name it exactly
release - Optionally add protection rules (e.g. require manual approval before deploying)
- Click Configure environment
4. Add the PyPI token as an environment secret¶
While still in the release environment settings:
- Under Environment secrets, click Add secret
- Name:
PYPI_API_TOKEN - Value: paste your PyPI token
- Click Add secret
How the Workflow Works¶
publish.yml triggers when you publish a GitHub Release:
It then:
- Checks out the code at the release tag
- Builds the distribution using
hatch build - Uploads to PyPI using the
pypa/gh-action-pypi-publishaction
The workflow runs in the release environment, which enforces any protection rules you configured.
Trusted Publishing (OIDC) — Optional¶
The workflow already has the correct OIDC permission (id-token: write), so you can optionally use PyPI Trusted Publishing instead of an API token:
- On PyPI, go to your project → Settings → Publishing
- Add a trusted publisher: GitHub Actions, with your repo and workflow filename (
publish.yml) - Remove the
user/passwordfields from the workflow and remove thePYPI_API_TOKENsecret
Trusted publishing is more secure because no token is stored anywhere.
First Publish¶
The first time you publish, PyPI will create the project under your account. Make sure the package name in pyproject.toml is available on PyPI before attempting the first release.