Skip to content

Publish the package to PyPI on every release - #43

Merged
dimartin merged 4 commits into
mainfrom
ci/pypi-publish
Dec 4, 2026
Merged

dimartin merged 4 commits into
mainfrom
ci/pypi-publish

Conversation

@dimartin

@dimartin dimartin commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

This repo has no release workflow, so a tagged version reached PyPI only when someone uploaded it by hand. This adds .github/workflows/pypi.yml, triggered by a pushed vX.Y.Z tag.

What it does

  • It checks that the tag names the version pyproject.toml declares.
  • It builds the sdist and wheel with uv build, with SOURCE_DATE_EPOCH at the commit time, and requires exactly the two files named for that version. It runs twine check --strict and writes the sha256 of both to the run summary.
  • On a tag, it publishes with pypa/gh-action-pypi-publish v1.14.3 (pinned by commit) using Trusted Publishing, in the pypi environment, with skip-existing. The job has only contents: read and id-token: write, and no PyPI token is stored in the repo.
  • From a branch (workflow_dispatch), it builds and checks only.
  • pyproject.toml gets readme = "README.md". Without it, twine check --strict fails and the PyPI page is blank.

Testing

  • The workflow parses.
  • Not run: workflow_dispatch works only once the file is on the default branch. After merge, gh workflow run pypi.yml --ref main is a dry run.

Known gaps

  • The workflow doesn't run the tests. A tag is cut from a main commit whose CI already passed. Calling the test workflow from here would need a workflow_call trigger, and techne's test workflow cancels runs that share its concurrency group, so that's left out.

Needed before the first tag after this merges

A PyPI owner of tenish-stack adds a trusted publisher on pypi.org: owner tenishai, repository stack, workflow pypi.yml, environment pypi. Until then, the publish step fails with an OIDC error.

Part of the suite-wide change started in tenishai/daedalus PR 309 (issue tenishai/daedalus#308): every public repo publishes to PyPI on every tag.

Update: build and publish are separate jobs

The first version ran uv build and uvx twine (unpinned packages from PyPI) in the job holding id-token: write, where any of them could mint the upload token. Our reviewer flagged that shape on tenishai/liminal#273 and tenishai/techne#1430. This now ports techne's reviewed pypi.yml (tenishai/techne#1430): a build job (contents: read) checks the tag against pyproject.toml and that it points at a commit on main, builds, runs twine and uploads dist/; a pypi job (tag only, environment pypi, permissions exactly id-token: write) only downloads and publishes. The workflow header names the trusted publisher's fields (a pending publisher until tenish-stack's first upload).

A test now pins the trigger and the split, and fails when the publishing job gains a permission beside id-token, gains a run: step, or the build side loses its check. Still publishes only from a vX.Y.Z tag; a merge to main publishes nothing.

A pushed tag never reached PyPI unless someone uploaded it by hand. A new
pypi.yml runs on a vX.Y.Z tag: it checks the tag against pyproject.toml,
builds the sdist and wheel with SOURCE_DATE_EPOCH at the commit time,
runs twine check --strict, and publishes by Trusted Publishing. A dry run
from a branch builds and checks only.

Signed-off-by: Rich DiMartino <richdimartino@gmail.com>
Co-Authored-By: Poe (Claude Opus) <richdimartino+poe@gmail.com>
@dimartin
dimartin requested a review from shaz as a code owner December 3, 2026 11:45
Prudence (cabal PR 525): with SOURCE_DATE_EPOCH, two builds give the same
wheel but different sdists, since sdist members keep file mtimes. The
comment said a re-run builds the same files; it now says the wheel, and
that skip-existing keeps a re-run passing either way.

Signed-off-by: Rich DiMartino <richdimartino@gmail.com>
Co-Authored-By: Poe (Claude Opus) <richdimartino+poe@gmail.com>
dimartin and others added 2 commits December 3, 2026 18:03
The one job that held id-token: write also ran uv build and uvx twine
(unpinned packages from PyPI), so either could mint the tenish-stack upload
token. This ports techne's reviewed pypi.yml (merged there as b8821f0): a
build job (contents: read, no persisted credentials) checks the tag against
pyproject.toml and that it is on main, builds, runs twine and uploads dist/
as an artifact; a pypi job (tag only, environment pypi, permissions exactly
id-token: write) only downloads and publishes. tests/test_ci_workflow.py
gains techne's three pypi.yml tests; dropping the job tag guard, widening
pypi's permissions, a run step in pypi, or disabling the on-main check each
fail one. The header names the trusted publisher's fields (a pending
publisher until the first upload).

