From 46c4cdd4618aa65916319dad40967af9191e8cb6 Mon Sep 17 00:00:00 2001 From: masquare <11543761+masquare@users.noreply.github.com> Date: Thu, 12 Nov 2026 18:58:52 +0100 Subject: [PATCH] ci(release): tag and create the GitHub release when the PR merges When the release PR is merged to main, the same workflow builds and smoke-tests the wheel and the sdist of each released package, creates the tag and the GitHub release with the notes from the merged PR, and attaches the files. Uploading to PyPI stays with publish.yml for now. --- .github/scripts/release.py | 121 +++++++++++++++++++++++++++++++++- .github/workflows/release.yml | 48 +++++++++++++- 2 files changed, 164 insertions(+), 5 deletions(-) diff --git a/.github/scripts/release.py b/.github/scripts/release.py index b0b63928c..ee4fa7cc1 100644 --- a/.github/scripts/release.py +++ b/.github/scripts/release.py @@ -9,6 +9,11 @@ - ``prepare`` sets the versions, writes the release notes, and opens the release PR. The PR body has the notes between ```` markers, so you can edit them there. +- ``released`` finds the packages whose version the merged release PR changed. +- ``smoke`` imports each built wheel and sdist in an isolated environment. It makes sure that each + file records the commit that it was built from. +- ``github-release`` creates the tag and the GitHub release of each released package. It reads the + notes from the merged PR and attaches the files in ``dist/``. PyGithub makes the GitHub calls and GitPython makes the git calls. Only ``uv`` and git-cliff run as subprocesses, because they have no Python API. The option ``--dry-run`` does not push and does not @@ -22,11 +27,12 @@ from pathlib import Path import re import subprocess # ruff: ignore[suspicious-subprocess-import] - the commands are fixed argument vectors +import tomllib from typing import NoReturn from connector_notes import commits, notes as connector_notes, repo from git import Repo -from github import Auth, Github +from github import Auth, Github, UnknownObjectException from github.Repository import Repository @@ -34,6 +40,7 @@ CONNECTORS = "packages/timenet-connectors/pyproject.toml" FLOOR = re.compile(r'"timenet\[build\]>=[^"]*"') VERSION = re.compile(r"\d+\.\d+\.\d+") +NOTES = re.compile(r"\n(?P.*?)\n", re.S) EXPLAIN = ( "Merge this PR to tag each package below and to create its GitHub release. The release notes are\n" "the text between the `release-notes` markers. Edit the notes here before you merge. The same run\n" @@ -221,6 +228,106 @@ def prepare(package: str, requested: str, *, dry_run: bool) -> None: open_pr(branch, title, f"{EXPLAIN}\n---\n\n{warning}{sections}", dry_run=dry_run) +def released() -> None: + """Find the packages whose version the merged release PR changed. Write them to the outputs.""" + git = repo() + found = {} + for package in PACKAGES: + version = version_of(package) + before = (git.commit("HEAD^").tree / f"packages/{package}/pyproject.toml").data_stream.read().decode() + if version == tomllib.loads(before)["project"]["version"]: + continue + if not VERSION.fullmatch(version): + fail(f"{package} is at {version}, not X.Y.Z.") + found[package] = version + if not found: + fail("This PR changed no package version.") + append("GITHUB_OUTPUT", "released=" + " ".join(f"{name}={version}" for name, version in found.items()) + "\n") + + +def merged_notes(remote: Repository, sha: str) -> dict[str, str]: + """Read the release notes from the body of the merged release PR. + + Returns: + The notes of each package. + """ + for pull in remote.get_commit(sha).get_pulls(): + if pull.merged and pull.head.ref.startswith("release/"): + return { + match["package"]: match["text"] for match in NOTES.finditer((pull.body or "").replace("\r\n", "\n")) + } + fail(f"No merged release PR holds {sha}.") + + +def github_release(entries: dict[str, str]) -> None: + """Create the tag and the GitHub release of each package. Attach its files from ``dist/``. + + A re-run creates only what is missing. + """ + git = repo() + sha = git.head.commit.hexsha + remote = github() + notes = merged_notes(remote, sha) + for package, version in entries.items(): + tag = f"{package}-v{version}" + if package not in notes: + fail(f"The release PR has no notes for {package}.") + try: + release = remote.get_release(tag) + if remote.get_git_ref(f"tags/{tag}").object.sha != sha: + fail(f"{tag} exists on another commit.") + except UnknownObjectException: + release = remote.create_git_release(tag, f"{package} {version}", notes[package], target_commitish=sha) + attached = {asset.name for asset in release.get_assets()} + stem = package.replace("-", "_") + files = [*Path("dist").glob(f"{stem}-{version}-*.whl"), *Path("dist").glob(f"{stem}-{version}.tar.gz")] + for file in files: + if file.name not in attached: + release.upload_asset(str(file)) + indexes = f"[TestPyPI](https://test.pypi.org/project/{package}/{version}/), [PyPI](https://pypi.org/project/{package}/{version}/)" + append("GITHUB_STEP_SUMMARY", f"- {package} {version}: {release.html_url} ({indexes} once uploaded)\n") + + +SMOKE = """ +import {module} +from timenet.manifest import GitSource +from timenet.provenance import PackageSnapshot +commit = PackageSnapshot.read({dist!r}).source +expected = GitSource(sha={sha!r}, contains_local_changes=False) +assert commit == expected, (commit, expected) +print('smoke ok:', {module}.__name__, commit) +""" + + +def smoke() -> None: + """Import each built wheel and sdist. Make sure that each file records this commit, clean. + + timenet-connectors uses the timenet wheel from the same run, if there is one. If there is none, + timenet comes from the index. Then timenet must be on the index already. + """ + sha = repo().head.commit.hexsha + stems = {package: package.replace("-", "_") for package in PACKAGES} + core_wheels = sorted(Path("dist").glob(f"{stems[PACKAGES[0]]}-*.whl")) + for package, stem in stems.items(): + extras = core_wheels if package == PACKAGES[1] else [] + for artifact in sorted(Path("dist").glob(f"{stem}-*")): + command = ["uv", "run", "--isolated", "--no-project"] + for file in (*extras, artifact): + command += ["--with", str(file)] + print(f"::group::{artifact.name}") + print(run(*command, "python", "-c", SMOKE.format(module=stem, dist=package, sha=sha))) + print("::endgroup::") + + +def parse(text: str) -> dict[str, str]: + """Read ``package=version`` pairs. + + Returns: + The versions by package. + """ + return dict(pair.split("=") for pair in text.split()) + + if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) commands = parser.add_subparsers(dest="command", required=True) @@ -228,5 +335,15 @@ def prepare(package: str, requested: str, *, dry_run: bool) -> None: prepare_parser.add_argument("--package", choices=["both", *PACKAGES], required=True) prepare_parser.add_argument("--version", default="") prepare_parser.add_argument("--dry-run", action="store_true") + commands.add_parser("released") + commands.add_parser("smoke") + commands.add_parser("github-release").add_argument("--released", required=True) args = parser.parse_args() - prepare(args.package, args.version, dry_run=args.dry_run) + if args.command == "prepare": + prepare(args.package, args.version, dry_run=args.dry_run) + elif args.command == "released": + released() + elif args.command == "smoke": + smoke() + else: + github_release(parse(args.released)) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 48fc76ed5..1ec281c33 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -2,13 +2,18 @@ name: Release # Release timenet, timenet-connectors, or both. Each package has its own version and its own tag, # -vX.Y.Z. The steps with logic are in .github/scripts/release.py. -# Run this workflow on main and select the package. It opens a release PR that sets the versions. -# The PR body has the release notes between release-notes markers. Edit the notes there. +# 1. Run this workflow on main and select the package. It opens a release PR that sets the +# versions. The PR body has the release notes between release-notes markers. Edit the notes +# there. The release uses the text in the merged PR. +# 2. Merge the release PR. The same workflow then builds and tests the wheel and the sdist of each +# package. It creates the tag and the GitHub release with the built files attached. +# The upload to TestPyPI and PyPI is still done by publish.yml. # If you release both packages, the timenet floor of timenet-connectors goes up. If you release one # package, the release PR warns when the other package has changes that the release leaves out. # The release PR fixes the versions. If main needs a bigger version bump before you merge, close # the PR and run the workflow again. -# Only admins and maintainers can start a release. You can re-run each job. +# Only admins and maintainers can start a release, merge the release PR, or re-run a job. You can +# re-run each job. Steps that already happened do not run again. # # Prerequisites: # - A GitHub App with write access to contents and pull requests, installed on this repository. @@ -31,6 +36,8 @@ on: description: Version to release, for example 0.12.0. Leave empty to compute it from the commits. required: false type: string + push: + branches: [main] permissions: contents: read @@ -40,6 +47,9 @@ env: jobs: authorize: + if: >- + github.event_name == 'workflow_dispatch' + || (github.event_name == 'push' && startsWith(github.event.head_commit.message, 'chore(release):')) runs-on: ubuntu-latest steps: - name: Allow only admins and maintainers @@ -55,6 +65,7 @@ jobs: prepare: needs: authorize + if: github.event_name == 'workflow_dispatch' runs-on: ubuntu-latest steps: - name: Guard to main @@ -80,3 +91,34 @@ jobs: PACKAGE: ${{ inputs.package }} REQUESTED: ${{ inputs.version }} run: uv run .github/scripts/release.py prepare --package "$PACKAGE" --version "$REQUESTED" + + release: + needs: authorize + if: github.event_name == 'push' + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: read + steps: + - uses: actions/checkout@v5 + with: + ref: ${{ github.sha }} + fetch-depth: 0 + - uses: astral-sh/setup-uv@v8.2.0 + - name: Find the packages this PR released + id: released + run: uv run .github/scripts/release.py released + - name: Build + env: + RELEASED: ${{ steps.released.outputs.released }} + run: | + for entry in $RELEASED; do + uv run scripts/build_dists.py --package "${entry%=*}" + done + - name: Smoke test built artifacts + run: uv run .github/scripts/release.py smoke + - name: Tag and create the GitHub releases + env: + GH_TOKEN: ${{ github.token }} + RELEASED: ${{ steps.released.outputs.released }} + run: uv run .github/scripts/release.py github-release --released "$RELEASED"