diff --git a/.github/RELEASE.md b/.github/RELEASE.md new file mode 100644 index 0000000..699f339 --- /dev/null +++ b/.github/RELEASE.md @@ -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. diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..4a2510c --- /dev/null +++ b/.github/workflows/publish.yml @@ -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 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.