Skip to content

Workflow python package release

github-actions[bot] edited this page Oct 9, 2026 · 9 revisions

python-package-release

What it does. Builds the sdist and wheel, runs twine check --strict, reads the version off the sdist's own name and refuses a tag that disagrees, runs your verify-command, installs the wheel into a fresh virtual environment and runs your smoke-command, writes the release notes; then publishes to the GitHub release with a SHA256SUMS and build provenance. PyPI (Trusted Publishing, PEP 740 attestations) is published from a job in your workflow (download the dist artifact, run the pypa action), because PyPI cannot trust a reusable workflow as the publisher. No secret anywhere.

Why it exists. Four repositories had written build, check, tag-against-version and PyPI publish by hand, four slightly different ways. The checks that matter are the ones that catch a release that does not match its tag, or a wheel that does not install.

Jobs and trust boundaries

Job Holds Notes
build contents: read Everything that runs your code: the build, twine check, verify-command, the wheel smoke test, the release notes command
pypi-unsupported contents: read Runs only when a caller still passes pypi: true: fails at once with the fix, instead of at PyPI with invalid-publisher
github-release contents: write, id-token: write, attestations: write Checks nothing out. Records provenance for every file and attaches them with a SHA256SUMS; creates the release from the notes or uploads to one that exists

SBOMs. The build job also runs syft (pinned by version and sha256, the syft-version and syft-sha256 inputs) over the unpacked wheel and sdist and writes sbom.cdx.json (CycloneDX) and sbom.spdx.json (SPDX). They are their own artifact, so PyPI never receives them; github-release adds them to the release, to SHA256SUMS and to the same provenance step. sbom: true is the default, and a pull request writes them too, so a broken SBOM is a red check, not a surprise at tag time. They list the package and its metadata, not a resolved dependency tree. artifact-release.yml does not write them: its files are arbitrary (a .deb, a firmware blob) and syft would report nothing, which reads as assurance and is not.

A pull request builds and checks and never publishes, whatever publish says. The version is read off the sdist's own name, so a static version =, a dynamic attribute and a VCS plugin all answer alike.

A minimal caller

on:
  release: { types: [published] }
  pull_request:          # builds and checks; never publishes
  workflow_dispatch:

jobs:
  package:
    uses: ChiefGyk3D/git-your-ship-together/.github/workflows/python-package-release.yml@<sha> # vX.Y.Z
    permissions:
      contents: write      # the GitHub release and its assets
      id-token: write      # build provenance
      attestations: write  # the provenance record
    with:
      publish: ${{ github.event_name == 'release' }}   # the GitHub release step; pypi stays false
      verify-command: grep -q "^## \[$VERSION\]" CHANGELOG.md
      smoke-command: my-cli --version
      egress-policy: block

  publish-pypi:
    needs: package
    if: github.event_name == 'release'
    runs-on: ubuntu-latest
    environment:
      name: pypi            # the name the Trusted Publisher on PyPI holds
      url: https://pypi.org/p/<project>
    permissions:
      contents: read
      id-token: write       # PyPI Trusted Publishing; the OIDC token names THIS workflow
    steps:
      - uses: step-security/harden-runner@<sha> # vX.Y.Z
        with:
          egress-policy: block
          allowed-endpoints: api.github.com:443 files.pythonhosted.org:443 fulcio.sigstore.dev:443 ghcr.io:443 github.com:443 pkg-containers.githubusercontent.com:443 productionresultssa12.blob.core.windows.net:443 pypi.org:443 rekor.sigstore.dev:443 release-assets.githubusercontent.com:443 timestamp.sigstore.dev:443 tuf-repo-cdn.sigstore.dev:443 upload.pypi.org:443 uploads.github.com:443
          disable-sudo: true
          disable-telemetry: true
      - uses: actions/download-artifact@<sha> # vX.Y.Z
        with:
          name: dist
          path: dist/
      - uses: pypa/gh-action-pypi-publish@<sha> # vX.Y.Z
        with:
          attestations: true

Why PyPI is published from your workflow. PyPI matches the OIDC token's job_workflow_ref. Inside a reusable workflow that is the reusable file, so a Trusted Publisher registered for your workflow is answered invalid-publisher even when owner, repository, workflow file and environment all match (warehouse#11096; hypeman v0.3.1 and v0.3.2). The publish-pypi job above lives in your file, needs the package job, downloads the dist artifact and runs pypa/gh-action-pypi-publish with attestations, so the token names your workflow. The package job keeps publish: on so the GitHub release step still runs, with pypi left false. The pypa action is a Docker action, so the job's harden-runner list needs ghcr.io:443 and pkg-containers.githubusercontent.com:443 under block.