Signed-off-by: Rich DiMartino <richdimartino@gmail.com>
Co-Authored-By: Poe (Claude Opus) <richdimartino+poe@gmail.com>
An extra uses: step in the job holding the OIDC token passed the test
(Temperance's finding on the same test in daedalus). The test now
compares the job's actions in order.

Signed-off-by: Rich DiMartino <richdimartino@gmail.com>
Co-Authored-By: Poe (Claude Opus) <richdimartino+poe@gmail.com>

@prudence-tenishai prudence-tenishai left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

This adds a tag-triggered .github/workflows/pypi.yml that publishes tenish-stack by Trusted Publishing. It also adds readme to pyproject.toml, a CHANGELOG entry and three tests. The split is done properly. build has only contents: read, so uv build and uvx twine never run where an OIDC token can be minted. pypi has exactly {id-token: write}, needs: [build], the job-level guard if: github.ref_type == 'tag' and environment: pypi. Its only steps are download-artifact and the publish action, and the publish step has skip-existing: true and a second tag guard of its own. The checkout sets persist-credentials: false. The build step checks that the exact files tenish_stack-$VERSION-py3-none-any.whl and tenish_stack-$VERSION.tar.gz exist and that nothing else is in dist, and $VERSION has already been checked against the tag. So the uploaded file names are tied to the tag without a separate check in the publish job. The pypi job does not wait on tests, which is a deliberate choice and the header says so: the on-main check (merge-base --is-ancestor HEAD origin/main, with fetch-depth: 0 so origin/main is present) only accepts commits that reached main through the ruleset's required ci check. I also checked the workflow_dispatch path. A dispatch from a branch only builds and checks. A dispatch with --ref vX.Y.Z does publish, because the guard tests ref_type rather than event_name. That run goes through the same version, on-main and environment checks as a tag push, and skip-existing makes it equivalent to a re-run. The header only promises "a dry run from a branch", so I don't count it as a defect. The tests are good. test_pypi_publishes_only_a_tag_by_trusted_publishing compares dicts exactly where that matters, and the two bash-extraction tests run the real step bodies rather than copies of them.

Issues

Nit

  1. tests/test_ci_workflow.py:83: two of the header's guarantees are not pinned. I ran the whole file against each mutant: setting persist-credentials: true passes 11/11, and so does deleting skip-existing: true. Both are correct in the workflow today. Two more asserts would make them as hard to delete as the other guarantees.
  2. tests/test_ci_workflow.py:37: test_every_action_is_pinned_to_a_commit_sha reads only ci.yml, so nothing tests the pins on checkout, setup-uv, upload-artifact and download-artifact in pypi.yml; only the publish action's pin is asserted. All five pins are correct today (see Verdict). Looping the pin test over both workflow files would keep them that way.
  3. .github/workflows/pypi.yml:18: one header line runs long ("No PyPI token is stored here. skip-existing lets…"). It looks like a reflow left over from an edit.

Suggestions

None.

Verdict

Verdict: LGTM, verification: executed: these ran on mash1 at 6f52cfc. tests/test_ci_workflow.py passed 11/11, and ruff check stack tests and mypy stack tests (CI's lint and type checks) were clean. I ran the real "Build the sdist and wheel" step body with VERSION=0.1.0: uv build produced exactly the expected wheel and sdist, twine check --strict passed both, and the step exited 0. I applied nine mutants to pypi.yml and ran the whole test file against each. Seven were caught (1 failed each): a run: step moved into pypi, contents: read added to pypi, the job-level tag guard dropped, the environment dropped, the tag-equals-declared check disabled, the on-main check disabled, and id-token added to build. Two passed, as listed under Nit 1. I resolved all five action pins through the GitHub API: checkout v7.0.1, setup-uv v10.3.3, upload-artifact v7.0.2 and download-artifact v8.0.2 are lightweight tags that point straight at the pinned commits. gh-action-pypi-publish v1.14.3 is an annotated tag that dereferences to 7245906… and is the latest release. I could not run the pypi job itself: it needs a pushed tag and the pending publisher on pypi.org. The OIDC exchange and the upload are therefore unexercised until the first release, and the evidence for the permissions and guards is the YAML and the mutants above. Temperance's pre-pass (same sha) had no WRONG or RISKY items and agrees with this.

@dimartin
dimartin merged commit 274812e into main Dec 4, 2026
3 checks passed
@dimartin
dimartin deleted the ci/pypi-publish branch December 4, 2026 12:50
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