From b38273cdac13d7e299d26f019e0f8df74b75b4d5 Mon Sep 17 00:00:00 2001 From: Mohsen Azimi <543633+mohsen1@users.noreply.github.com> Date: Thu, 15 Oct 2026 16:31:32 +0200 Subject: [PATCH] Publish releases with GitHub Actions and registry OIDC --- .github/workflows/ci.yml | 23 ++++++++++++ .github/workflows/release.yml | 55 +++++++++++++++++++++++++++ README.md | 11 ++++++ scripts/release-version.py | 22 +++++++++++ tests/test_release_version.py | 71 +++++++++++++++++++++++++++++++++++ 5 files changed, 182 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/release.yml create mode 100644 scripts/release-version.py create mode 100644 tests/test_release_version.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..6c0bdad5 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,23 @@ +name: Checks +on: + pull_request: + push: + branches: [main] + workflow_dispatch: +permissions: + contents: read +concurrency: + group: ci-${{ github.ref }} + cancel-in-progress: true +jobs: + check: + runs-on: ubuntu-24.04 + timeout-minutes: 30 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + with: + python-version: '3.13.12' + - run: python scripts/check.py --artifacts artifacts diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 00000000..afaec532 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,55 @@ +name: Release +on: + push: + tags: ['v*'] + workflow_dispatch: + inputs: + publish: + description: Publish the checked package version + type: boolean + default: false +permissions: + contents: read +concurrency: + group: release + cancel-in-progress: false +jobs: + build: + runs-on: ubuntu-24.04 + timeout-minutes: 30 + outputs: + version: ${{ steps.version.outputs.version }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + persist-credentials: false + - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + with: + python-version: '3.13.12' + - id: version + run: python scripts/release-version.py + - run: python scripts/check.py --artifacts artifacts + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: package + path: artifacts/* + if-no-files-found: error + publish: + if: github.event_name == 'push' || inputs.publish + needs: build + runs-on: ubuntu-24.04 + timeout-minutes: 10 + environment: pypi + permissions: + contents: read + id-token: write + steps: + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: package + path: artifacts + - uses: pypa/gh-action-pypi-publish@dc37677b2e1c63e2034f94d8a5b11f265b73ba33 # v1.14.2 + with: + packages-dir: artifacts/ + attestations: true diff --git a/README.md b/README.md index 27710c08..25710171 100644 --- a/README.md +++ b/README.md @@ -30,3 +30,14 @@ Licensed under the Apache License, Version 2.0. The generated models under The repository bundles its public API contract in `openapi.public.json`. Run `python3 scripts/check.py` to install pinned tools, regenerate the client, run checks and verify built packages. + +## Publishing + +GitHub Actions runs the checks on pull requests and main. The `Release` workflow +builds checked artifacts without publishing credentials. Run it on main with +`publish=false` to verify the release path. A `vX.Y.Z` tag matching the package +version, or a main-branch dispatch with `publish=true`, publishes after the +`pypi` environment approves the job. Release commits must belong to main. + +Publishing uses GitHub OIDC and the repository's `release.yml` trusted publisher. +No registry token is stored in GitHub. diff --git a/scripts/release-version.py b/scripts/release-version.py new file mode 100644 index 00000000..a2b46c5f --- /dev/null +++ b/scripts/release-version.py @@ -0,0 +1,22 @@ +"""Admit a release only at its package version and a commit on main.""" + +import json +import os +import re +import subprocess +import tomllib +from pathlib import Path + +version = tomllib.loads(Path("pyproject.toml").read_text())["project"]["version"] +if not re.fullmatch(r"[0-9]+\.[0-9]+\.[0-9]+", version): + raise SystemExit("package version must be x.y.z") +ref = os.environ["GITHUB_REF"] +if ref not in ("refs/heads/main", f"refs/tags/v{version}"): + raise SystemExit("release requires main or a tag equal to the package version") +subprocess.run(["git", "fetch", "--quiet", "origin", "main"], check=True) +subprocess.run(["git", "merge-base", "--is-ancestor", "HEAD", "origin/main"], check=True) +if json.loads(Path("openapi.public.json").read_text())["info"]["version"] != version: + raise SystemExit("package and bundled public API versions differ") +with Path(os.environ["GITHUB_OUTPUT"]).open("a") as output: + output.write(f"version={version}\n") +print(f"Release version: {version}") diff --git a/tests/test_release_version.py b/tests/test_release_version.py new file mode 100644 index 00000000..86034486 --- /dev/null +++ b/tests/test_release_version.py @@ -0,0 +1,71 @@ +"""Release admission binds a version to the ref and main's ancestry.""" + +import json +import os +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +SCRIPT = Path(__file__).resolve().parents[1] / "scripts/release-version.py" + + +class ReleaseAdmission(unittest.TestCase): + def test_ref_version_and_main_ancestry(self) -> None: + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + + def git(*arguments: str) -> None: + subprocess.run(["git", *arguments], cwd=root, check=True, capture_output=True) + + git("init", "--quiet", "-b", "main") + git("config", "user.name", "Fixture") + git("config", "user.email", "fixture@example.com") + git("remote", "add", "origin", str(root)) + (root / "pyproject.toml").write_text('[project]\nversion = "1.2.3"\n') + (root / "openapi.public.json").write_text(json.dumps({"info": {"version": "1.2.3"}})) + git("add", ".") + git("commit", "--quiet", "-m", "fixture") + for ref, admitted in ( + ("refs/heads/main", True), + ("refs/tags/v1.2.3", True), + ("refs/tags/v9.9.9", False), + ("refs/heads/feature", False), + ): + with self.subTest(ref=ref): + output = root / "output" + output.unlink(missing_ok=True) + result = subprocess.run( + [sys.executable, str(SCRIPT)], + cwd=root, + env={**os.environ, "GITHUB_REF": ref, "GITHUB_OUTPUT": str(output)}, + capture_output=True, + text=True, + ) + self.assertEqual(result.returncode == 0, admitted, result.stderr) + if admitted: + self.assertEqual(output.read_text(), "version=1.2.3\n") + else: + self.assertFalse(output.exists()) + git("checkout", "--quiet", "-b", "feature") + (root / "change").write_text("unmerged") + git("add", ".") + git("commit", "--quiet", "-m", "unmerged") + result = subprocess.run( + [sys.executable, str(SCRIPT)], + cwd=root, + env={ + **os.environ, + "GITHUB_REF": "refs/tags/v1.2.3", + "GITHUB_OUTPUT": str(root / "rejected"), + }, + capture_output=True, + text=True, + ) + self.assertNotEqual(result.returncode, 0) + self.assertFalse((root / "rejected").exists()) + + +if __name__ == "__main__": + unittest.main()