Warning: the pypa action must be a direct step of your job. pypa/gh-action-pypi-publish is a Docker action that derives its image from the repository of the action that contains it. Wrapped in a composite action or a reusable workflow it resolves to that repository's image: GYST v1.19.0 wrapped it in a composite, and hypeman's v0.3.3 release ran docker run ghcr.io/ChiefGyk3D/git-your-ship-together:<sha>, which does not exist, and failed with "Run 'docker run --help'" (run 37976560401). Keep both steps in your own job, as above.

PyPI setup, once per project. On the project's Publishing page add a Trusted Publisher naming this owner, the calling repository, the file name of the workflow that holds the publish-pypi job and the environment pypi. That is the whole credential.

The workflow outputs version. SBOMs are not attached yet; see issue #99 on Releases and supply chain.

Inputs (21), read from python-package-release.yml:

Input Type Default Meaning
python-version string 3.13 Python used to build, check and smoke-test the package.
package-directory string . Directory holding pyproject.toml, relative to the repository root.
build-install-command string python -m pip install --upgrade pip build twine Shell that installs the build tooling.
build-command string python -m build Shell that writes the sdist and wheel to dist/.
tag-prefix string v What precedes the version in a release tag; v makes v1.2.3 the tag for version 1.2.3.
verify-command string empty Shell run after the build with $VERSION set, to refuse a release whose tree disagrees with it, e.g. grep -q "^## \[$VERSION\]" CHANGELOG.md; empty runs nothing.
smoke-command string empty Shell run with the built wheel installed in a fresh venv on PATH and $VERSION set, e.g. my-cli --version; empty skips the check.
release-notes-command string empty Shell that prints the GitHub release notes to stdout with $VERSION set, e.g. a CHANGELOG section; empty lets GitHub generate them from the commits.
publish boolean false Publish to the GitHub release. False builds, checks and stops; a pull request never publishes whatever this says.
pypi boolean false Unsupported, kept so old callers still parse. PyPI Trusted Publishing cannot use a reusable workflow as the publisher (invalid-publisher, warehouse#11096), so true fails the run with the fix; publish from a job in the caller's workflow with the publish-pypi composite action instead.
pypi-environment string pypi Unused by this workflow; the environment belongs to the caller's publish job. Kept so existing callers still parse.
github-release boolean true Attach the distribution files, their SBOMs, their SHA256SUMS and build provenance to the GitHub release for the tag.
release-title string empty Title of a GitHub release this workflow creates; empty uses the tag.
sbom boolean true Write a CycloneDX and an SPDX SBOM for the built wheel and sdist (syft, pinned) and attach them to the GitHub release with the rest, listed in SHA256SUMS and covered by the same build provenance. They are written whenever the package is built, so a pull request exercises them; they are only published with the release.
syft-version string 1.54.0 syft release to download (anchore/syft), without the v; the one syft pin in this repository. Changing it means changing syft-sha256. To bump - gh release download v<new> -R anchore/syft -p '*_linux_amd64.tar.gz', sha256sum it and compare the line in the release's own *_checksums.txt.
syft-sha256 string 54a87372498168b2d033e876fd41fa4e8035b872699e525a57046e1f2f09c860 SHA-256 of the syft linux_amd64 tarball for that version.
attest boolean true Record SLSA build provenance for every distribution file through GitHub's attestation API.
egress-policy string audit harden-runner egress policy for every job - audit logs outbound connections, block allows only allowed-endpoints.
allowed-endpoints string api.github.com:443 files.pythonhosted.org:443 fulcio.sigstore.dev:443 github.com:443 pypi.org:443 rekor.sigstore.dev:443 release-assets.githubusercontent.com:443 timestamp.sigstore.dev:443 tuf-repo-cdn.sigstore.dev:443 upload.pypi.org:443 uploads.github.com:443 harden-runner allow-list for block, space-separated host:port entries. The build hosts were measured by running the build in block mode here; the release job's hosts are GitHub's and Sigstore's documented ones. Wildcards are not supported and a newline-separated list matches nothing.
extra-allowed-endpoints string empty Appended to allowed-endpoints, for hosts only this repository reaches. Same format.
timeout-minutes number 20 Per-job timeout.

Outputs a caller job can read:

Output Meaning
version The version the sdist was built as.

What it refuses to do

It never publishes from a pull request, never publishes a tag that disagrees with the packaged version, and never needs a stored credential.

Clone this wiki locally