From 62fbfcdb5b8a850fd2989429531f453b0cf109c7 Mon Sep 17 00:00:00 2001 From: iunir <38693523+iunir@users.noreply.github.com> Date: Wed, 25 Nov 2026 17:25:36 +0100 Subject: [PATCH 1/6] Publish to PyPI with trusted publishing --- .github/workflows/publish.yml | 107 ++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 .github/workflows/publish.yml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..2ab8b8d --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,107 @@ +name: Publish to PyPI + +on: + # Publishing a GitHub release publishes the package. + 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: {} + +# Queued rather than cancelled: cancelling a run mid-upload is worse than +# making a second run for the same tag wait. +concurrency: + group: ${{ github.workflow }}-${{ inputs.tag || github.event.release.tag_name }} + 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@v6 + with: + ref: refs/tags/${{ env.TAG }} + persist-credentials: false + + - name: Set up Python + uses: actions/setup-python@v6 + with: + python-version: "3.12" + + - 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 + + # 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@v6 + with: + version: "0.10.4" + + - name: Build the distributions + run: make build + + # A week, to outlast the pypi environment's approval gate. + - name: Upload the distributions + uses: actions/upload-artifact@v4 + with: + name: dist + path: dist + if-no-files-found: error + retention-days: 7 + + publish: + needs: build + runs-on: ubuntu-latest + timeout-minutes: 15 + # PyPI's trusted publisher checks the OIDC claim against this workflow file + # and environment, so only this job can publish. + environment: ${{ inputs.target || 'pypi' }} + permissions: + id-token: write + steps: + - name: Download the distributions + uses: actions/download-artifact@v4 + 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@v1.14.3 + with: + repository-url: >- + ${{ env.TARGET == 'pypi' && 'https://upload.pypi.org/legacy/' + || 'https://test.pypi.org/legacy/' }} + skip-existing: true From 3ff56c0d32a0c550903b53bc9562fa8f3d15dc74 Mon Sep 17 00:00:00 2001 From: iunir <38693523+iunir@users.noreply.github.com> Date: Wed, 25 Nov 2026 17:44:51 +0100 Subject: [PATCH 2/6] Document the release process --- .github/RELEASE.md | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 .github/RELEASE.md diff --git a/.github/RELEASE.md b/.github/RELEASE.md new file mode 100644 index 0000000..8d38790 --- /dev/null +++ b/.github/RELEASE.md @@ -0,0 +1,56 @@ +# 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. + +## 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`: + +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**. + +### 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 from the tag and waits for approval of the `pypi` environment, if the +environment has required reviewers. Approve it. + +### 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`. +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 with an existing tag and `target` set to `testpypi`. This does not need a GitHub release. +If the version is already on TestPyPI, the upload is skipped. From 64c9b4d188ecc3f490ba8e287e009699b3561626 Mon Sep 17 00:00:00 2001 From: iunir <38693523+iunir@users.noreply.github.com> Date: Wed, 25 Nov 2026 17:45:02 +0100 Subject: [PATCH 3/6] Link the release process from the README --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 0d4ea6b..adf814f 100644 --- a/README.md +++ b/README.md @@ -520,6 +520,10 @@ make docs-serve make docs-deploy ``` +#### Release + +See [RELEASE.md](.github/RELEASE.md). + ## Maintained By [Lightly](https://www.lightly.ai) is a spin-off from ETH Zurich that helps companies build efficient active learning pipelines to select the most relevant data for their models. From 90d338ce6e281606d7821b6668f279ca9bc0e33a Mon Sep 17 00:00:00 2001 From: iunir <38693523+iunir@users.noreply.github.com> Date: Wed, 25 Nov 2026 18:03:58 +0100 Subject: [PATCH 4/6] Harden the publish workflow Pin actions to commit SHAs, check that the tag is on main, build with Python 3.12, keep the build artifact for 30 days, and scope concurrency per index. Document the environment protection, the TestPyPI setup, and that pre-releases publish to PyPI. --- .github/RELEASE.md | 33 +++++++++++++++++++++++------ .github/workflows/publish.yml | 40 +++++++++++++++++++++++------------ 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/.github/RELEASE.md b/.github/RELEASE.md index 8d38790..4755140 100644 --- a/.github/RELEASE.md +++ b/.github/RELEASE.md @@ -7,6 +7,19 @@ workflow uploads the package with [trusted publishing](https://docs.pypi.org/trusted-publishers/). Nobody needs a PyPI token or password. +## One-time setup + +Without this setup, anyone with write access can publish. + +1. In **Settings** > **Environments**, create the `pypi` and `testpypi` environments. A job + that names a missing environment creates it without protection. +2. For each environment, add the release maintainers as **Required reviewers**. Under + **Deployment branches and tags**, allow only the branch `main` and the tag pattern `v*`. +3. On [PyPI](https://pypi.org/manage/project/labelformat/settings/publishing/) and + [TestPyPI](https://test.pypi.org/manage/project/labelformat/settings/publishing/), add a + trusted publisher: owner `lightly-ai`, repository `labelformat`, workflow `publish.yml`, and + environment `pypi` or `testpypi`. + ## Release process ### 1. Bump the version @@ -21,7 +34,8 @@ Merge it. ### 2. Create the GitHub release -Create a [release](https://github.com/lightly-ai/labelformat/releases/new) from `main`: +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`. @@ -29,12 +43,15 @@ Create a [release](https://github.com/lightly-ai/labelformat/releases/new) from 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 from the tag and waits for approval of the `pypi` environment, if the -environment has required reviewers. Approve it. +builds the package from the tag and waits for approval of the `pypi` environment. Approve it +within 30 days, after which the run expires. ### 4. Check the release @@ -42,8 +59,9 @@ 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`. -Delete the release and the tag, then do step 2 again with the correct tag. +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 @@ -52,5 +70,6 @@ with the tag and `target` set to `pypi`. Files that are already on PyPI are skip ## Rehearse on TestPyPI Run [Publish to PyPI](https://github.com/lightly-ai/labelformat/actions/workflows/publish.yml) -manually with an existing tag and `target` set to `testpypi`. This does not need a GitHub release. -If the version is already on TestPyPI, the upload is skipped. +manually from `main` with an existing tag and `target` set to `testpypi`. This does not need a +GitHub release. If the version is already on TestPyPI, the upload is skipped and the run still +passes, so only a version that is new on TestPyPI tests the upload. diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 2ab8b8d..ef066e5 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,7 +1,7 @@ name: Publish to PyPI on: - # Publishing a GitHub release publishes the package. + # 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. @@ -19,10 +19,10 @@ on: permissions: {} -# Queued rather than cancelled: cancelling a run mid-upload is worse than -# making a second run for the same tag wait. +# 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 }} + group: ${{ github.workflow }}-${{ inputs.tag || github.event.release.tag_name }}-${{ inputs.target || 'pypi' }} cancel-in-progress: false env: @@ -41,13 +41,15 @@ jobs: # `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@v6 + 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@v6 + uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0 with: python-version: "3.12" @@ -60,37 +62,49 @@ jobs: 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@v6 + 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.12" - # A week, to outlast the pypi environment's approval gate. + # 30 days, the longest that a deployment waits for approval. - name: Upload the distributions - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 with: name: dist path: dist if-no-files-found: error - retention-days: 7 + retention-days: 30 publish: needs: build runs-on: ubuntu-latest timeout-minutes: 15 # PyPI's trusted publisher checks the OIDC claim against this workflow file - # and environment, so only this job can publish. + # 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@v4 + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 with: name: dist path: dist @@ -99,7 +113,7 @@ jobs: # 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@v1.14.3 + uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3 with: repository-url: >- ${{ env.TARGET == 'pypi' && 'https://upload.pypi.org/legacy/' From bf014bb3f07008f2d01de0064b3156e6123cd6d5 Mon Sep 17 00:00:00 2001 From: iunir <38693523+iunir@users.noreply.github.com> Date: Thu, 26 Nov 2026 09:48:15 +0100 Subject: [PATCH 5/6] Address review: shorter retention, Python 3.15, approval steps Keep the build artifact for a day, build with Python 3.15, explain the /legacy/ upload URL, and say how to approve the deployment. --- .github/RELEASE.md | 9 ++++----- .github/workflows/publish.yml | 9 +++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/RELEASE.md b/.github/RELEASE.md index 4755140..e503628 100644 --- a/.github/RELEASE.md +++ b/.github/RELEASE.md @@ -50,8 +50,8 @@ A pre-release also publishes to PyPI. For a pre-release, use a pre-release versi Publishing the release starts [Publish to PyPI](https://github.com/lightly-ai/labelformat/actions/workflows/publish.yml). It -builds the package from the tag and waits for approval of the `pypi` environment. Approve it -within 30 days, after which the run expires. +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 @@ -70,6 +70,5 @@ with the tag and `target` set to `pypi`. Files that are already on PyPI are skip ## 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`. This does not need a -GitHub release. If the version is already on TestPyPI, the upload is skipped and the run still -passes, so only a version that is new on TestPyPI tests the upload. +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. diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index ef066e5..4a2510c 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -51,7 +51,7 @@ jobs: - name: Set up Python uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0 with: - python-version: "3.12" + python-version: "3.15" - name: Assert the tag matches the version it points at run: | @@ -81,16 +81,16 @@ jobs: - name: Build the distributions run: make build env: - UV_PYTHON: "3.12" + UV_PYTHON: "3.15" - # 30 days, the longest that a deployment waits for approval. + # 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: 30 + retention-days: 1 publish: needs: build @@ -115,6 +115,7 @@ jobs: - 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/' }} From 13ffbca8c64d7de56fbfd7f59394fb6977f3c1f6 Mon Sep 17 00:00:00 2001 From: iunir <38693523+iunir@users.noreply.github.com> Date: Thu, 26 Nov 2026 09:50:45 +0100 Subject: [PATCH 6/6] Replace the one-time setup with a note on the repository settings The environments exist already. Keep only why their protection must stay. --- .github/RELEASE.md | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/.github/RELEASE.md b/.github/RELEASE.md index e503628..699f339 100644 --- a/.github/RELEASE.md +++ b/.github/RELEASE.md @@ -7,18 +7,11 @@ workflow uploads the package with [trusted publishing](https://docs.pypi.org/trusted-publishers/). Nobody needs a PyPI token or password. -## One-time setup - -Without this setup, anyone with write access can publish. - -1. In **Settings** > **Environments**, create the `pypi` and `testpypi` environments. A job - that names a missing environment creates it without protection. -2. For each environment, add the release maintainers as **Required reviewers**. Under - **Deployment branches and tags**, allow only the branch `main` and the tag pattern `v*`. -3. On [PyPI](https://pypi.org/manage/project/labelformat/settings/publishing/) and - [TestPyPI](https://test.pypi.org/manage/project/labelformat/settings/publishing/), add a - trusted publisher: owner `lightly-ai`, repository `labelformat`, workflow `publish.yml`, and - environment `pypi` or `testpypi`. +## 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