Skip to content

Publish to PyPI with trusted publishing instead of an API token - #170

Open
rr3khan wants to merge 3 commits into
mainfrom
riyad/add-trusted-pypi-publishing-setup
Open

rr3khan wants to merge 3 commits into
mainfrom
riyad/add-trusted-pypi-publishing-setup

Conversation

@rr3khan

@rr3khan rr3khan commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

What changed

The release workflow (.github/workflows/release.yml) can now publish onepasswordconnectsdk to PyPI with PyPI trusted publishing (OIDC) instead of the long-lived PYPI_API_TOKEN secret. It keeps the token as a fallback until trusted publishing is set up.

The trigger is unchanged: the workflow still runs when a release/* PR is merged into main. The single release job is split into three:

  • build (read-only): runs poetry build and uploads dist/ as an artifact.
  • publish: the only job with id-token: write. It runs in the pypi environment and uploads the build job's files. It runs no code from the repo.
    • Token while it exists: the step passes PYPI_API_TOKEN as the password. While the secret is set, it uploads with the token, as today. Once the secret is deleted, the password is empty, so the action uses the job's OIDC token and attaches attestations.
  • release (contents: write): creates the tag and GitHub release, the same as before.

It also replaces the deprecated ::set-output command with $GITHUB_OUTPUT.

Why

Anyone holding the PYPI_API_TOKEN (set in 2021) can publish a package under our name. Trusted publishing removes the token, and the environment puts every publish behind a review.

The fallback means this can merge before anyone has access to the PyPI project. Releases keep working on the token until the secret is deleted. Switching over is a settings change, not another PR.

Rollout

  1. GitHub: create the pypi environment. Require review from 1Password/open-source, turn on "Prevent self-review", and add no secrets. For deployment branches, use the same rules as connect-sdk-js, which has the same merged-release-PR trigger: main, beta and release/*. If a release runs before the environment exists, GitHub creates it with no protection rules, and the token upload still works.
  2. Merge this. Releases still use the token, now behind the environment's approval.
  3. PyPI: add a trusted publisher on the onepasswordconnectsdk project (Manage → Publishing) with owner 1Password, repository connect-sdk-python, workflow release.yml and environment pypi.
  4. GitHub: delete the PYPI_API_TOKEN repository secret (Settings → Secrets and variables → Actions). Make sure the token is saved in 1Password first: re-adding the secret is the rollback.
  5. After the next release publishes through trusted publishing: revoke the token on PyPI. A small follow-up PR can drop the now-unused password: line.

Don't do step 4 before step 3. With no secret and no trusted publisher, the publish fails at the OIDC exchange and nothing is uploaded.

Notes for reviewers

  • Branch rules on a pull_request trigger aren't proven yet. GitHub matches environment branch rules against the run's ref. connect-sdk-js's last release (v1.4.3) recorded its deployment against the head branch release/v1.4.3, which the release/* rule covers. Its branch rules were only added after that release, though, so this hasn't been tested with them in place. If the first publish is rejected with "not allowed to deploy", the environment's branch rules are the place to look.
  • The token path is unchanged apart from attestations. Without trusted publishing, the action skips attestations and logs a warning; it doesn't fail.
  • The tag target hasn't changed. The GitHub release still tags main (commit: main), as before.

🤖 Generated with Claude Code

rr3khan and others added 2 commits December 3, 2026 16:48
The release workflow uploaded to PyPI with a long-lived PYPI_API_TOKEN
secret. Switch it to PyPI trusted publishing (OIDC), so the workflow gets
a short-lived token for each release and no secret is stored.

The single release job is split into build, publish and GitHub release
jobs. Only the publish job gets an OIDC token, and it runs in the `pypi`
environment so each publish needs a reviewer's approval. Also replace the
deprecated `::set-output` command with $GITHUB_OUTPUT.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Publish with the PYPI_API_TOKEN secret unless the PYPI_TRUSTED_PUBLISHING
repository variable is `true`. Releases keep working if this merges
before the trusted publisher exists on PyPI, and switching over is a
settings change rather than another PR. The token path will be removed
once a release has gone through with trusted publishing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Drop the PYPI_TRUSTED_PUBLISHING variable. The publish step now passes the
PYPI_API_TOKEN secret directly, so releases use the token while the secret
exists and trusted publishing once it is deleted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants