diff --git a/.github/workflows/binding-publish-all.yml b/.github/workflows/binding-publish-all.yml index 8125f74..bfc0510 100644 --- a/.github/workflows/binding-publish-all.yml +++ b/.github/workflows/binding-publish-all.yml @@ -4,9 +4,21 @@ name: Publish bindings to every registry # the per-registry publish workflows (Go tags, PyPI, npm, Maven Central) side by # side and waits for them. Each registry is still published by its own workflow, # so npm and PyPI trusted publishing keep matching those workflow files, and the -# pypi and maven-central environments still ask for approval. +# pypi and maven-central environments still apply. They ask for approval only if +# required reviewers are set on them, which they are not today. +# +# It also runs by itself after binding generation succeeds on main, for every +# package and language, as a real run with setup_done and Maven auto publish on: +# the pull request card asked maintainers to do any registry setup before the +# merge. When another generation run is queued or running, this run publishes +# nothing, because that run starts publishing again when it finishes. So many +# merges at once lead to one publication, after the last generation. on: + workflow_run: + workflows: [Generate and commit Go, Python, TypeScript and Java bindings] + types: [completed] + branches: [main] workflow_dispatch: inputs: packages: @@ -50,22 +62,63 @@ concurrency: cancel-in-progress: false jobs: + gate: + # A run started by generation goes ahead only after a successful generation of main, and only when no other + # generation run is waiting or running: that one starts publishing again when it finishes. + if: >- + (github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main') || + (github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success' + && github.event.workflow_run.head_branch == 'main') + runs-on: ubuntu-24.04 + timeout-minutes: 5 + permissions: + actions: read + outputs: + publish: ${{ steps.check.outputs.publish }} + steps: + - name: Check for newer generation runs + id: check + shell: bash + env: + GH_TOKEN: ${{ github.token }} + REPO: ${{ github.repository }} + EVENT: ${{ github.event_name }} + run: | + set -euo pipefail + if [[ "$EVENT" != workflow_run ]]; then + echo "publish=true" >> "$GITHUB_OUTPUT" + exit 0 + fi + waiting=0 + for status in queued in_progress waiting pending requested; do + count="$(gh api "repos/$REPO/actions/workflows/binding-generate.yml/runs?branch=main&status=$status&per_page=1" --jq .total_count)" + waiting=$((waiting + count)) + done + if [[ "$waiting" -gt 0 ]]; then + echo "Another binding generation run is queued or running. It starts publishing when it finishes, so this run publishes nothing." >> "$GITHUB_STEP_SUMMARY" + echo "publish=false" >> "$GITHUB_OUTPUT" + else + echo "publish=true" >> "$GITHUB_OUTPUT" + fi + preflight: # Lists every selected package with its registry status, and stops a real run that needs registry setup first. - if: github.ref == 'refs/heads/main' + needs: gate + if: needs.gate.outputs.publish == 'true' runs-on: ubuntu-24.04 timeout-minutes: 10 outputs: languages: ${{ steps.plan.outputs.languages }} targets: ${{ steps.plan.outputs.targets }} + # A run started by generation has no inputs: every package and language, a real run, setup taken as done. env: - PACKAGES: ${{ inputs.packages }} - EXECUTE: ${{ !inputs.dry_run }} - GO: ${{ inputs.go }} - PYTHON: ${{ inputs.python }} - TYPESCRIPT: ${{ inputs.typescript }} - JAVA: ${{ inputs.java }} - SETUP_DONE: ${{ inputs.setup_done }} + PACKAGES: ${{ inputs.packages || 'all' }} + EXECUTE: ${{ github.event_name == 'workflow_run' || !inputs.dry_run }} + GO: ${{ github.event_name == 'workflow_run' || inputs.go }} + PYTHON: ${{ github.event_name == 'workflow_run' || inputs.python }} + TYPESCRIPT: ${{ github.event_name == 'workflow_run' || inputs.typescript }} + JAVA: ${{ github.event_name == 'workflow_run' || inputs.java }} + SETUP_DONE: ${{ github.event_name == 'workflow_run' || inputs.setup_done }} steps: - name: Check out repository @@ -112,7 +165,7 @@ jobs: needs: preflight if: needs.preflight.outputs.languages != '[]' runs-on: ubuntu-24.04 - # Covers environment approvals and, with Maven auto publish, waiting for packages to reach Maven Central. + # Covers environment approvals, if reviewers are set, and with Maven auto publish, waiting for earlier waves. timeout-minutes: 240 strategy: fail-fast: false @@ -123,8 +176,8 @@ jobs: # Starting another workflow with workflow_dispatch. actions: write env: - EXECUTE: ${{ !inputs.dry_run }} - MAVEN_AUTO_PUBLISH: ${{ inputs.maven_auto_publish }} + EXECUTE: ${{ github.event_name == 'workflow_run' || !inputs.dry_run }} + MAVEN_AUTO_PUBLISH: ${{ github.event_name == 'workflow_run' || inputs.maven_auto_publish }} LANGUAGE: ${{ matrix.language }} # Only the versions preflight found missing from this registry, not every selected package. TARGETS: ${{ fromJSON(needs.preflight.outputs.targets)[matrix.language] }} diff --git a/tooling/extension-bindings/README.md b/tooling/extension-bindings/README.md index 1ad45e2..8b0df52 100644 --- a/tooling/extension-bindings/README.md +++ b/tooling/extension-bindings/README.md @@ -162,6 +162,23 @@ selected packages to Go, PyPI, npm and Maven Central from one dispatch on `main` It takes package keys (or `all`), a checkbox per language, `dry_run`, and `maven_auto_publish`. +It also runs by itself after **Generate and commit** succeeds on `main`: every +package and language, a real run, with `setup_done` and `maven_auto_publish` +on, because the pull request card asked maintainers to do any registry setup +before the merge. If another generation run is queued or running, it publishes +nothing and leaves the job to the run that finishes last, so many merges at once +lead to one publication. Preflight only sends each registry the versions it does +not have, so a run with nothing new ends after the lookups. Nobody has to press +anything: the `pypi` and `maven-central` environments have no required reviewers. +Only a new npm package still waits for approval on npmjs.com. To add a manual +gate later, set required reviewers on those environments in the repository +settings; the run then waits for one of them. + +Go tags go on the commit that was packaged. Main may move on during the run, for +example with a docs change, as long as that commit stays on main and none of the +published modules changed since; otherwise the Go publish stops and asks for a +new run. + 1. A preflight job looks each selected package up on its registry and writes a table to the run summary: already published (skipped), new version, or first release. A first release needs a one-time setup that the workflow cannot do: @@ -185,8 +202,9 @@ It takes package keys (or `all`), a checkbox per language, `dry_run`, and (`binding-promote.yml`, `binding-publish-pypi.yml`, `binding-publish-npm.yml`, `binding-publish-maven.yml`) and waits for it. Because each registry is still published by its own workflow, trusted publishing keeps matching those files, - and the `pypi` and `maven-central` environments still ask for approval. The - per-registry workflows also stay usable on their own. + and the `pypi` and `maven-central` environments still apply (they ask for + approval only if required reviewers are set on them). The per-registry + workflows also stay usable on their own. Before the merge, **Publishing setup card** (`publish-setup-card.yml`) comments on every pull request that can change bindings with the npm and PyPI diff --git a/tooling/extension-bindings/ci/publish.py b/tooling/extension-bindings/ci/publish.py index fcd0040..85f81c2 100644 --- a/tooling/extension-bindings/ci/publish.py +++ b/tooling/extension-bindings/ci/publish.py @@ -200,14 +200,30 @@ def verify_archives(root: Path, output: Path, binding: GoBinding) -> None: raise ValueError(f"{binding.target}: {suffix} bytes differ from committed source") +def check_main(root: Path, source: str, bindings: list[GoBinding]) -> None: + """Main can move on while a publication runs, for example with a docs change. Tags still go on the packaged + commit, so that commit must be on main, and main must not have changed any of these modules since.""" + head = git(root, "ls-remote", "origin", "refs/heads/main").split()[0] + if head == source: + return + git(root, "fetch", "--no-tags", "origin", "refs/heads/main") + current = git(root, "rev-parse", "FETCH_HEAD").strip() + if subprocess.run(["git", "merge-base", "--is-ancestor", source, current], cwd=root).returncode != 0: + raise ValueError("main no longer contains the packaged commit; rerun publication on its current head") + changed = git(root, "diff", "--name-only", source, current, "--", *(binding.directory for binding in bindings)) + if changed.strip(): + raise ValueError( + f"main moved and changed {changed.split()[0]} after packaging; rerun publication on its current head" + ) + + def preflight(root: Path, bindings: list[GoBinding], output: Path, github: GitHub) -> tuple[str, dict, list[str]]: source = clean_commit(root) publication = read_document(output / "publication.yaml") if publication != {"sourceCommit": source, "targets": [binding.target for binding in bindings]}: raise ValueError("publication artifacts do not identify this exact checkout and selected targets") + check_main(root, source, bindings) refs = dict((line.split()[1], line.split()[0]) for line in git(root, "ls-remote", "origin").splitlines()) - if refs.get("refs/heads/main") != source: - raise ValueError("main moved after packaging; rerun publication on its current head") plan = read_document(output / "release-plan.yaml") planned = {entry["target"]: entry for entry in plan["releases"]} if set(planned) != {binding.target for binding in bindings}: @@ -255,8 +271,7 @@ def promote(root: Path, bindings: list[GoBinding], output: Path, github: GitHub, if dry_run: summarize("Dry run passed: committed source, package versions, archives, live discovery pages, tags, and release assets verified. No publication was performed.") return - if git(root, "ls-remote", "origin", "refs/heads/main").split()[0] != source: - raise ValueError("main moved during preflight; rerun publication") + check_main(root, source, bindings) for binding in bindings: if binding.tag in new_tags: git(root, "-c", "user.name=github-actions[bot]", diff --git a/tooling/extension-bindings/ci/test_publish.py b/tooling/extension-bindings/ci/test_publish.py index 767fd36..b8d1be8 100644 --- a/tooling/extension-bindings/ci/test_publish.py +++ b/tooling/extension-bindings/ci/test_publish.py @@ -209,16 +209,38 @@ def test_missing_or_mismatched_discovery_is_rejected(self): with patch("publish.urlopen", return_value=io.BytesIO(go_page(binding).encode())): check_discovery(binding) - def test_remote_main_movement_is_rejected(self): + def move_main(self, path: str, content: str = "new head\n", rewrite: bool = False) -> None: other = self.work / "other" - git(self.root, "clone", "--branch", "main", str(self.remote), str(other)) - git(other, "config", "user.name", "Test") - git(other, "config", "user.email", "test@example.invalid") - (other / "new-input.txt").write_text("new head\n") + if not other.exists(): + git(self.root, "clone", "--branch", "main", str(self.remote), str(other)) + git(other, "config", "user.name", "Test") + git(other, "config", "user.email", "test@example.invalid") + if rewrite: + # History that does not contain the packaged commit, as after a force push. + git(other, "checkout", "--orphan", "rewritten") + target = other / path + target.parent.mkdir(parents=True, exist_ok=True) + target.write_text(content) git(other, "add", ".") git(other, "commit", "-m", "Main moved") - git(other, "push", "origin", "main") - with self.assertRaisesRegex(ValueError, "main moved"): + git(other, "push", "--force", "origin", "HEAD:main") + + def test_main_moving_without_touching_the_modules_is_accepted(self): + # A docs change merged while publishing does not change what the tags point at. + self.move_main("docs/notes.md") + promote(self.root, self.bindings, self.output, self.github, False) + refs = git(self.root, "ls-remote", "origin") + for binding in self.bindings: + self.assertIn(binding.tag, refs) + + def test_main_changing_a_module_after_packaging_is_rejected(self): + self.move_main(f"{self.bindings[0].directory}/later.go", "package later\n") + with self.assertRaisesRegex(ValueError, "main moved and changed .*later.go"): + preflight(self.root, self.bindings, self.output, self.github) + + def test_main_without_the_packaged_commit_is_rejected(self): + self.move_main("docs/notes.md", rewrite=True) + with self.assertRaisesRegex(ValueError, "no longer contains the packaged commit"): preflight(self.root, self.bindings, self.output, self.github) def test_dependency_version_mismatch_is_rejected(self):