Repository navigation
Publish to PyPI with trusted publishing #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
62fbfcd
Publish to PyPI with trusted publishing
iunir 3ff56c0
Document the release process
iunir 64c9b4d
Link the release process from the README
iunir 90d338c
Harden the publish workflow
iunir bf014bb
Address review: shorter retention, Python 3.15, approval steps
iunir 13ffbca
Replace the one-time setup with a note on the repository settings
iunir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Release | ||
|
|
||
| How to publish a new version of `labelformat` to [PyPI](https://pypi.org/project/labelformat/). | ||
|
|
||
| The [Publish to PyPI](https://github.com/lightly-ai/labelformat/actions/workflows/publish.yml) | ||
| workflow uploads the package with | ||
| [trusted publishing](https://docs.pypi.org/trusted-publishers/). Nobody needs a PyPI token or | ||
| password. | ||
|
|
||
| ## Repository settings | ||
|
|
||
| The `pypi` and `testpypi` environments require approval from the `lightly` team and allow only | ||
| `main` and `v*` tags. Do not relax this: PyPI does not check the branch, so these settings are | ||
| what stop a workflow edited on a branch from publishing. | ||
|
|
||
| ## Release process | ||
|
|
||
| ### 1. Bump the version | ||
|
|
||
| Open a `Bump version to X.Y.Z` pull request: | ||
|
|
||
| - Set `version` in `pyproject.toml`. | ||
| - Run `uv sync` to update `uv.lock`. Use the uv version that CI uses, see | ||
| [run-tests.yml](workflows/run-tests.yml). | ||
|
|
||
| Merge it. | ||
|
|
||
| ### 2. Create the GitHub release | ||
|
|
||
| Create a [release](https://github.com/lightly-ai/labelformat/releases/new) from `main`. The | ||
| workflow stops if the tag is not on `main`. | ||
|
|
||
| 1. Tag: `vX.Y.Z` > **Create new tag**. The tag must match the version in `pyproject.toml`. | ||
| 2. Title: `vX.Y.Z`. | ||
| 3. Click **Generate release notes**. | ||
| 4. Select **Set as the latest release**. | ||
| 5. Click **Publish release**. | ||
|
|
||
| A pre-release also publishes to PyPI. For a pre-release, use a pre-release version such as | ||
| `1.2.0rc1`, so that pip does not install it by default. | ||
|
|
||
| ### 3. Approve the deployment | ||
|
|
||
| Publishing the release starts | ||
| [Publish to PyPI](https://github.com/lightly-ai/labelformat/actions/workflows/publish.yml). It | ||
| builds the package and waits for approval. Within a day, open the run and click **Review | ||
| deployments** > **Approve and deploy**. After a day, the package expires: run the workflow again. | ||
|
|
||
| ### 4. Check the release | ||
|
|
||
| Make sure that the new version is on [PyPI](https://pypi.org/project/labelformat/). | ||
|
|
||
| ## If the workflow fails | ||
|
|
||
| The workflow stops before the upload if the tag does not match the version in `pyproject.toml`, | ||
| or if the tag is not on `main`. Delete the release and the tag, then do step 2 again with the | ||
| correct tag. | ||
|
|
||
| For other failures, fix the cause and run | ||
| [Publish to PyPI](https://github.com/lightly-ai/labelformat/actions/workflows/publish.yml) manually | ||
| with the tag and `target` set to `pypi`. Files that are already on PyPI are skipped. | ||
|
|
||
| ## Rehearse on TestPyPI | ||
|
|
||
| Run [Publish to PyPI](https://github.com/lightly-ai/labelformat/actions/workflows/publish.yml) | ||
| manually from `main` with an existing tag and `target` set to `testpypi`. If the version is | ||
| already on TestPyPI, the upload is skipped, so only a new version tests the upload. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| name: Publish to PyPI | ||
|
|
||
| on: | ||
| # Publishing a GitHub release publishes the package, also a pre-release. | ||
| release: | ||
| types: [published] | ||
| # Retry hatch for a failed run, and a TestPyPI rehearsal of the whole path. | ||
| workflow_dispatch: | ||
| inputs: | ||
| tag: | ||
| description: Tag to publish, e.g. v1.2.3. | ||
| type: string | ||
| required: true | ||
| target: | ||
| description: Index to publish to. "testpypi" rehearses the whole path. | ||
| type: choice | ||
| options: [pypi, testpypi] | ||
| default: pypi | ||
|
|
||
| permissions: {} | ||
|
|
||
| # One run per tag and index. Not cancel-in-progress, because cancelling a run | ||
| # mid-upload is worse than waiting. GitHub keeps only the newest pending run. | ||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ inputs.tag || github.event.release.tag_name }}-${{ inputs.target || 'pypi' }} | ||
| cancel-in-progress: false | ||
|
|
||
| env: | ||
| TAG: ${{ inputs.tag || github.event.release.tag_name }} | ||
| TARGET: ${{ inputs.target || 'pypi' }} | ||
|
|
||
| jobs: | ||
| # Runs the tagged commit's own code, so it must not be able to mint a PyPI | ||
| # credential: no `id-token: write`, and all it hands onwards is the artifact. | ||
| build: | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 15 | ||
| permissions: | ||
| contents: read | ||
| steps: | ||
| # `refs/tags/` because actions/checkout resolves an unqualified ref as a | ||
| # branch first, and a branch with the tag's name would be built instead. | ||
| - name: Checkout the release commit | ||
| uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 | ||
| with: | ||
| ref: refs/tags/${{ env.TAG }} | ||
| # Full history, for the check that the tag is on main. | ||
| fetch-depth: 0 | ||
| persist-credentials: false | ||
|
|
||
| - name: Set up Python | ||
| uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0 | ||
| with: | ||
| python-version: "3.15" | ||
|
|
||
| - name: Assert the tag matches the version it points at | ||
| run: | | ||
| set -euo pipefail | ||
| version="$(python3 -c 'import tomllib; print(tomllib.load(open("pyproject.toml", "rb"))["project"]["version"])')" | ||
| if [ "v${version}" != "${TAG}" ]; then | ||
| echo "::error::${TAG} points at a commit whose version is ${version}." | ||
| exit 1 | ||
| fi | ||
|
|
||
| - name: Assert the tag is on main | ||
| run: | | ||
| set -euo pipefail | ||
| if ! git merge-base --is-ancestor HEAD origin/main; then | ||
| echo "::error::${TAG} points at a commit that is not on main." | ||
| exit 1 | ||
| fi | ||
|
|
||
| # No enable-cache, unlike the test workflow: a cache written by another | ||
| # workflow would be an input to what gets published. | ||
| - name: Set up Uv | ||
| uses: astral-sh/setup-uv@d0cc045d04ccac9d8b7881df0226f9e82c39688e # v6.8.0 | ||
| with: | ||
| version: "0.10.4" | ||
|
|
||
| # Without UV_PYTHON, uv build uses .python-version (3.8). | ||
| - name: Build the distributions | ||
| run: make build | ||
| env: | ||
| UV_PYTHON: "3.15" | ||
|
|
||
| # A day, as releasers approve at once. A later approval fails, see RELEASE.md. | ||
| - name: Upload the distributions | ||
| uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 | ||
| with: | ||
| name: dist | ||
| path: dist | ||
| if-no-files-found: error | ||
| retention-days: 1 | ||
|
|
||
| publish: | ||
| needs: build | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 15 | ||
| # PyPI's trusted publisher checks the OIDC claim against this workflow file | ||
| # and environment, not the branch. The environment's required reviewers and | ||
| # deployment rules stop a workflow edited on a branch, see RELEASE.md. | ||
| environment: ${{ inputs.target || 'pypi' }} | ||
| permissions: | ||
| id-token: write | ||
| steps: | ||
| - name: Download the distributions | ||
| uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 | ||
| with: | ||
| name: dist | ||
| path: dist | ||
|
|
||
| # Not `uv publish`, which uploads PEP 740 attestations but does not create | ||
| # them. Given no password, so a misconfigured publisher fails here rather | ||
| # than falling back to a token. `skip-existing` lets a failed run be retried. | ||
| - name: Publish to the index | ||
| uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3 | ||
| with: | ||
| # `/legacy/` is the name of PyPI's only upload API. | ||
| repository-url: >- | ||
| ${{ env.TARGET == 'pypi' && 'https://upload.pypi.org/legacy/' | ||
| || 'https://test.pypi.org/legacy/' }} | ||
| skip-existing: true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.