Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
22 changes: 22 additions & 0 deletions scripts/release-version.py
Original file line number Diff line number Diff line change
@@ -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}")
71 changes: 71 additions & 0 deletions tests/test_release_version.py
Original file line number Diff line number Diff line change
@@ -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()
Loading