From 9dbc867502d7ece5b71d3630dab5c3601790ccf0 Mon Sep 17 00:00:00 2001 From: scuttletoby <123843277+scuttletoby@users.noreply.github.com> Date: Sat, 14 Nov 2026 04:09:58 +0000 Subject: [PATCH 1/2] fork-ci: release commits that hold only fork-ci, protected tags Consumers pinning a fork-ci action at a main commit (ci/v1) fail at "Set up job": GitHub downloads the whole tree at that commit, and upstream's tree has a dangling symlink (.repos/alchemy-effect/.vscode/tsrs-tsdk/tsgo). - .github/scripts/fork-ci-release.py builds a release commit with git plumbing: only LICENSE, fork-ci/ and the workflow_call-only fork-pr-queue.yml, at the same paths, parent = the previous release, on branch ci-release, tagged ci/vN (annotated), pushed atomically and never forced. Refuses symlinks, submodules, self-starting workflows and half-made releases (with the repair command); same main commit or unchanged files = no release; main moved = no release. - .github/workflows/fork-ci-release.yml runs it daily / by hand on a green main tip (FORK_PUSH_TOKEN from Environment fork-push, same guard as fork-follow), then a consumer smoke job uses job-changed@ci-release remotely and checks the downloaded files equal the release commit. - .github/fork-ci-release-rulesets.json: rulesets for refs/tags/ci/* (no delete, no update) and ci-release (no delete, no force push), applied after merge. - fork-ci/README.md and the action READMEs: pin @ # ci/vN, never a main commit. Co-Authored-By: Claude Opus 5.5 --- .github/fork-ci-release-rulesets.json | 18 + .github/fork-ci-release.json | 9 + .github/fork-ci-tests.txt | 1 + .github/scripts/fork-ci-release.py | 415 ++++++++++++++++++++++++ .github/scripts/fork-ci-release.test.py | 406 +++++++++++++++++++++++ .github/workflows/fork-ci-release.yml | 277 ++++++++++++++++ fork-ci/README.md | 71 ++++ fork-ci/actions/docs-scope/README.md | 6 +- fork-ci/actions/gradle-cache/README.md | 4 +- fork-ci/actions/job-changed/README.md | 4 +- fork-ci/actions/pr-queue/README.md | 4 +- 11 files changed, 1207 insertions(+), 8 deletions(-) create mode 100644 .github/fork-ci-release-rulesets.json create mode 100644 .github/fork-ci-release.json create mode 100644 .github/scripts/fork-ci-release.py create mode 100644 .github/scripts/fork-ci-release.test.py create mode 100644 .github/workflows/fork-ci-release.yml create mode 100644 fork-ci/README.md diff --git a/.github/fork-ci-release-rulesets.json b/.github/fork-ci-release-rulesets.json new file mode 100644 index 0000000000000..7448f5aab2ca2 --- /dev/null +++ b/.github/fork-ci-release-rulesets.json @@ -0,0 +1,18 @@ +[ + { + "name": "fork-ci release tags: never moved or deleted", + "target": "tag", + "enforcement": "active", + "bypass_actors": [], + "conditions": { "ref_name": { "include": ["refs/tags/ci/*"], "exclude": [] } }, + "rules": [{ "type": "deletion" }, { "type": "update" }] + }, + { + "name": "fork-ci release branch: no force push or delete", + "target": "branch", + "enforcement": "active", + "bypass_actors": [], + "conditions": { "ref_name": { "include": ["refs/heads/ci-release"], "exclude": [] } }, + "rules": [{ "type": "deletion" }, { "type": "non_fast_forward" }] + } +] diff --git a/.github/fork-ci-release.json b/.github/fork-ci-release.json new file mode 100644 index 0000000000000..a838e4583974a --- /dev/null +++ b/.github/fork-ci-release.json @@ -0,0 +1,9 @@ +{ + "_comment": "The fork-ci release (.github/scripts/fork-ci-release.py, fork-ci/README.md). paths: the only files a release commit holds, at the same paths as on sourceBranch. legacyTags: tags made before release commits existed (they name a sourceBranch commit, not a release commit, and stay where they are).", + "sourceBranch": "main", + "requiredCheck": "fork-ci-result", + "branch": "ci-release", + "tagPrefix": "ci/v", + "legacyTags": ["ci/v1"], + "paths": ["LICENSE", "fork-ci", ".github/workflows/fork-pr-queue.yml"] +} diff --git a/.github/fork-ci-tests.txt b/.github/fork-ci-tests.txt index 1f985f61b6ede..162da12eaa2fb 100644 --- a/.github/fork-ci-tests.txt +++ b/.github/fork-ci-tests.txt @@ -11,6 +11,7 @@ .github/scripts/fork-follow.test.sh .github/scripts/fork-interaction-limit.test.sh .github/scripts/fork-pr-queue-workflow.test.py +.github/scripts/fork-ci-release.test.py .github/scripts/fork-release-workflow.test.py .github/scripts/fork-release.test.sh .github/scripts/fork-schema-check.test.cjs diff --git a/.github/scripts/fork-ci-release.py b/.github/scripts/fork-ci-release.py new file mode 100644 index 0000000000000..b001c0fa87ed3 --- /dev/null +++ b/.github/scripts/fork-ci-release.py @@ -0,0 +1,415 @@ +#!/usr/bin/env python3 +"""Fork only: release the fork's CI code (fork-ci/) as commits other repositories can pin. + +Why: a repository that runs `uses: OWNER/REPO/fork-ci/actions/@` makes GitHub download the +WHOLE tree at before the job starts. A commit of this fork's main branch carries upstream's +whole tree, and upstream's tree can hold things the runner cannot unpack (ci/v1, a main commit, +fails every consumer job at "Set up job" on a dangling symlink under .repos/). Main is also rebased +onto upstream, so only a tag keeps one of its commits alive. + +A release commit holds only the files in .github/fork-ci-release.json `paths` (fork-ci/, the +reusable workflows consumers call, LICENSE), at the same paths as on the source branch, so a +consumer's `uses:` path never changes. It is built with git plumbing from one source-branch commit, +its parent is the previous release commit, and it is pushed as the branch `branch` (ci-release) and +the annotated tag (ci/vN) in ONE atomic push. Consumers pin `@ # ci/vN`. + + python3 -I .github/scripts/fork-ci-release.py plan --remote URL --source SHA [--require-check] [--repo OWNER/NAME] + python3 -I .github/scripts/fork-ci-release.py release --remote URL --source SHA [--require-check] [--repo OWNER/NAME] + python3 -I .github/scripts/fork-ci-release.py verify-download --dir DIR --commit SHA [--git-dir DIR] + +plan says what a release would do and writes nothing. release does it. Both refuse (exit 1, the +reason on stderr and in $GITHUB_STEP_SUMMARY) on: + - a half state: a ci/vN tag (not in legacyTags) that is not on the branch (tag pushed, branch + not), a branch head that no ci/vN tag names (branch pushed, tag not), tags but no branch; + - a release tree that would hold a symlink, a submodule, anything but a plain file, a missing + path, or a workflow that starts on anything but workflow_call; + - a source whose required check (fork-ci-result) failed (--require-check); + - a push the remote refused (nothing is half-written: the push is atomic). +They do nothing and exit 0 when the source branch has moved past --source (the run for the newer +commit releases it), when the required check has not finished yet, and when the release tree is the +same as the branch head's (a re-run, or a source that did not change fork-ci). An existing tag is +never moved or deleted: the push forces nothing, so the branch moves only fast-forward and a tag +that exists is refused. + +verify-download checks that DIR (the folder the runner unpacked a remote `uses:` into) holds exactly +the files of COMMIT: the consumer smoke test of fork-ci-release.yml. + +No dependencies but git and node (node runs fork-workflow-triggers.cjs). The work happens in a +temporary repository: the caller's checkout is never touched. Credentials for the push come from +git's own configuration (fork-ci-release.yml passes FORK_PUSH_TOKEN as GIT_CONFIG_* variables). +Output (action, tag, sha, source, reason) goes to $GITHUB_OUTPUT when it is set. +Tests: .github/scripts/fork-ci-release.test.py. +""" +import argparse +import json +import os +import pathlib +import re +import subprocess +import sys +import tempfile +import urllib.request + +HERE = pathlib.Path(__file__).resolve().parent +CONFIG = HERE.parent / "fork-ci-release.json" +TRIGGERS = HERE / "fork-workflow-triggers.cjs" +SHA = re.compile(r"^[0-9a-f]{40}$") +PLAIN_FILE = {"100644", "100755"} + + +class Refuse(Exception): + """A state this tool will not act on; the message says why and what to do.""" + + +class Skip(Exception): + """Nothing to do now (exit 0); the message says why.""" + + +def load_config(path=CONFIG): + cfg = json.loads(pathlib.Path(path).read_text()) + for key in ("sourceBranch", "branch", "tagPrefix", "paths"): + if not cfg.get(key): + raise Refuse(f"{path}: `{key}` is missing") + cfg.setdefault("legacyTags", []) + cfg.setdefault("requiredCheck", "") + for p in cfg["paths"]: + if p.startswith("/") or p.endswith("/") or ".." in p.split("/") or not p: + raise Refuse(f"{path}: path {p!r} is not a plain path from the repository root") + return cfg + + +def git(repo, *args, input=None, env=None, check=True): + r = subprocess.run(["git", "-C", str(repo), *args], input=input, capture_output=True, + env=env, encoding="utf-8", errors="surrogateescape") + if check and r.returncode: + raise Refuse(f"git {' '.join(args[:3])} failed: {r.stderr.strip()[-800:]}") + return r + + +# --- remote state ------------------------------------------------------------------------------ +def remote_refs(repo, remote, cfg): + """{ref: sha} for the source branch, the release branch and the release tags; a tag's peeled + commit is under ref + '^{}' (annotated tags).""" + r = git(repo, "ls-remote", remote, f"refs/heads/{cfg['sourceBranch']}", f"refs/heads/{cfg['branch']}", + f"refs/tags/{cfg['tagPrefix']}*") + refs = {} + for line in r.stdout.splitlines(): + sha, ref = line.split("\t", 1) + refs[ref] = sha + return refs + + +def release_tags(refs, cfg): + """({name: (number, commit)}, [names that do not read as ]).""" + prefix = "refs/tags/" + cfg["tagPrefix"] + tags, odd = {}, [] + for ref, sha in refs.items(): + if not ref.startswith(prefix) or ref.endswith("^{}"): + continue + name = ref[len("refs/tags/"):] + m = re.fullmatch(r"[1-9][0-9]*", ref[len(prefix):]) + if not m: + odd.append(name) + continue + tags[name] = (int(m.group(0)), refs.get(ref + "^{}", sha)) + return tags, odd + + +def check_state(refs, cfg, on_branch): + """The previous release commit (None for the first release) and the next tag, or Refuse for a + half state. on_branch(commit) says whether commit is the branch head or one of its ancestors.""" + tags, _ = release_tags(refs, cfg) + branch = refs.get(f"refs/heads/{cfg['branch']}") + numbers = [n for n, _ in tags.values()] + next_tag = f"{cfg['tagPrefix']}{max(numbers, default=0) + 1}" + ours = {name: (n, c) for name, (n, c) in tags.items() if name not in cfg["legacyTags"]} + if branch is None: + if ours: + name, (_, commit) = max(ours.items(), key=lambda kv: kv[1][0]) + raise Refuse( + f"half state: tag {name} exists but the branch {cfg['branch']} does not (a release whose branch push " + f"failed, or a deleted branch). If {name} ({commit}) is a release commit, restore the branch: " + f"git push {commit}:refs/heads/{cfg['branch']}") + return None, next_tag + for name, (_, commit) in sorted(ours.items(), key=lambda kv: kv[1][0]): + if not on_branch(commit): + raise Refuse( + f"half state: tag {name} ({commit}) is not on the branch {cfg['branch']} (at {branch}): a release " + f"whose branch push failed? If {name}'s parent is {branch}, move the branch to it: " + f"git push {commit}:refs/heads/{cfg['branch']}") + newest = max(ours.items(), key=lambda kv: kv[1][0], default=None) + if newest is None or newest[1][1] != branch: + named = f"the newest tag {newest[0]} names {newest[1][1]}" if newest else "no release tag exists" + raise Refuse( + f"half state: the branch {cfg['branch']} is at {branch}, but {named} (a release whose tag push failed?). " + f"Tag the branch head as {next_tag} with git tag -a {next_tag} {branch} -m '{next_tag}' and push the tag, " + f"after checking that {branch} is a release commit") + return branch, next_tag + + +# --- the release tree -------------------------------------------------------------------------- +def release_entries(repo, source, cfg): + """[(mode, oid, path)] of the release tree; Refuse when it would hold anything but plain files.""" + r = git(repo, "--literal-pathspecs", "ls-tree", "-r", "-z", "--full-tree", source, "--", *cfg["paths"]) + entries = [] + for item in filter(None, r.stdout.split("\0")): + meta, path = item.split("\t", 1) + mode, kind, oid = meta.split() + if mode == "120000": + raise Refuse(f"{path} is a symlink: a release holds no symlinks (the runner cannot unpack a dangling one)") + if kind != "blob" or mode not in PLAIN_FILE: + raise Refuse(f"{path} is a {kind} (mode {mode}): a release holds only plain files") + entries.append((mode, oid, path)) + for p in cfg["paths"]: + if not any(path == p or path.startswith(p + "/") for _, _, path in entries): + raise Refuse(f"{p} is not in {source}: every path in the config must be there") + return entries + + +def ensure_objects(repo, remote, tree): + """Fetch, in one fetch, the files of TREE that a partial (blob:none) fetch left out.""" + def missing(): + r = git(repo, "rev-list", "--objects", "--missing=print", "--no-object-names", tree) + return [line[1:] for line in r.stdout.splitlines() if line.startswith("?")] + gone = missing() + if gone: + git(repo, "-c", "fetch.negotiationAlgorithm=noop", "fetch", "-q", "--no-tags", "--no-write-fetch-head", + "--filter=blob:none", "--stdin", remote, input="\n".join(gone) + "\n") + gone = missing() + if gone: + raise Refuse(f"{len(gone)} file(s) of the release could not be fetched, e.g. {gone[0]}") + + +def workflow_triggers(text): + with tempfile.NamedTemporaryFile("w", suffix=".yml", delete=False) as f: + f.write(text) + try: + r = subprocess.run(["node", str(TRIGGERS), f.name], capture_output=True, text=True) + finally: + os.unlink(f.name) + if r.returncode: + return None, r.stderr.strip() + return set(r.stdout.split()), "" + + +def check_workflows(repo, entries): + """Every workflow in the release starts only on workflow_call: pushing the release branch or + tag must start nothing in this repository.""" + for _, oid, path in entries: + if not re.match(r"\.github/workflows/[^/]+$", path): + continue + events, why = workflow_triggers(git(repo, "cat-file", "blob", oid).stdout) + if events is None: + raise Refuse(f"{path}: cannot tell what starts it ({why}); a release workflow must be workflow_call only") + if events != {"workflow_call"}: + raise Refuse(f"{path} starts on {', '.join(sorted(events))}: a release workflow must be workflow_call only " + "(pushing the release branch would start it)") + + +def write_tree(repo, entries): + with tempfile.TemporaryDirectory() as d: + env = {**os.environ, "GIT_INDEX_FILE": os.path.join(d, "index")} + info = "".join(f"{mode} {oid}\t{path}\0" for mode, oid, path in entries) + git(repo, "update-index", "-z", "--add", "--index-info", input=info, env=env) + return git(repo, "write-tree", "--missing-ok", env=env).stdout.strip() + + +# --- the required check ------------------------------------------------------------------------ +def check_verdict(runs, name): + """'success', 'pending' or 'failure' from the GitHub Actions check runs named NAME of a commit + (the newest run decides; none at all is pending).""" + mine = [r for r in runs if r.get("name") == name and (r.get("app") or {}).get("slug") == "github-actions"] + if not mine: + return "pending", f"no {name} check on it yet" + newest = max(mine, key=lambda r: (r.get("started_at") or "", r.get("id") or 0)) + if newest.get("status") != "completed": + return "pending", f"{name} is {newest.get('status')}" + if newest.get("conclusion") == "success": + return "success", f"{name} passed" + return "failure", f"{name} ended {newest.get('conclusion')}" + + +def fetch_check_runs(repo_name, sha, name): + api = os.environ.get("GITHUB_API_URL", "https://api.github.com") + req = urllib.request.Request(f"{api}/repos/{repo_name}/commits/{sha}/check-runs?check_name={name}&per_page=100", + headers={"Accept": "application/vnd.github+json"}) + token = os.environ.get("GH_TOKEN") or os.environ.get("GITHUB_TOKEN") + if token: + req.add_header("Authorization", f"Bearer {token}") + with urllib.request.urlopen(req, timeout=30) as resp: + return json.load(resp).get("check_runs", []) + + +def repo_name_of(remote): + m = re.search(r"github\.com[/:]([^/]+/[^/]+?)(?:\.git)?/?$", remote) + return m.group(1) if m else None + + +# --- the release ------------------------------------------------------------------------------- +def release(remote, source, cfg, write, required=None, before_push=None, log=print): + """Plan (write=False) or make the release. Returns a dict (action, tag, sha, source, reason). + required(source) -> (verdict, why) checks the source's CI; before_push() is a test hook.""" + if not SHA.match(source or ""): + raise Refuse(f"--source must be a full commit SHA, not {source!r}") + with tempfile.TemporaryDirectory(prefix="fork-ci-release-") as work: + repo = pathlib.Path(work) + git(repo, "init", "-q", "--bare") + git(repo, "remote", "add", "origin", remote) + refs = remote_refs(repo, "origin", cfg) + tip = refs.get(f"refs/heads/{cfg['sourceBranch']}") + if tip != source: + raise Skip(f"{cfg['sourceBranch']} is at {tip}, not {source}: the run for the newer commit releases it") + if required: + verdict, why = required(source) + if verdict == "pending": + raise Skip(f"{source}: {why}; nothing released until it passes") + if verdict != "success": + raise Refuse(f"{source}: {why}; a release needs it green") + branch = refs.get(f"refs/heads/{cfg['branch']}") + if branch: # its whole history: a release commit holds few files + git(repo, "fetch", "-q", "--no-tags", "--filter=blob:none", "origin", + f"+refs/heads/{cfg['branch']}:refs/release/branch") + git(repo, "fetch", "-q", "--no-tags", "--depth=1", "--filter=blob:none", "origin", source) + + def on_branch(commit): + return git(repo, "merge-base", "--is-ancestor", commit, branch, check=False).returncode == 0 + + parent, tag = check_state(refs, cfg, on_branch) + _, odd = release_tags(refs, cfg) + for name in odd: + log(f"::warning::tag {name} does not read as {cfg['tagPrefix']}; ignored") + entries = release_entries(repo, source, cfg) + tree = write_tree(repo, entries) + ensure_objects(repo, "origin", tree) + check_workflows(repo, entries) + if parent and git(repo, "rev-parse", f"{parent}^{{tree}}").stdout.strip() == tree: + newest = max((n, name) for name, (n, c) in release_tags(refs, cfg)[0].items() if c == parent)[1] + raise Skip(f"{newest} ({parent}) already holds these files: nothing to release") + if not write: + return {"action": "release", "tag": tag, "sha": "", "source": source, "parent": parent or "", + "reason": f"{tag} would hold {len(entries)} files from {source}"} + ident = git(repo, "var", "GIT_COMMITTER_IDENT", check=False) + if ident.returncode: + raise Refuse("git has no identity (user.name, user.email) to write the release commit with") + body = (f"{tag}: fork-ci from {cfg['sourceBranch']} {source}\n\n" + f"Only {', '.join(cfg['paths'])}, at the same paths as on {cfg['sourceBranch']}.\n" + f"Pin it as @ # {tag}.\n\n" + f"Fork-CI-Source: {source}\n") + commit = git(repo, "commit-tree", tree, *(["-p", parent] if parent else []), input=body).stdout.strip() + tag_obj = git(repo, "mktag", input=(f"object {commit}\ntype commit\ntag {tag}\ntagger " + f"{ident.stdout.strip()}\n\n{tag}: fork-ci from {source}\n")).stdout.strip() + if before_push: + before_push() + # Right before the push: the source branch and the release refs are as planned. + now = remote_refs(repo, "origin", cfg) + if now.get(f"refs/heads/{cfg['sourceBranch']}") != source: + raise Skip(f"{cfg['sourceBranch']} moved during the run (now {now.get('refs/heads/' + cfg['sourceBranch'])}): " + "nothing pushed; the run for the newer commit releases it") + if now.get(f"refs/heads/{cfg['branch']}") != branch or f"refs/tags/{tag}" in now: + raise Refuse(f"the branch {cfg['branch']} or the tag {tag} changed during the run (another release?): " + "nothing pushed; run again") + # No force at all: the branch only fast-forwards (a branch someone else moved or created + # meanwhile is not an ancestor, so the push is refused) and an existing tag is refused. + r = git(repo, "push", "--atomic", "--porcelain", "origin", + f"{commit}:refs/heads/{cfg['branch']}", f"{tag_obj}:refs/tags/{tag}", check=False) + if r.returncode: + raise Refuse(f"the push was refused, nothing was written (atomic): {(r.stdout + r.stderr).strip()[-800:]}") + after = remote_refs(repo, "origin", cfg) + if after.get(f"refs/heads/{cfg['branch']}") != commit or after.get(f"refs/tags/{tag}^{{}}") != commit: + raise Refuse(f"pushed, but reading back shows the branch at {after.get('refs/heads/' + cfg['branch'])} and " + f"{tag} at {after.get('refs/tags/' + tag + '^{}')}, not {commit}") + return {"action": "released", "tag": tag, "sha": commit, "source": source, "parent": parent or "", + "reason": f"{tag} is {commit}: pin @{commit} # {tag}"} + + +# --- the consumer smoke test ------------------------------------------------------------------- +def verify_download(directory, commit, git_dir="."): + """Problems (a list) when DIRECTORY does not hold exactly the plain files of COMMIT.""" + r = git(git_dir, "ls-tree", "-r", "-z", "--full-tree", commit) + want = {} + for item in filter(None, r.stdout.split("\0")): + meta, path = item.split("\t", 1) + mode, kind, oid = meta.split() + want[path] = (mode, oid) + root = pathlib.Path(directory) + if not root.is_dir(): + return [f"{directory} does not exist: the runner did not unpack the action there"] + problems, have = [], {} + for p in sorted(root.rglob("*")): + rel = p.relative_to(root).as_posix() + if p.is_symlink(): + problems.append(f"{rel} is a symlink") + elif p.is_file(): + oid = git(git_dir, "hash-object", "--no-filters", "--", str(p)).stdout.strip() + have[rel] = oid + for path, (mode, oid) in want.items(): + if mode not in PLAIN_FILE: + problems.append(f"{path} is mode {mode} in {commit}") + elif path not in have: + problems.append(f"{path} is missing") + elif have[path] != oid: + problems.append(f"{path} differs from {commit}") + problems += [f"{path} is not in {commit}" for path in sorted(set(have) - set(want))] + return problems + + +# --- command line ------------------------------------------------------------------------------ +def emit(result): + out = os.environ.get("GITHUB_OUTPUT") + lines = [f"{k}={str(v).replace(chr(10), ' ')}" for k, v in result.items()] + if out: + with open(out, "a") as f: + f.write("\n".join(lines) + "\n") + summary = os.environ.get("GITHUB_STEP_SUMMARY") + if summary: + with open(summary, "a") as f: + f.write(f"### fork-ci release: {result['action']}\n\n{result['reason']}\n") + + +def main(argv=None): + p = argparse.ArgumentParser(description=__doc__.split("\n")[0]) + sub = p.add_subparsers(dest="cmd", required=True) + for name in ("plan", "release"): + s = sub.add_parser(name) + s.add_argument("--remote", required=True, help="the repository's git URL") + s.add_argument("--source", required=True, help="the source-branch commit to release (full SHA)") + s.add_argument("--require-check", action="store_true", help="needs the config's requiredCheck green on it") + s.add_argument("--repo", help="OWNER/NAME for the check API (default: from --remote)") + s.add_argument("--config", default=str(CONFIG)) + v = sub.add_parser("verify-download") + v.add_argument("--dir", required=True) + v.add_argument("--commit", required=True) + v.add_argument("--git-dir", default=".") + a = p.parse_args(argv) + if a.cmd == "verify-download": + problems = verify_download(a.dir, a.commit, a.git_dir) + for line in problems[:50]: + print(f"::error::{line}", file=sys.stderr) + if not problems: + print(f"{a.dir} holds exactly the files of {a.commit}") + return 1 if problems else 0 + try: + cfg = load_config(a.config) + required = None + if a.require_check: + if not cfg["requiredCheck"]: + raise Refuse("--require-check, but the config names no requiredCheck") + name = a.repo or repo_name_of(a.remote) + if not name: + raise Refuse(f"cannot tell OWNER/NAME from {a.remote}; pass --repo") + required = lambda sha: check_verdict(fetch_check_runs(name, sha, cfg["requiredCheck"]), # noqa: E731 + cfg["requiredCheck"]) + result = release(a.remote, a.source, cfg, write=a.cmd == "release", required=required) + except Skip as e: + result = {"action": "none", "tag": "", "sha": "", "source": a.source, "parent": "", "reason": str(e)} + except Refuse as e: + print(f"::error::fork-ci release: {e}", file=sys.stderr) + emit({"action": "refused", "tag": "", "sha": "", "source": a.source, "parent": "", "reason": str(e)}) + return 1 + print(f"{result['action']}: {result['reason']}") + emit(result) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/.github/scripts/fork-ci-release.test.py b/.github/scripts/fork-ci-release.test.py new file mode 100644 index 0000000000000..e4a4a4b292917 --- /dev/null +++ b/.github/scripts/fork-ci-release.test.py @@ -0,0 +1,406 @@ +"""Tests .github/scripts/fork-ci-release.py (the fork-ci release) against throwaway local repositories +(file:// remotes, so the partial fetch the real run uses is exercised), the real tree, the real +config, the ruleset file and .github/workflows/fork-ci-release.yml. + + python3 .github/scripts/fork-ci-release.test.py + +Needs git, node and PyYAML. Reaches no network and no GitHub. Each check prints ok/FAIL; any FAIL +exits 1. Each row of the failure table (first release, re-run, half states, main moving, a push +refused half-way) is one block below. +""" +import contextlib +import importlib.util +import io +import json +import os +import pathlib +import subprocess +import sys +import tempfile + +import yaml + +sys.dont_write_bytecode = True # importing fork-ci-release.py must not leave __pycache__ in the tree +root = pathlib.Path(__file__).resolve().parents[2] +spec = importlib.util.spec_from_file_location("fork_ci_release", root / ".github/scripts/fork-ci-release.py") +rel = importlib.util.module_from_spec(spec) +spec.loader.exec_module(rel) +failures = [] + + +def check(name, ok, detail=""): + print(("ok " if ok else "FAIL ") + name + ("" if ok or not detail else f"\n | {detail}")) + if not ok: + failures.append(name) + + +work = pathlib.Path(tempfile.mkdtemp(prefix="fork-ci-release-test-")) +gitconfig = work / "gitconfig" +gitconfig.write_text("[user]\n\tname = Test\n\temail = test@example.invalid\n[init]\n\tdefaultBranch = main\n" + "[protocol \"file\"]\n\tallow = always\n") +for k in ("GH_TOKEN", "GITHUB_TOKEN", "GITHUB_OUTPUT", "GITHUB_STEP_SUMMARY", "GIT_DIR", "GIT_INDEX_FILE"): + os.environ.pop(k, None) +os.environ.update(GIT_CONFIG_GLOBAL=str(gitconfig), GIT_CONFIG_NOSYSTEM="1", GH_CONFIG_DIR=str(work / "gh")) + +CFG = {"sourceBranch": "main", "requiredCheck": "fork-ci-result", "branch": "ci-release", "tagPrefix": "ci/v", + "legacyTags": ["ci/v1"], "paths": ["LICENSE", "fork-ci", ".github/workflows/fork-pr-queue.yml"]} +CALL_ONLY = "on:\n workflow_call:\n inputs:\n x:\n type: string\njobs: {}\n" + + +def sh(cwd, *args, input=None, check_=True): + r = subprocess.run(["git", "-C", str(cwd), *args], capture_output=True, text=True, input=input) + if check_ and r.returncode: + raise RuntimeError(f"git {args}: {r.stderr}") + return r.stdout.strip() + + +class World: + """A bare 'GitHub' remote and a clone that pushes main to it.""" + + def __init__(self, name): + self.dir = work / name + self.remote = self.dir / "remote.git" + self.src = self.dir / "src" + self.url = f"file://{self.remote}" + sh(work, "init", "-q", "--bare", str(self.remote)) + for k, v in (("uploadpack.allowFilter", "true"), ("uploadpack.allowAnySHA1InWant", "true")): + sh(self.remote, "config", k, v) + sh(work, "init", "-q", str(self.src)) + self.write({"LICENSE": "MIT\n", "README.md": "upstream\n", + "fork-ci/actions/a/action.yml": "name: a\nruns: {using: composite, steps: []}\n", + "fork-ci/actions/a/a.py": "print('a')\n", + ".github/workflows/fork-pr-queue.yml": CALL_ONLY, + ".github/workflows/fork-tests.yml": "on:\n push:\njobs: {}\n"}) + os.chmod(self.src / "fork-ci/actions/a/a.py", 0o755) + (self.src / ".repos/x/.vscode").mkdir(parents=True) + os.symlink("does/not/exist", self.src / ".repos/x/.vscode/tsgo") # upstream's dangling symlink + self.first = self.commit("upstream + fork") + sh(self.src, "tag", "-a", "ci/v1", "-m", "ci/v1", self.first) + sh(self.src, "push", "-q", self.url, "main", "ci/v1") + + def write(self, files): + for path, text in files.items(): + p = self.src / path + if text is None: + p.unlink() + continue + p.parent.mkdir(parents=True, exist_ok=True) + p.write_text(text) + + def commit(self, msg, push=False): + sh(self.src, "add", "-A") + sh(self.src, "commit", "-q", "--allow-empty", "-m", msg) + sha = sh(self.src, "rev-parse", "HEAD") + if push: + sh(self.src, "push", "-q", "-f", self.url, "main") + return sha + + def refs(self): + out = sh(self.remote, "for-each-ref", "--format=%(refname) %(objectname)") + return dict(line.split() for line in out.splitlines()) + + def run(self, source=None, write=True, cfg=CFG, **kw): + """('released'|'none'|'refused', result or message).""" + try: + return "ok", rel.release(self.url, source or sh(self.remote, "rev-parse", "main"), cfg, write, + log=lambda *_: None, **kw) + except rel.Skip as e: + return "none", str(e) + except rel.Refuse as e: + return "refused", str(e) + + +def tree_of(w, commit): + out = sh(w.remote, "ls-tree", "-r", "--full-tree", commit) + return {line.split("\t")[1]: line.split()[0] for line in out.splitlines()} + + +# --- first release ever (no branch; only the legacy tag ci/v1) ----------------------------------- +w = World("main") +before = w.refs() +kind, plan = w.run(write=False) +check("first release: plan says release ci/v2 and writes nothing", + kind == "ok" and plan["action"] == "release" and plan["tag"] == "ci/v2" and w.refs() == before, str(plan)) +kind, r = w.run() +refs = w.refs() +v2 = r["sha"] if kind == "ok" else "" +check("first release: released ci/v2", kind == "ok" and r["action"] == "released" and r["tag"] == "ci/v2", str(r)) +check("first release: branch ci-release and tag ci/v2 name the same new commit", + refs.get("refs/heads/ci-release") == v2 and sh(w.remote, "rev-parse", "ci/v2^{commit}") == v2) +check("first release: ci/v2 is an annotated tag", + sh(w.remote, "cat-file", "-t", refs.get("refs/tags/ci/v2", "x"), check_=False) == "tag") +check("first release: ci/v1 (legacy) did not move", refs.get("refs/tags/ci/v1") == before["refs/tags/ci/v1"]) +files = tree_of(w, v2) +check("first release: only the configured paths, at the same paths as on main", + set(files) == {"LICENSE", "fork-ci/actions/a/action.yml", "fork-ci/actions/a/a.py", + ".github/workflows/fork-pr-queue.yml"}, str(sorted(files))) +check("first release: no symlink, only plain files", set(files.values()) <= {"100644", "100755"}, str(files)) +check("first release: an executable keeps its mode", files.get("fork-ci/actions/a/a.py") == "100755") +check("first release: the release commit has no parent and names its source", + sh(w.remote, "rev-list", "--parents", "-n1", v2) == v2 and + f"Fork-CI-Source: {w.first}" in sh(w.remote, "log", "-1", "--format=%B", v2)) +check("first release: the files are the source's files", + all(sh(w.remote, "rev-parse", f"{v2}:{p}") == sh(w.remote, "rev-parse", f"{w.first}:{p}") for p in files)) + +# --- re-run: same main commit, or a main commit that does not change the release files ------------- +kind, msg = w.run() +check("rerun: same main commit releases nothing and moves nothing", kind == "none" and w.refs() == refs, msg) +w.write({"README.md": "upstream moved on\n"}) +w.commit("unrelated upstream change", push=True) +refs = w.refs() +kind, msg = w.run() +check("rerun: a main commit that leaves the release files alone releases nothing", + kind == "none" and "already holds" in msg and w.refs() == refs, msg) + +# --- a second release: parent is the previous release, ci/v2 never moves -------------------------- +w.write({"fork-ci/actions/a/a.py": "print('a2')\n", "fork-ci/actions/b/action.yml": "name: b\n"}) +w.commit("fork-ci change", push=True) +v2_tag = w.refs()["refs/tags/ci/v2"] +kind, r = w.run() +v3 = r["sha"] if kind == "ok" else "" +check("next release: ci/v3 whose parent is ci/v2's commit", + kind == "ok" and r["tag"] == "ci/v3" and sh(w.remote, "rev-parse", f"{v3}^") == v2, str(r)) +check("next release: ci/v2 still names the same tag object and commit", + w.refs()["refs/tags/ci/v2"] == v2_tag and sh(w.remote, "rev-parse", "ci/v2^{commit}") == v2) + +# --- what a release tree may hold -------------------------------------------------------------- +def refused_tree(label, files, expect, mode=None, link=None): + t = World(label) + t.write(files) + if link: + os.symlink(link[1], t.src / link[0]) + sh(t.src, "add", "-A") + if mode: # after add -A, which would drop an index entry with nothing on disk + sh(t.src, "update-index", "--add", "--cacheinfo", mode) + sh(t.src, "commit", "-q", "-m", label) + sh(t.src, "push", "-q", t.url, "main") + refs = t.refs() + kind, msg = t.run() + check(f"tree: {label} is refused, nothing written", + kind == "refused" and expect in msg and t.refs() == refs, f"{kind}: {msg}") + + +refused_tree("a symlink in fork-ci", {}, "symlink", link=("fork-ci/actions/a/link", "../../../README.md")) +refused_tree("a dangling symlink in fork-ci", {}, "symlink", link=("fork-ci/actions/a/tsgo", "nowhere")) +refused_tree("a submodule in fork-ci", {}, "commit", mode=f"160000,{'1' * 40},fork-ci/actions/sub") +refused_tree("a release workflow that starts on push", + {".github/workflows/fork-pr-queue.yml": "on:\n push:\n workflow_call:\njobs: {}\n"}, "workflow_call only") +refused_tree("a release workflow whose triggers cannot be read", + {".github/workflows/fork-pr-queue.yml": "on: &x\n workflow_call:\njobs: {}\n"}, "cannot tell") +refused_tree("a configured path missing on main", {"LICENSE": None}, "LICENSE is not in") + +# --- half states: tag pushed but branch push failed, and the other way round --------------------- +h = World("half") +h.run() +v2 = sh(h.remote, "rev-parse", "ci-release") +h.write({"fork-ci/actions/a/a.py": "print('half')\n"}) +h.commit("change", push=True) +orphan = sh(h.remote, "commit-tree", f"{v2}^{{tree}}", "-p", v2, input="ci/v3 whose branch push failed\n") +sh(h.remote, "update-ref", "refs/tags/ci/v3", orphan) +refs = h.refs() +kind, msg = h.run() +check("half state: tag ci/v3 pushed, branch not: refused, nothing written, the message gives the repair", + kind == "refused" and "ci/v3" in msg and f"git push {orphan}:refs/heads/ci-release" in msg + and h.refs() == refs, msg) +sh(h.remote, "update-ref", "refs/heads/ci-release", orphan, v2) # the repair the message gives +kind, r = h.run() +check("half state: after the repair the next release is ci/v4 on top of ci/v3", + kind == "ok" and r["tag"] == "ci/v4" and sh(h.remote, "rev-parse", f"{r['sha']}^") == orphan, str(r)) + +b = World("branch-only") +b.run() +v2 = sh(b.remote, "rev-parse", "ci-release") +untagged = sh(b.remote, "commit-tree", f"{v2}^{{tree}}", "-p", v2, input="tag push failed\n") +sh(b.remote, "update-ref", "refs/heads/ci-release", untagged, v2) +b.write({"fork-ci/actions/a/a.py": "print('b')\n"}) +b.commit("change", push=True) +refs = b.refs() +kind, msg = b.run() +check("half state: branch pushed, tag not: refused, nothing written, says to tag the head", + kind == "refused" and untagged in msg and "git tag -a ci/v3" in msg and b.refs() == refs, msg) + +n = World("no-branch") +n.run() +sh(n.remote, "update-ref", "-d", "refs/heads/ci-release") +n.write({"fork-ci/actions/a/a.py": "print('n')\n"}) +n.commit("change", push=True) +refs = n.refs() +kind, msg = n.run() +check("half state: release tags but no branch: refused, nothing written", + kind == "refused" and "branch ci-release does not" in msg and n.refs() == refs, msg) + +# --- main moved: before the run, and during it --------------------------------------------------- +m = World("moved") +old = sh(m.remote, "rev-parse", "main") +m.write({"fork-ci/actions/a/a.py": "print('m')\n"}) +m.commit("newer", push=True) +kind, msg = m.run(source=old) +check("main moved before the run: releases nothing (the newer commit's run does)", + kind == "none" and "not " + old in msg and "refs/heads/ci-release" not in m.refs(), msg) + + +def move_main(): + m.write({"fork-ci/actions/a/a.py": "print('m2')\n"}) + m.commit("moved during the run", push=True) + + +kind, msg = m.run(before_push=move_main) +check("main moved during the run: nothing pushed, exit 0 with the reason", + kind == "none" and "moved during the run" in msg and "refs/heads/ci-release" not in m.refs(), msg) +kind, r = m.run() +check("main moved: the next run releases the new tip", kind == "ok" and r["source"] == sh(m.remote, "rev-parse", "main")) + +c = World("race") +mine = sh(c.remote, "rev-parse", "main") +kind, msg = c.run(before_push=lambda: sh(c.remote, "update-ref", "refs/tags/ci/v2", mine)) +check("a tag made by someone else during the run is never moved: refused, nothing pushed", + kind == "refused" and sh(c.remote, "rev-parse", "ci/v2") == mine and "refs/heads/ci-release" not in c.refs(), msg) + +# --- the push stops half-way: the remote refuses one of the two refs ------------------------------- +p = World("atomic") +hook = p.remote / "hooks/update" +hook.write_text('#!/bin/sh\ncase "$1" in refs/tags/*) echo "no tags today" >&2; exit 1;; esac\nexit 0\n') +hook.chmod(0o755) +kind, msg = p.run() +check("push refused for the tag only: refused, and the branch was not written either (atomic)", + kind == "refused" and "refs/heads/ci-release" not in p.refs() and "refs/tags/ci/v2" not in p.refs(), msg) +hook.unlink() +kind, r = p.run() +check("push refused: a re-run once the remote accepts releases ci/v2", kind == "ok" and r["tag"] == "ci/v2", str(r)) + +# --- the required check (fork-ci-result) --------------------------------------------------------- +q = World("checks") +for verdict, want in (("pending", "none"), ("failure", "refused"), ("success", "ok")): + kind, msg = q.run(required=lambda sha, v=verdict: (v, f"fork-ci-result {v}")) + check(f"required check {verdict}: {want}", kind == want, str(msg)) +run = lambda i, status, conclusion, slug="github-actions", t="2026-11-14T00:00:0": { # noqa: E731 + "id": i, "name": "fork-ci-result", "status": status, "conclusion": conclusion, "started_at": f"{t}{i}Z", + "app": {"slug": slug}} +for label, runs, want in ( + ("none at all is pending", [], "pending"), + ("the newest run decides (an old failure, a new pass)", + [run(1, "completed", "failure"), run(2, "completed", "success")], "success"), + ("the newest run decides (an old pass, a new failure)", + [run(1, "completed", "success"), run(2, "completed", "failure")], "failure"), + ("a newest run in progress is pending", [run(1, "completed", "success"), run(2, "in_progress", None)], "pending"), + ("a cancelled run is not a pass", [run(1, "completed", "cancelled")], "failure"), + ("a check of the same name from another app does not count", [run(1, "completed", "success", "evil")], "pending")): + got, _ = rel.check_verdict(runs, "fork-ci-result") + check(f"check verdict: {label}", got == want, got) + +# --- verify-download (the consumer smoke test's check) ------------------------------------------- +v = World("verify") +v.run() +commit = sh(v.remote, "rev-parse", "ci-release") + + +def unpacked(name): + d = work / "unpacked" / name + d.mkdir(parents=True) + archive = subprocess.run(["git", "-C", str(v.remote), "archive", commit], capture_output=True, check=True).stdout + subprocess.run(["tar", "-x", "-C", str(d)], input=archive, check=True) + return d + + +check("verify-download: an unpacked release commit passes", rel.verify_download(unpacked("good"), commit, v.remote) == []) +d = unpacked("extra") +(d / "fork-ci/extra.txt").write_text("x") +check("verify-download: an extra file fails", any("extra.txt" in p for p in rel.verify_download(d, commit, v.remote))) +d = unpacked("changed") +(d / "LICENSE").write_text("other\n") +check("verify-download: a changed file fails", any("LICENSE differs" in p for p in rel.verify_download(d, commit, v.remote))) +d = unpacked("missing") +(d / "LICENSE").unlink() +check("verify-download: a missing file fails", any("LICENSE is missing" in p for p in rel.verify_download(d, commit, v.remote))) +d = unpacked("link") +os.symlink("nowhere", d / "fork-ci/tsgo") +check("verify-download: a symlink fails", any("symlink" in p for p in rel.verify_download(d, commit, v.remote))) +check("verify-download: no folder fails", rel.verify_download(work / "none", commit, v.remote) != []) +with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()): + code = rel.main(["verify-download", "--dir", str(work / "none"), "--commit", commit, "--git-dir", str(v.remote)]) +check("verify-download: the command exits 1 on a problem", code == 1) + +# --- the command line: exit codes and outputs ------------------------------------------------------ +cl = World("cli") +cfg_file = cl.dir / "cfg.json" +cfg_file.write_text(json.dumps(CFG)) +out = cl.dir / "out" +os.environ["GITHUB_OUTPUT"] = str(out) +src = sh(cl.remote, "rev-parse", "main") +with contextlib.redirect_stdout(io.StringIO()): + c1 = rel.main(["release", "--remote", cl.url, "--source", src, "--config", str(cfg_file)]) + c2 = rel.main(["release", "--remote", cl.url, "--source", src, "--config", str(cfg_file)]) +with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()): + c3 = rel.main(["release", "--remote", cl.url, "--source", "main", "--config", str(cfg_file)]) +lines = out.read_text().splitlines() +os.environ.pop("GITHUB_OUTPUT") +check("cli: release exits 0 and writes action=released, tag and sha", + c1 == 0 and "action=released" in lines and "tag=ci/v2" in lines and f"sha={sh(cl.remote, 'rev-parse', 'ci/v2^{commit}')}" in lines) +check("cli: the re-run exits 0 with action=none", c2 == 0 and lines.count("action=none") == 1) +check("cli: a source that is not a full SHA is refused (exit 1)", c3 == 1 and "action=refused" in lines) + +# --- the real config, the real tree, the ruleset file, the workflow ------------------------------ +cfg = rel.load_config() +check("config: the real config reads", cfg["branch"] and cfg["tagPrefix"] and cfg["paths"]) +head = sh(root, "rev-parse", "HEAD") +try: + entries = rel.release_entries(root, head, cfg) + rel.check_workflows(root, entries) + real_ok, why = True, "" +except rel.Refuse as e: + real_ok, why = False, str(e) +check("real tree: this commit's release would hold plain files only and workflow_call-only workflows", + real_ok and entries, why) +check("real tree: fork-ci-release.yml itself is not released (it starts on schedule)", + real_ok and not any(p.endswith("fork-ci-release.yml") for _, _, p in entries)) +for bad in ("/abs", "a/../b", "dir/", ""): + t = work / f"cfg-{abs(hash(bad))}.json" + t.write_text(json.dumps({**CFG, "paths": [bad]})) + try: + rel.load_config(t) + ok = False + except rel.Refuse: + ok = True + check(f"config: path {bad!r} is refused", ok) + +rulesets = json.loads((root / ".github/fork-ci-release-rulesets.json").read_text()) +by_target = {r["target"]: r for r in rulesets} +tag_rs, branch_rs = by_target.get("tag", {}), by_target.get("branch", {}) +tag_patterns = tag_rs.get("conditions", {}).get("ref_name", {}).get("include", []) +check("ruleset: tags matching the release tags (and ci/v1) cannot be deleted or updated; creating them is allowed", + any(cfg["tagPrefix"].startswith(p[len("refs/tags/"):-1]) for p in tag_patterns if p.endswith("*")) + and {r["type"] for r in tag_rs.get("rules", [])} == {"deletion", "update"}, json.dumps(tag_rs)) +check("ruleset: the release branch cannot be deleted or force-pushed; fast-forward pushes are allowed", + branch_rs.get("conditions", {}).get("ref_name", {}).get("include") == [f"refs/heads/{cfg['branch']}"] + and {r["type"] for r in branch_rs.get("rules", [])} == {"deletion", "non_fast_forward"}, json.dumps(branch_rs)) +check("ruleset: both active, nobody bypasses", + all(r.get("enforcement") == "active" and r.get("bypass_actors") == [] for r in rulesets) and len(rulesets) == 2) + +wf = yaml.safe_load((root / ".github/workflows/fork-ci-release.yml").read_text()) +on = wf.get(True, wf.get("on")) +jobs = wf["jobs"] +check("workflow: starts only on schedule and workflow_dispatch", set(on) == {"schedule", "workflow_dispatch"}, str(on)) +check("workflow: one release at a time, never cancelled half-way", + wf.get("concurrency", {}).get("cancel-in-progress") is False) +check("workflow: no write permission anywhere (the push uses FORK_PUSH_TOKEN)", + wf.get("permissions") == {} and all("write" not in json.dumps(j.get("permissions", {})) for j in jobs.values())) +uses_token = [jid for jid, j in jobs.items() if "secrets.FORK_PUSH_TOKEN" in json.dumps(j)] +check("workflow: only guard (presence) and release (value) see FORK_PUSH_TOKEN", + set(uses_token) == {"guard", "release"} and jobs["release"].get("environment") == "fork-push" + and "secrets.FORK_PUSH_TOKEN != ''" in json.dumps(jobs["guard"]), str(uses_token)) +check("workflow: release runs only after this attempt's guard passed", + jobs["release"].get("if") == "needs.guard.outputs.attempt == github.run_attempt") +rel_run = next((s.get("run", "") for s in jobs["release"]["steps"] if s.get("id") == "release"), "") +check("workflow: release is the script's release with the required check, from this run's commit", + "fork-ci-release.py release --require-check" in rel_run and '--source "$GITHUB_SHA"' in rel_run) +check("workflow: the plan job writes nothing (plan, no token)", + "fork-ci-release.py plan --require-check" in json.dumps(jobs["plan"]) and "FORK_PUSH" not in json.dumps(jobs["plan"])) +smoke_uses = [s.get("uses", "") for s in jobs["smoke"]["steps"] if s.get("uses")] +check("workflow: the smoke test runs a released action from a remote ref of the release branch (not ./)", + smoke_uses == [f"scuttletoby/t3code/fork-ci/actions/job-changed@{cfg['branch']}"], str(smoke_uses)) +check("workflow: the smoke test checks the unpacked files and the released SHA", + "verify-download" in json.dumps(jobs["smoke"]) and "needs.release.outputs.sha" in json.dumps(jobs["smoke"]) + and f"_actions/scuttletoby/t3code/{cfg['branch']}" in json.dumps(jobs["smoke"])) + +print(f"\n{len(failures)} failed" if failures else "\nall passed") +sys.exit(1 if failures else 0) diff --git a/.github/workflows/fork-ci-release.yml b/.github/workflows/fork-ci-release.yml new file mode 100644 index 0000000000000..5e2ee5d4d6354 --- /dev/null +++ b/.github/workflows/fork-ci-release.yml @@ -0,0 +1,277 @@ +name: Fork CI release + +# Fork only. Releases fork-ci/ as commits other repositories can pin: only fork-ci/, the reusable +# workflows consumers call and LICENSE, never upstream's tree (.github/scripts/fork-ci-release.py says +# why and how; fork-ci/README.md says how to pin; .github/fork-ci-release.json lists the files). +# Runs daily and by hand; it releases only main's tip, only once fork-ci-result passed on it, and only +# when the release files changed: +# - plan: no token, writes nothing. Says release or none (and why), or refuses a half state. +# - guard, release: only when the plan says release. FORK_PUSH_TOKEN from the Environment fork-push +# (GITHUB_TOKEN may not push commits that hold a workflow file), guarded exactly as fork-follow.yml +# guards it (fork-follow-guard.sh). One atomic push: the branch ci-release and the tag ci/vN. +# - smoke: downloads the action the way a consumer does, a remote `uses:` (not ./) of the branch +# ci-release, whose head is the newest release commit, runs it, and checks that the runner unpacked +# exactly that commit's files. ci/v1 (a main commit) failed exactly here for every consumer. +# Dispatch with smoke-only to test the current release without releasing. +# The smoke `uses:` names this repository literally: a `uses:` ref cannot be an expression. +# Tests: .github/scripts/fork-ci-release.test.py. + +on: + schedule: + - cron: "41 7 * * *" + workflow_dispatch: + inputs: + smoke-only: + description: Only run the consumer smoke test of the current release; release nothing + type: boolean + default: false + +permissions: {} + +# One release at a time; a cancel would only be a stop half-way. +concurrency: + group: fork-ci-release + cancel-in-progress: false + +env: + # Must match `environment:` of the release job (an expression there cannot read env). + FORK_PUSH_ENVIRONMENT: fork-push + # The only branch the Environment may deploy to (fork-follow-guard.sh checks it). + SERIES_BRANCH: main + +jobs: + plan: + name: Plan + if: ${{ !inputs.smoke-only }} + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + contents: read + checks: read + outputs: + action: ${{ steps.plan.outputs.action }} + steps: + - name: Checkout + shell: bash + env: + REF: ${{ github.sha }} + HISTORY: tip + SPARSE: /.github/ + run: | + set -euo pipefail + [[ -n "$REF" ]] || { echo "::error::No commit or branch to check out." >&2; exit 1; } + case "$HISTORY" in + full) history=(--filter=blob:none) ;; + tip) history=(--depth=1 --filter=blob:none) ;; + *) echo "::error::HISTORY is full or tip, not '$HISTORY'." >&2; exit 1 ;; + esac + read -r -a sparse <<< "$SPARSE" + git init -q + git remote add origin "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY" + # A name is a branch, fetched as refs/heads/: a bare name would take a tag of the same + # name first. + if [[ "$REF" =~ ^[0-9a-f]{40}$ ]]; then src="$REF"; else src="refs/heads/$REF"; fi + git fetch -q --no-tags --no-recurse-submodules "${history[@]}" origin "$src" + if (( ${#sparse[@]} )); then git sparse-checkout set --no-cone "${sparse[@]}"; fi + git checkout -q --detach FETCH_HEAD + + - id: plan + name: Plan the release (writes nothing) + env: + GH_TOKEN: ${{ github.token }} + run: >- + python3 -I .github/scripts/fork-ci-release.py plan --require-check + --remote "https://github.com/$GITHUB_REPOSITORY.git" --repo "$GITHUB_REPOSITORY" --source "$GITHUB_SHA" + + # No Environment here, so the FORK_PUSH_TOKEN secret seen here can only be a repository or + # organisation secret. Only whether it is empty reaches the step, never the value. + guard: + name: Guard the token + needs: [plan] + if: needs.plan.outputs.action == 'release' + runs-on: ubuntu-latest + timeout-minutes: 5 + permissions: + actions: read + contents: read + outputs: + # Set only once the guard passed: a re-run of failed jobs keeps an earlier attempt's result. + attempt: ${{ steps.passed.outputs.attempt }} + steps: + - name: Checkout + shell: bash + env: + REF: ${{ github.sha }} + HISTORY: tip + SPARSE: /.github/scripts/ + run: | + set -euo pipefail + [[ -n "$REF" ]] || { echo "::error::No commit or branch to check out." >&2; exit 1; } + case "$HISTORY" in + full) history=(--filter=blob:none) ;; + tip) history=(--depth=1 --filter=blob:none) ;; + *) echo "::error::HISTORY is full or tip, not '$HISTORY'." >&2; exit 1 ;; + esac + read -r -a sparse <<< "$SPARSE" + git init -q + git remote add origin "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY" + # A name is a branch, fetched as refs/heads/: a bare name would take a tag of the same + # name first. + if [[ "$REF" =~ ^[0-9a-f]{40}$ ]]; then src="$REF"; else src="refs/heads/$REF"; fi + git fetch -q --no-tags --no-recurse-submodules "${history[@]}" origin "$src" + if (( ${#sparse[@]} )); then git sparse-checkout set --no-cone "${sparse[@]}"; fi + git checkout -q --detach FETCH_HEAD + + - name: Refuse unless only the Environment holds FORK_PUSH_TOKEN, for main only + env: + GH_TOKEN: ${{ github.token }} + REPO_SECRET_PRESENT: ${{ secrets.FORK_PUSH_TOKEN != '' }} + REPORT: ${{ runner.temp }}/reports/guard.md + run: | + mkdir -p "$RUNNER_TEMP/reports" + bash .github/scripts/fork-follow-guard.sh + + - id: passed + name: Note the attempt the guard passed in + run: echo "attempt=$GITHUB_RUN_ATTEMPT" >> "$GITHUB_OUTPUT" + + release: + name: Release + needs: [plan, guard] + if: needs.guard.outputs.attempt == github.run_attempt + environment: fork-push + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + actions: read + checks: read + contents: read + outputs: + tag: ${{ steps.release.outputs.tag }} + sha: ${{ steps.release.outputs.sha }} + steps: + - name: Checkout + shell: bash + env: + REF: ${{ github.sha }} + HISTORY: tip + SPARSE: /.github/ + run: | + set -euo pipefail + [[ -n "$REF" ]] || { echo "::error::No commit or branch to check out." >&2; exit 1; } + case "$HISTORY" in + full) history=(--filter=blob:none) ;; + tip) history=(--depth=1 --filter=blob:none) ;; + *) echo "::error::HISTORY is full or tip, not '$HISTORY'." >&2; exit 1 ;; + esac + read -r -a sparse <<< "$SPARSE" + git init -q + git remote add origin "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY" + # A name is a branch, fetched as refs/heads/: a bare name would take a tag of the same + # name first. + if [[ "$REF" =~ ^[0-9a-f]{40}$ ]]; then src="$REF"; else src="refs/heads/$REF"; fi + git fetch -q --no-tags --no-recurse-submodules "${history[@]}" origin "$src" + if (( ${#sparse[@]} )); then git sparse-checkout set --no-cone "${sparse[@]}"; fi + git checkout -q --detach FETCH_HEAD + + - name: Check FORK_PUSH_TOKEN + env: + FORK_PUSH_TOKEN: ${{ secrets.FORK_PUSH_TOKEN }} + REPORT: ${{ runner.temp }}/reports/token.md + run: | + mkdir -p "$RUNNER_TEMP/reports" + bash .github/scripts/fork-follow-push.sh check + + - name: Re-check the Environment right before the push + env: + GH_TOKEN: ${{ github.token }} + REPORT: ${{ runner.temp }}/reports/guard-again.md + run: | + mkdir -p "$RUNNER_TEMP/reports" + bash .github/scripts/fork-follow-guard.sh --environment-only + + # The token reaches git as an HTTP header in GIT_CONFIG_* variables (as fork-follow-push.sh + # passes it): never in a URL, a command line or a config file. + - id: release + name: Release fork-ci (branch ci-release, tag ci/vN, one atomic push) + env: + FORK_PUSH_TOKEN: ${{ secrets.FORK_PUSH_TOKEN }} + GH_TOKEN: ${{ github.token }} + GIT_AUTHOR_NAME: github-actions[bot] + GIT_AUTHOR_EMAIL: 41898583+github-actions[bot]@users.noreply.github.com + GIT_COMMITTER_NAME: github-actions[bot] + GIT_COMMITTER_EMAIL: 41898583+github-actions[bot]@users.noreply.github.com + run: | + header="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$FORK_PUSH_TOKEN" | base64 -w0)" + echo "::add-mask::$header" + unset FORK_PUSH_TOKEN + export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=http.https://github.com/.extraheader GIT_CONFIG_VALUE_0="$header" + export GIT_TERMINAL_PROMPT=0 + python3 -I .github/scripts/fork-ci-release.py release --require-check \ + --remote "https://github.com/$GITHUB_REPOSITORY.git" --repo "$GITHUB_REPOSITORY" --source "$GITHUB_SHA" + + smoke: + name: Consumer smoke test + needs: [plan, guard, release] + if: >- + !cancelled() && + contains(fromJSON('["success", "skipped"]'), needs.plan.result) && + contains(fromJSON('["success", "skipped"]'), needs.guard.result) && + contains(fromJSON('["success", "skipped"]'), needs.release.result) + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + contents: read + steps: + - name: Checkout + shell: bash + env: + REF: ${{ github.sha }} + HISTORY: tip + SPARSE: /.github/ + run: | + set -euo pipefail + [[ -n "$REF" ]] || { echo "::error::No commit or branch to check out." >&2; exit 1; } + case "$HISTORY" in + full) history=(--filter=blob:none) ;; + tip) history=(--depth=1 --filter=blob:none) ;; + *) echo "::error::HISTORY is full or tip, not '$HISTORY'." >&2; exit 1 ;; + esac + read -r -a sparse <<< "$SPARSE" + git init -q + git remote add origin "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY" + # A name is a branch, fetched as refs/heads/: a bare name would take a tag of the same + # name first. + if [[ "$REF" =~ ^[0-9a-f]{40}$ ]]; then src="$REF"; else src="refs/heads/$REF"; fi + git fetch -q --no-tags --no-recurse-submodules "${history[@]}" origin "$src" + if (( ${#sparse[@]} )); then git sparse-checkout set --no-cone "${sparse[@]}"; fi + git checkout -q --detach FETCH_HEAD + + # GitHub downloads the whole tree of the ref at "Set up job", as for any consumer: a tree the + # runner cannot unpack fails the job before this step. + - id: action + name: Run a released action from a remote ref, as a consumer does + uses: scuttletoby/t3code/fork-ci/actions/job-changed@ci-release + with: + base: HEAD + head: HEAD + workflow: .github/workflows/fork-ci-release.yml + job: smoke + + - name: Check the runner unpacked exactly the release commit + env: + CHANGED: ${{ steps.action.outputs.changed }} + REASON: ${{ steps.action.outputs.reason }} + RELEASED: ${{ needs.release.outputs.sha }} + run: | + [[ "$CHANGED" == true || "$CHANGED" == false ]] \ + || { echo "::error::the released action ran but gave no answer (changed='$CHANGED')"; exit 1; } + echo "The action ran: changed=$CHANGED ($REASON)" + git fetch -q --no-tags --depth=1 origin refs/heads/ci-release + head="$(git rev-parse FETCH_HEAD)" + if [[ -n "$RELEASED" && "$RELEASED" != "$head" ]]; then + echo "::error::ci-release is at $head, not at this run's release $RELEASED (another release since?)" + exit 1 + fi + dir="$(dirname "$RUNNER_WORKSPACE")/_actions/scuttletoby/t3code/ci-release" + python3 -I .github/scripts/fork-ci-release.py verify-download --dir "$dir" --commit "$head" diff --git a/fork-ci/README.md b/fork-ci/README.md new file mode 100644 index 0000000000000..8fdbb4f2c8844 --- /dev/null +++ b/fork-ci/README.md @@ -0,0 +1,71 @@ +# fork-ci: CI actions for other repositories + +Composite actions under [`actions/`](actions/) and the reusable workflow +[`.github/workflows/fork-pr-queue.yml`](../.github/workflows/fork-pr-queue.yml), for any +repository to use. Each action's README says what it does and what it needs. + +## Pinning + +Pin a **release commit** by its full SHA, with its tag as a comment: + +```yaml +- uses: scuttletoby/t3code/fork-ci/actions/job-changed@ # ci/vN +``` + +Find it on the [`ci-release`](https://github.com/scuttletoby/t3code/tree/ci-release) branch, or with +`git ls-remote https://github.com/scuttletoby/t3code.git 'refs/tags/ci/v*'` (the `^{}` line is the +commit to pin). + +**Never pin a commit of `main`.** For a `uses:` line GitHub downloads the whole repository at the +pinned commit. Main also holds upstream T3 Code, and its tree has a dangling symlink that makes every +job fail at "Set up job" (`Could not find file '.../.repos/...'`). Main is also rebased onto +upstream, so its old commits disappear. `ci/v1` is such a main commit: it is kept for history, but +do not use it. + +A release commit holds only what consumers use: `LICENSE`, `fork-ci/` and the reusable workflows +that start only on `workflow_call` (the list is `paths` in +[`.github/fork-ci-release.json`](../.github/fork-ci-release.json)). The paths are the same as on main. +Release commits contain no symlinks and no workflow that starts by itself. + +## How a release is made + +[`.github/workflows/fork-ci-release.yml`](../.github/workflows/fork-ci-release.yml) runs once a day +and can also be started by hand. It runs +[`.github/scripts/fork-ci-release.py`](../.github/scripts/fork-ci-release.py) on the tip of main. +It makes a release only when that commit passed `fork-ci-result` and changed the release files. +The new commit's parent is the previous release. The script pushes branch `ci-release` and tag +`ci/vN` in one atomic push, and never forces either. A second run on the same main commit does +nothing. + +The script refuses, and writes nothing, in these cases: + +- a half-made release (a tag that is not on the branch, or a branch head that has no tag); +- a symlink, submodule or other non-file in the release paths; +- a released workflow that starts on anything other than `workflow_call`. + +Each refusal says what is wrong, and for a half-made release it gives the command that repairs it. Next, a smoke job uses a released action +the way a consumer does (a remote `uses:` of `ci-release`). It checks that the downloaded files are +exactly the release commit's files. + +The push needs `FORK_PUSH_TOKEN` (fine-grained, this repository only, Contents and Workflows +read and write). It must be a secret of the Environment `fork-push`, whose deployment branches are +`main` only, and not a repository secret. `fork-follow.yml` uses the same rule. Until then the guard +job refuses. Someone with push rights can release from a clean checkout of main with +`python3 .github/scripts/fork-ci-release.py release --require-check --remote https://github.com/scuttletoby/t3code.git --repo scuttletoby/t3code --source
` +(`plan` instead of `release` shows what would happen and writes nothing). + +## Protection + +Two repository rulesets keep pinned releases alive. They are recorded in +[`.github/fork-ci-release-rulesets.json`](../.github/fork-ci-release-rulesets.json): + +- `refs/tags/ci/*`: tags cannot be deleted or moved; new ones can be created; +- `refs/heads/ci-release`: the branch cannot be deleted or force-pushed. + +To create them again (repository admin): + +```sh +jq -c '.[]' .github/fork-ci-release-rulesets.json | while read -r ruleset; do + printf '%s' "$ruleset" | gh api -X POST repos/scuttletoby/t3code/rulesets --input - +done +``` diff --git a/fork-ci/actions/docs-scope/README.md b/fork-ci/actions/docs-scope/README.md index b42889b6ae15c..03d36afc54476 100644 --- a/fork-ci/actions/docs-scope/README.md +++ b/fork-ci/actions/docs-scope/README.md @@ -11,7 +11,7 @@ reads, which folders each job builds) is a JSON config in the calling repository ## Usage -Pin the action by full SHA (the tagged one, so a rebase of this fork cannot orphan it): +Pin a release commit by its full SHA, never a commit of main ([why](../../README.md#pinning)): ```yaml jobs: @@ -28,7 +28,7 @@ jobs: --jq '.workflow_runs[0].head_sha // ""') || sha="" echo "sha=$sha" >> "$GITHUB_OUTPUT" - id: scope - uses: scuttletoby/t3code/fork-ci/actions/docs-scope@ # ci/vN + uses: scuttletoby/t3code/fork-ci/actions/docs-scope@ # ci/vN with: config: .github/docs-scope.json # default base: ${{ steps.green.outputs.sha }} # empty: a full run @@ -150,7 +150,7 @@ so the "config is never docs" rule cannot be dodged by spelling. What it cannot stop: a `pull_request` run uses the workflow file from the pull request, so a pull request that edits the `uses:` line chooses the code that judges it. That edit is a change to a non-docs file, so every correct version -runs in full for it; pin only SHAs that this fork's CI tested and tagged. +runs in full for it; pin only release commits ([`fork-ci/README.md`](../../README.md#pinning)). ## Fails closed diff --git a/fork-ci/actions/gradle-cache/README.md b/fork-ci/actions/gradle-cache/README.md index 19f58d48c5c1f..9f46c6db250e0 100644 --- a/fork-ci/actions/gradle-cache/README.md +++ b/fork-ci/actions/gradle-cache/README.md @@ -13,11 +13,11 @@ on every run: the same bytes on a fresh runner and on a warm one, and never an o ## Use in a workflow -Pin the action by full SHA (the commit a `ci/vN` tag names): +Pin a release commit by its full SHA, never a commit of main ([why](../../README.md#pinning)): ```yaml - id: m2 - uses: scuttletoby/t3code/fork-ci/actions/gradle-cache@ # ci/vN + uses: scuttletoby/t3code/fork-ci/actions/gradle-cache@ # ci/vN with: lock: engine/m2-lock.json # project fact cache-dir: ~/.cache/my-ci-m2 # optional: keep an existing warm cache diff --git a/fork-ci/actions/job-changed/README.md b/fork-ci/actions/job-changed/README.md index e1f6a94f41ee9..24987df51f7b8 100644 --- a/fork-ci/actions/job-changed/README.md +++ b/fork-ci/actions/job-changed/README.md @@ -10,7 +10,7 @@ builds, which docs a build reads) stays in the calling repository: pass folders ## Usage -Pin the action by full SHA (the tagged one, so a rebase of this fork cannot orphan it): +Pin a release commit by its full SHA, never a commit of main ([why](../../README.md#pinning)): ```yaml jobs: @@ -19,7 +19,7 @@ jobs: - uses: actions/checkout@ # the default ref: on pull_request GitHub's merge commit with: { fetch-depth: 0 } # the base must be in the checkout - id: changed - uses: scuttletoby/t3code/fork-ci/actions/job-changed@ # ci/vN + uses: scuttletoby/t3code/fork-ci/actions/job-changed@ # ci/vN with: base: ${{ github.event_name == 'pull_request' && 'HEAD^1' || steps.green.outputs.sha }} workflow: .github/workflows/ci.yml diff --git a/fork-ci/actions/pr-queue/README.md b/fork-ci/actions/pr-queue/README.md index 35b13f9f2eac6..8d7e28c2210fe 100644 --- a/fork-ci/actions/pr-queue/README.md +++ b/fork-ci/actions/pr-queue/README.md @@ -283,7 +283,9 @@ It runs only on `push`, `schedule` and `workflow_dispatch`, so the code that merges pull requests never runs for a pull request. It checks out only the config file from the caller, and the queue from the workflow's own repository and commit (`job.workflow_repository`, `job.workflow_sha`, runner 2.334.0 or -newer): the queue that runs is the one the caller pinned. One run per +newer): the queue that runs is the one the caller pinned. `<40-hex sha>` here and +below is a release commit (`@ # ci/vN`), never a commit of main: see +[`fork-ci/README.md`](../../README.md#pinning). One run per repository at a time, never cancelled half-way. Inputs: `runs-on`, `config`, `state-file`, `cache`, `lock-file` and `dry-run`, as in "The queue". From 47c06dfdbe3fef92a5f222c7cb314a6ef4a688d6 Mon Sep 17 00:00:00 2001 From: scuttletoby <123843277+scuttletoby@users.noreply.github.com> Date: Sat, 14 Nov 2026 04:21:47 +0000 Subject: [PATCH 2/2] fork-ci release: count only push runs' fork-ci-result; pin-check in README The release gate took the newest fork-ci-result check run on the main tip. A workflow_dispatch of fork-tests.yml on main with ref= puts such a check on the tip's SHA while testing something else, so it could turn a red tip green. Now only check runs whose check suite belongs to a push or merge_group workflow run of that SHA count (actions/runs?head_sha=), any failed counted run refuses, and a commit with no counted run is pending. The plan job gets actions: read for the runs API. fork-ci/README.md: pin only a ci/* tag whose commit is on ci-release, with a one-line check (anyone with push rights can create a ci/* tag and the rulesets keep even a stray one). Co-Authored-By: Claude Opus 5.5 --- .github/scripts/fork-ci-release.py | 67 ++++++++++++++++++------- .github/scripts/fork-ci-release.test.py | 58 +++++++++++++++++---- .github/workflows/fork-ci-release.yml | 1 + fork-ci/README.md | 8 +++ 4 files changed, 107 insertions(+), 27 deletions(-) diff --git a/.github/scripts/fork-ci-release.py b/.github/scripts/fork-ci-release.py index b001c0fa87ed3..670b93c3c0121 100644 --- a/.github/scripts/fork-ci-release.py +++ b/.github/scripts/fork-ci-release.py @@ -23,7 +23,8 @@ not), a branch head that no ci/vN tag names (branch pushed, tag not), tags but no branch; - a release tree that would hold a symlink, a submodule, anything but a plain file, a missing path, or a workflow that starts on anything but workflow_call; - - a source whose required check (fork-ci-result) failed (--require-check); + - a source whose required check (fork-ci-result) failed in any push run of it (--require-check; + only runs a push or merge queue started count, never a workflow_dispatch); - a push the remote refused (nothing is half-written: the push is atomic). They do nothing and exit 0 when the source branch has moved past --source (the run for the newer commit releases it), when the required check has not finished yet, and when the release tree is the @@ -215,29 +216,58 @@ def write_tree(repo, entries): # --- the required check ------------------------------------------------------------------------ -def check_verdict(runs, name): - """'success', 'pending' or 'failure' from the GitHub Actions check runs named NAME of a commit - (the newest run decides; none at all is pending).""" - mine = [r for r in runs if r.get("name") == name and (r.get("app") or {}).get("slug") == "github-actions"] +# Only check runs of workflow runs that a push to the source branch (or a merge queue) started count. +# A workflow_dispatch of fork-tests.yml with `ref=` runs on the source tip's SHA (GitHub +# records the dispatching branch's head) but tests another commit, so its fork-ci-result says nothing +# about the tip; pull_request runs test a merge commit. Those are matched by check suite and ignored. +TRUSTED_EVENTS = ("push", "merge_group") + + +def check_verdict(runs, name, suites): + """'success', 'pending' or 'failure' from the check runs named NAME of a commit. Only GitHub + Actions check runs whose check suite is in SUITES (the suites of the commit's push and merge_group + workflow runs, see trusted_suites) count. Any counted failure (or cancel) is a failure, even when a + newer counted run passed: a re-run that went green does not hide a red push run (the next commit + on the source branch releases instead). Otherwise any counted run still going is pending, and no + counted run at all is pending too: a commit tested only by hand (workflow_dispatch) is not green, + and its push run may not have started yet, so the next scheduled run looks again.""" + mine = [r for r in runs if r.get("name") == name and (r.get("app") or {}).get("slug") == "github-actions" + and (r.get("check_suite") or {}).get("id") in suites] if not mine: - return "pending", f"no {name} check on it yet" - newest = max(mine, key=lambda r: (r.get("started_at") or "", r.get("id") or 0)) - if newest.get("status") != "completed": - return "pending", f"{name} is {newest.get('status')}" - if newest.get("conclusion") == "success": - return "success", f"{name} passed" - return "failure", f"{name} ended {newest.get('conclusion')}" + others = sum(1 for r in runs if r.get("name") == name) + return "pending", (f"no {name} check from a push run on it yet" + + (f" ({others} from other runs, e.g. workflow_dispatch, do not count)" if others else "")) + bad = [r for r in mine if r.get("status") == "completed" and r.get("conclusion") != "success"] + if bad: + return "failure", f"{name} ended {bad[0].get('conclusion')} in a push run (check run {bad[0].get('id')})" + going = [r for r in mine if r.get("status") != "completed"] + if going: + return "pending", f"{name} is {going[0].get('status')}" + return "success", f"{name} passed" -def fetch_check_runs(repo_name, sha, name): +def trusted_suites(workflow_runs): + """The check suite ids of the workflow runs that TRUSTED_EVENTS started.""" + return {r.get("check_suite_id") for r in workflow_runs + if r.get("event") in TRUSTED_EVENTS and r.get("check_suite_id") is not None} + + +def gh_get(path): api = os.environ.get("GITHUB_API_URL", "https://api.github.com") - req = urllib.request.Request(f"{api}/repos/{repo_name}/commits/{sha}/check-runs?check_name={name}&per_page=100", - headers={"Accept": "application/vnd.github+json"}) + req = urllib.request.Request(f"{api}{path}", headers={"Accept": "application/vnd.github+json"}) token = os.environ.get("GH_TOKEN") or os.environ.get("GITHUB_TOKEN") if token: req.add_header("Authorization", f"Bearer {token}") with urllib.request.urlopen(req, timeout=30) as resp: - return json.load(resp).get("check_runs", []) + return json.load(resp) + + +def fetch_check_runs(repo_name, sha, name): + return gh_get(f"/repos/{repo_name}/commits/{sha}/check-runs?check_name={name}&per_page=100").get("check_runs", []) + + +def fetch_workflow_runs(repo_name, sha): + return gh_get(f"/repos/{repo_name}/actions/runs?head_sha={sha}&per_page=100").get("workflow_runs", []) def repo_name_of(remote): @@ -397,8 +427,9 @@ def main(argv=None): name = a.repo or repo_name_of(a.remote) if not name: raise Refuse(f"cannot tell OWNER/NAME from {a.remote}; pass --repo") - required = lambda sha: check_verdict(fetch_check_runs(name, sha, cfg["requiredCheck"]), # noqa: E731 - cfg["requiredCheck"]) + required = lambda sha: check_verdict( # noqa: E731 + fetch_check_runs(name, sha, cfg["requiredCheck"]), cfg["requiredCheck"], + trusted_suites(fetch_workflow_runs(name, sha))) result = release(a.remote, a.source, cfg, write=a.cmd == "release", required=required) except Skip as e: result = {"action": "none", "tag": "", "sha": "", "source": a.source, "parent": "", "reason": str(e)} diff --git a/.github/scripts/fork-ci-release.test.py b/.github/scripts/fork-ci-release.test.py index e4a4a4b292917..e52d72d71e448 100644 --- a/.github/scripts/fork-ci-release.test.py +++ b/.github/scripts/fork-ci-release.test.py @@ -273,20 +273,60 @@ def move_main(): for verdict, want in (("pending", "none"), ("failure", "refused"), ("success", "ok")): kind, msg = q.run(required=lambda sha, v=verdict: (v, f"fork-ci-result {v}")) check(f"required check {verdict}: {want}", kind == want, str(msg)) -run = lambda i, status, conclusion, slug="github-actions", t="2026-11-14T00:00:0": { # noqa: E731 +PUSH, DISPATCH = 501, 502 # check suite ids of a push run and a workflow_dispatch run of the same commit +wf_runs = [{"id": 1, "event": "push", "check_suite_id": PUSH}, + {"id": 2, "event": "workflow_dispatch", "check_suite_id": DISPATCH}, + {"id": 3, "event": "pull_request", "check_suite_id": 503}, + {"id": 4, "event": "merge_group", "check_suite_id": 504}] +check("trusted suites: only push and merge_group workflow runs", rel.trusted_suites(wf_runs) == {PUSH, 504}, + str(rel.trusted_suites(wf_runs))) +run = lambda i, status, conclusion, slug="github-actions", suite=PUSH, t="2026-11-14T00:00:0": { # noqa: E731 "id": i, "name": "fork-ci-result", "status": status, "conclusion": conclusion, "started_at": f"{t}{i}Z", - "app": {"slug": slug}} + "app": {"slug": slug}, "check_suite": {"id": suite}} for label, runs, want in ( ("none at all is pending", [], "pending"), - ("the newest run decides (an old failure, a new pass)", - [run(1, "completed", "failure"), run(2, "completed", "success")], "success"), - ("the newest run decides (an old pass, a new failure)", - [run(1, "completed", "success"), run(2, "completed", "failure")], "failure"), - ("a newest run in progress is pending", [run(1, "completed", "success"), run(2, "in_progress", None)], "pending"), + ("a push run that passed is green", [run(1, "completed", "success")], "success"), + ("a newer passing workflow_dispatch run does not hide an older failed push run", + [run(1, "completed", "failure"), run(2, "completed", "success", suite=DISPATCH)], "failure"), + ("only workflow_dispatch runs (even green) are not green: pending", + [run(1, "completed", "success", suite=DISPATCH)], "pending"), + ("a pull_request run does not count", [run(1, "completed", "success", suite=503)], "pending"), + ("any failed push run refuses, even under a newer pass (a re-run)", + [run(1, "completed", "failure"), run(2, "completed", "success")], "failure"), + ("an old pass, a new failure", [run(1, "completed", "success"), run(2, "completed", "failure")], "failure"), + ("a push run in progress is pending", [run(1, "completed", "success"), run(2, "in_progress", None)], "pending"), ("a cancelled run is not a pass", [run(1, "completed", "cancelled")], "failure"), ("a check of the same name from another app does not count", [run(1, "completed", "success", "evil")], "pending")): - got, _ = rel.check_verdict(runs, "fork-ci-result") - check(f"check verdict: {label}", got == want, got) + got, why = rel.check_verdict(runs, "fork-ci-result", rel.trusted_suites(wf_runs)) + check(f"check verdict: {label}", got == want, f"{got}: {why}") + +# The same through main(): the API answers come from the fake gh_get, so the wiring (check runs +# matched to the commit's push workflow runs) is what decides. +g = World("gate") +gate_sha = sh(g.remote, "rev-parse", "main") +for label, checks_, want in ( + ("main(): a green dispatch run over a red push run refuses", + [run(1, "completed", "failure"), run(2, "completed", "success", suite=DISPATCH)], 1), + ("main(): only a green dispatch run does nothing (pending)", [run(2, "completed", "success", suite=DISPATCH)], 0), + ("main(): a green push run plans the release", [run(1, "completed", "success")], 0)): + asked = [] + + def fake_gh_get(path, checks_=checks_): + asked.append(path) + if "/check-runs?" in path: + return {"check_runs": checks_} + if "/actions/runs?" in path: + return {"workflow_runs": wf_runs} + raise AssertionError(path) + real, rel.gh_get = rel.gh_get, fake_gh_get + out = io.StringIO() + try: + with contextlib.redirect_stdout(out), contextlib.redirect_stderr(io.StringIO()): + code = rel.main(["plan", "--require-check", "--remote", g.url, "--repo", "o/r", "--source", gate_sha]) + finally: + rel.gh_get = real + check(label, code == want and any(f"head_sha={gate_sha}" in x for x in asked) + and ("release:" in out.getvalue()) == (label.endswith("plans the release")), f"{code} {out.getvalue()} {asked}") # --- verify-download (the consumer smoke test's check) ------------------------------------------- v = World("verify") diff --git a/.github/workflows/fork-ci-release.yml b/.github/workflows/fork-ci-release.yml index 5e2ee5d4d6354..d2c65b563e7f0 100644 --- a/.github/workflows/fork-ci-release.yml +++ b/.github/workflows/fork-ci-release.yml @@ -46,6 +46,7 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 10 permissions: + actions: read contents: read checks: read outputs: diff --git a/fork-ci/README.md b/fork-ci/README.md index 8fdbb4f2c8844..1ade514665a1f 100644 --- a/fork-ci/README.md +++ b/fork-ci/README.md @@ -16,6 +16,14 @@ Find it on the [`ci-release`](https://github.com/scuttletoby/t3code/tree/ci-rele `git ls-remote https://github.com/scuttletoby/t3code.git 'refs/tags/ci/v*'` (the `^{}` line is the commit to pin). +Pin only a `ci/*` tag whose commit is on `ci-release`. Anyone with push rights can create a new +`ci/*` tag, and the rulesets below keep even a stray one from being deleted. Check before pinning +(replace `vN`; `ahead` or `identical` means the tag is on the branch, anything else means do not pin it): + +```sh +curl -s https://api.github.com/repos/scuttletoby/t3code/compare/ci/vN...ci-release | jq -r .status +``` + **Never pin a commit of `main`.** For a `uses:` line GitHub downloads the whole repository at the pinned commit. Main also holds upstream T3 Code, and its tree has a dangling symlink that makes every job fail at "Set up job" (`Could not find file '.../.repos/...'`). Main is also rebased onto