Satchel Hamilton
← Writing

Publishing to PyPI without a token: one workflow, seven packages

Seven of my tools are on PyPI: gatecheck, toroid, kinescope, crucible-ttc, manifold-cov, congruent-eq and bonafide. I never created, pasted or stored a PyPI API token for any of them. They all publish through PyPI's Trusted Publishing, from the same 31-line workflow file.

The problem with a stored token

The usual way to publish from CI is to make a long-lived API token on PyPI and save it as a secret in GitHub. That works. But now a token that can publish your package sits in a second system. Any workflow in the repo can read it, and it stays valid until you rotate it. If it leaks, someone else can upload a release under your name, and nobody running pip install can tell.

What Trusted Publishing does instead

Trusted Publishing uses OpenID Connect (OIDC), a standard way for one service to vouch for who is asking. It works in three steps:

  1. On PyPI, I register a trusted publisher for the project. It says: the GitHub repo satchmakua/gatecheck, running the workflow publish.yml, may publish this package.
  2. When that workflow runs, GitHub Actions issues a short-lived OIDC token that states which repo and which workflow is running.
  3. PyPI checks that token against what I registered. If it matches, PyPI hands back an upload token for this one project that expires in minutes.

Nothing long-lived is stored, so there is nothing to leak. The credential is made at the moment of publishing and is useless soon after.

The whole setup

This is the entire publish workflow. The same file, byte for byte, is in all seven repos:

name: Publish to PyPI

on:
  release:
    types: [published]
  workflow_dispatch:

permissions:
  contents: read

jobs:
  publish:
    runs-on: ubuntu-latest
    permissions:
      id-token: write   # required: mints the OIDC token PyPI trusts
      contents: read    # required: for actions/checkout
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.11"
      - name: Build sdist + wheel
        run: |
          python -m pip install --upgrade build
          python -m build
      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1

The line that matters is id-token: write. It lets the job ask GitHub for the OIDC token. The publish step has no with: block, no password and no token. pypa/gh-action-pypi-publish does the exchange with PyPI on its own. I publish a GitHub Release, the workflow runs, and the package appears on PyPI. Adding an eighth package means copying this file and registering one more trusted publisher.

The rule that catches everyone once

A version on PyPI can be uploaded only once. After 1.0.0 is up, you cannot upload 1.0.0 again, even if you delete it. If 1.0.0 has a bug, the only fix is to publish 1.0.1. That is on purpose: it means pip install gatecheck==1.0.0 gets the same files for everyone, every time.

So the version in pyproject.toml is the release, and bumping it is a decision, not a chore. My workflow runs only when I publish a GitHub Release, so the Git tag, the release and the PyPI version are set in one step and can't drift apart.