diff --git a/.github/workflows/ci-gate-caller.yml b/.github/workflows/ci-gate-caller.yml index fc691c14..2c5f1961 100644 --- a/.github/workflows/ci-gate-caller.yml +++ b/.github/workflows/ci-gate-caller.yml @@ -18,13 +18,19 @@ # (repository, context, head SHA; cancel-in-progress) so that every consumer gets it with the pin, and # its sweep group is separate. The pull_request job at the end is permanent # (docs/CI-GATE.adoc, "The pull_request start"); tests/ci-gate.sh pins its shape. +# `workflow_run.workflows` is an EXPLICIT list, with no globs: it is the `name:` of every workflow file in +# the profile (profiles/ci-gate/hyperpolymath/proven-cicd-and-workflows.json), so a push starts one evaluation +# per listed workflow, not one per workflow of the repository. Regenerate it whenever the profile changes: +# scripts/ci-gate-trigger-names.sh profiles/ci-gate/hyperpolymath/proven-cicd-and-workflows.json . +# tests/ci-gate.sh pins it to that output. A required workflow missing from the list never triggers an +# evaluation (the sweep or a pull_request event still evaluates the commit; docs/CI-GATE.adoc, "Liveness"). # No `secrets:` line: the reusable consumes none. name: CI gate on: pull_request: workflow_run: - workflows: ["**", "!CI gate"] + workflows: ["Bounded secret scanner controls", "Central Estate CI/CD Audit", "Proven lock gate", "Secret Scanner", "Supplemental secret checks"] types: [completed, requested, in_progress] schedule: - cron: "7,22,37,52 * * * *" diff --git a/docs/CI-GATE.adoc b/docs/CI-GATE.adoc index 9fc21527..b21406f8 100644 --- a/docs/CI-GATE.adoc +++ b/docs/CI-GATE.adoc @@ -15,7 +15,7 @@ A3 `estate-gate` replaces it by changing one pin and one context name in each ca == Design in one paragraph -The caller starts `gate-pr` on `pull_request` for same-repository branches (see "Bootstrap" and "The pull_request start"). Its default-branch `gate` job triggers on `workflow_run` (types `completed`, `requested` and `in_progress`, any workflow but its own; a start event is let through only for a re-run, see "Re-runs"), on `schedule` (minutes 7, 22, 37 and 52 of every hour) and on `workflow_dispatch`; see "Sweep" below for the last two. Every evaluation reads the head commit with *two* requests, whatever the number of workflows: the workflow runs of the head SHA (`GET /repos/{repo}/actions/runs?head_sha=`) and the latest check runs of the commit (`GET /repos/{repo}/commits/{sha}/check-runs?filter=latest`), each over its pages of 100. The check runs are joined to their workflow run on check suite id (`check_suite_id` of the run, `check_suite.id` of the check run), which rebuilds the same per-job records the verdict has always read; the profile format and the verdict rules are unchanged except for the pending rules below. The reusable's job has a `concurrency` group keyed on repository, context and head SHA with `cancel-in-progress`, so the newest event for a commit replaces an evaluation of it that is still queued or running; the sweep has its own group. GitHub runs `workflow_run` workflows from the default branch, so a pull request cannot edit the caller, its pin or its list. The reusable reads the profile for `github.repository` from this suite at `job.workflow_sha`, runs the verdict script and posts one commit status. It is a commit status, not a check run, because a job in a reusable workflow gets the check-run name ` / `, which can never equal `CI/gate`. +The caller starts `gate-pr` on `pull_request` for same-repository branches (see "Bootstrap" and "The pull_request start"). Its default-branch `gate` job triggers on `workflow_run` (types `completed`, `requested` and `in_progress`, and only for the workflows listed by name in `workflow_run.workflows`: an explicit list, with no globs, that must be the `name:` of each workflow in the profile, see "Liveness: the trigger list"; the gate's own workflow is never in it; a start event is let through only for a re-run, see "Re-runs"), on `schedule` (minutes 7, 22, 37 and 52 of every hour) and on `workflow_dispatch`; see "Sweep" below for the last two. Every evaluation reads the head commit with *two* requests, whatever the number of workflows: the workflow runs of the head SHA (`GET /repos/{repo}/actions/runs?head_sha=`) and the latest check runs of the commit (`GET /repos/{repo}/commits/{sha}/check-runs?filter=latest`), each over its pages of 100. The check runs are joined to their workflow run on check suite id (`check_suite_id` of the run, `check_suite.id` of the check run), which rebuilds the same per-job records the verdict has always read; the profile format and the verdict rules are unchanged except for the pending rules below. The reusable's job has a `concurrency` group keyed on repository, context and head SHA with `cancel-in-progress`, so the newest event for a commit replaces an evaluation of it that is still queued or running; the sweep has its own group. GitHub runs `workflow_run` workflows from the default branch, so a pull request cannot edit the caller, its pin or its list. The reusable reads the profile for `github.repository` from this suite at `job.workflow_sha`, runs the verdict script and posts one commit status. It is a commit status, not a check run, because a job in a reusable workflow gets the check-run name ` / `, which can never equal `CI/gate`. *Why it works this way (resource-algebra#50).* One push started 22 workflows. The previous design started an evaluation per started and per completed workflow, and each evaluation listed the jobs of every run (one request per run), again on every poll of a waiting pull request, plus a sweep every 15 minutes. That exhausted the installation token quota (`HTTP 403 API rate limit exceeded`), after which the gate published a failure for a commit whose every required check had passed, and a status stands until something overwrites it. Reading in bulk and coalescing remove most of that load. Neither can promise that the API stays available, so an unavailable API now ends in `pending`, never in a failure. @@ -98,6 +98,14 @@ On `schedule` and `workflow_dispatch` there is no triggering run, so the reusabl The reusable accepts `workflow_run`, `pull_request`, `schedule` and `workflow_dispatch`; only the last two sweep. The job needs `pull-requests: read` in addition to `statuses: write`, `actions: read`, `checks: read` and `contents: read`: *the caller must grant them*, or the pull request listing or the check-run read fails. A sweep writes statuses for pull request heads from forks too (the statuses API is keyed by commit in the base repository); it reads metadata only and never checks out or runs their code. +== Liveness: the trigger list + +`workflow_run.workflows` matches workflow *names* (the top-level `name:` of a workflow file), not file paths, while a profile lists workflow *files*. The caller therefore carries an explicit list, with no globs: the `name:` of each distinct workflow file in the profile. `scripts/ci-gate-trigger-names.sh PROFILE.json REPO-ROOT` prints that array (sorted, unique, one line) and exits 2, with a message, if a listed file is missing, has no `name:`, has a name containing `*`, `?`, `[`, `]`, `!` or `+`, or is named `CI gate`. Regenerate the list whenever the profile changes. `tests/ci-gate.sh` pins the suite's own caller to the script's output for the suite's profile. The consumers' list must be the profile's names: the template ships a placeholder that matches no workflow and must be replaced. + +A glob (`"**"`) was replaced because it started one gate run per completed workflow of the repository (about 22 per push on some repositories), which together with the polling exhausted the installation's API quota. + +*Consequence.* A required workflow that is missing from the list never triggers an evaluation. A pending commit may stay pending until the sweep (every 15 minutes) or another event evaluates it. An incomplete list can also leave stale success: after a sweep posts `success`, a re-run of the omitted workflow may fail without triggering reevaluation, leaving that success in place until another event or sweep evaluates the commit. Safety depends on keeping the required-workflow trigger list complete. + == Workflow acceptance contract [cols="1,4",options="header"] diff --git a/scripts/ci-gate-trigger-names.sh b/scripts/ci-gate-trigger-names.sh new file mode 100755 index 00000000..350486ec --- /dev/null +++ b/scripts/ci-gate-trigger-names.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# The `workflow_run.workflows` list of a CI/gate caller, derived from its profile. +# Usage: ci-gate-trigger-names.sh PROFILE.json REPO-ROOT +# `workflow_run.workflows` matches workflow NAMES (the top-level `name:` of a workflow file), not paths; +# a profile (profiles/ci-gate//.json) lists workflow FILES. This prints the JSON array +# (sorted, unique, one line) of the `name:` of each distinct workflow file the profile names. +# Exit 0 = printed; 2 = usage/tool error or an unusable workflow (file missing, no `name:`, a glob +# character in the name, or the name of the gate itself). Fail-closed: it never prints a partial list. +set -euo pipefail +GATE_NAME='CI gate' +[[ $# -eq 2 && -f $1 && -d $2 ]] || { echo 'usage: ci-gate-trigger-names.sh PROFILE.json REPO-ROOT' >&2; exit 2; } +command -v jq >/dev/null || { echo 'jq required' >&2; exit 2; } +command -v yq >/dev/null || { echo 'yq (mikefarah v4) required' >&2; exit 2; } +profile=$1 root=$2 +files=$(jq -r '.checks[].workflow' "$profile") || { echo "cannot read workflow files from $profile" >&2; exit 2; } +[[ -n $files ]] || { echo "profile $profile names no workflow" >&2; exit 2; } +names=() +while IFS= read -r wf; do + [[ -f $root/$wf ]] || { echo "workflow file missing: $wf" >&2; exit 2; } + name=$(yq -r '.name // ""' "$root/$wf") || { echo "cannot parse $wf" >&2; exit 2; } + [[ -n $name && $name != null ]] || { echo "workflow has no name: $wf" >&2; exit 2; } + [[ $name != *[\*\?\[\]\!+]* ]] || { echo "workflow name contains a glob character (* ? [ ] ! +): '$name' in $wf" >&2; exit 2; } + [[ $name != "$GATE_NAME" ]] || { echo "workflow name is the gate's own ('$GATE_NAME'): $wf" >&2; exit 2; } + names+=("$name") +done < <(sort -u <<<"$files") +printf '%s\n' "${names[@]}" | jq -Rn -c '[inputs] | unique' diff --git a/templates/ci-gate-caller.yml b/templates/ci-gate-caller.yml index efbcf5f7..623686c0 100644 --- a/templates/ci-gate-caller.yml +++ b/templates/ci-gate-caller.yml @@ -58,13 +58,21 @@ # reusable also re-reads the runs just before it writes and re-evaluates when they moved # (scripts/ci-gate-publish.sh; tests/ci-gate.sh asserts the template has no concurrency of its own). # +# ⚠ REPLACE `workflow_run.workflows` below. It matches workflow NAMES (the top-level `name:` of a workflow +# file), never paths, and it must be an EXPLICIT list, with no globs: it is the `name:` of every workflow +# file in your profile. Generate it from a checkout of this suite and of your repository: +# proven-cicd-and-workflows/scripts/ci-gate-trigger-names.sh \ +# proven-cicd-and-workflows/profiles/ci-gate//.json +# and paste the printed array. Left unchanged, the placeholder matches no workflow, the gate is never +# triggered by a workflow_run, and any check that compares the list with this script's output (as +# tests/ci-gate.sh does for the suite's own caller) fails on it. Do not leave the placeholder. # No `secrets:` line: the reusable consumes none. name: CI gate on: pull_request: workflow_run: - workflows: ["**", "!CI gate"] + workflows: ["Replace with the name: of each workflow in your profile"] # PLACEHOLDER: see the comment above types: [completed, requested, in_progress] schedule: - cron: "7,22,37,52 * * * *" diff --git a/tests/ci-gate.sh b/tests/ci-gate.sh index 79cf9afd..33bd3a51 100644 --- a/tests/ci-gate.sh +++ b/tests/ci-gate.sh @@ -1649,6 +1649,45 @@ for wf in $(jq -r '.checks[].workflow' "$SELFPROFILE" | sort -u); do [ -f "$ROOT/$wf" ] && ok "the suite's profile names an existing workflow $wf" || bad "the suite's profile names a missing workflow $wf" done + +echo "== the explicit workflow_run list (scripts/ci-gate-trigger-names.sh) ==" +NAMES="$ROOT/scripts/ci-gate-trigger-names.sh" +wfl() { yq -o=json -I=0 "$ON.workflow_run.workflows" "$1" 2> /dev/null; } # -> the list as JSON (null if absent) +list_of() { wfl "$1" | jq -c 'if type == "array" then sort else null end'; } # order-insensitive +globbad() { wfl "$1" | jq -c '[.[] | select(type != "string" or test("[*?\\[\\]!+]") or . == "CI gate")] | length'; } # glob or gate name +want=$(bash "$NAMES" "$SELFPROFILE" "$ROOT" 2>/dev/null | jq -c 'sort') +[ -n "$want" ] && ok "the script prints a list for the suite's profile: $want" || bad "the script printed nothing for the suite's profile" +chk "the suite's caller workflow_run.workflows equals the script's output for the suite's profile" "$(list_of "$SELF")" "$want" +# The control: the same comparison must FAIL for the retired glob, so the check above can fail. +GLOBCALLER="$WORK/glob-caller.yml"; sed 's|^ workflows: \[.*\]$| workflows: ["**", "!CI gate"]|' "$SELF" > "$GLOBCALLER" +[ "$(wfl "$GLOBCALLER")" = '["**","!CI gate"]' ] && ok "control: the temporary caller really carries [\"**\", \"!CI gate\"]" || bad "control: the glob caller was not built" +[ "$(list_of "$GLOBCALLER")" != "$want" ] && ok "control: the equality check FAILS for a caller that still has [\"**\", \"!CI gate\"]" || bad "control: the equality check passed for the retired glob" +for f in "$SELF" "$TEMPLATE"; do + n=$(basename "$f") + chk "$n: workflow_run.workflows has no glob character and not the gate's own name" "$(globbad "$f")" 0 + chk "$n: workflow_run.workflows is a non-empty list" "$(wfl "$f" | jq -c 'type == "array" and length > 0')" true +done +chk "control: the glob check sees the retired glob in a planted caller" "$(globbad "$FX/bad-glob-caller.yml")" 2 +printf 'on:\n workflow_run:\n workflows: ["Build C+"]\n' > "$WORK/plus-caller.yml" +chk "control: the glob check rejects Build C+ in a planted caller" "$(globbad "$WORK/plus-caller.yml")" 1 +chk "the template's list is a placeholder that is not any profile's output (a consumer must replace it)" "$(wfl "$TEMPLATE" | jq -c 'length == 1 and (.[0] | startswith("Replace with"))')" true +echo "-- negative cases of the script: each exits 2 --" +NR="$WORK/names-root"; mkdir -p "$NR/.github/workflows" +printf 'name: Alpha build\non: push\n' > "$NR/.github/workflows/a.yml" +printf 'on: push\n' > "$NR/.github/workflows/noname.yml" +printf 'name: "Build *"\non: push\n' > "$NR/.github/workflows/glob.yml" +printf 'name: "!Alpha"\non: push\n' > "$NR/.github/workflows/bang.yml" +printf 'name: "Build C+"\non: push\n' > "$NR/.github/workflows/plus.yml" +printf 'name: CI gate\non: push\n' > "$NR/.github/workflows/gate.yml" +np() { printf '{"checks":[%s],"repository":"example/repo","schema_version":2}\n' "$(jq -nr --args '$ARGS.positional | map("{\"job\":\"j\",\"workflow\":\".github/workflows/\(.)\"}") | join(",")' "$@")" > "$WORK/np.json"; } +np a.yml a.yml; OUT=$(bash "$NAMES" "$WORK/np.json" "$NR" 2>&1); RC=$? +chk "positive control: two jobs of one workflow give one name, exit 0" "$RC/$OUT" '0/["Alpha build"]' +for spec in missing.yml:missing noname.yml:'has no name' glob.yml:'glob character' bang.yml:'glob character' plus.yml:'glob character' gate.yml:"gate's own"; do + wf=${spec%%:*}; msg=${spec#*:} + np a.yml "$wf"; OUT=$(bash "$NAMES" "$WORK/np.json" "$NR" 2>&1); RC=$? + [ "$RC" = 2 ] && [[ $OUT == *"$msg"* ]] && [ "$(grep -c '^\[' <<<"$OUT")" = 0 ] && ok "script: $wf exits 2 with '$msg' and prints no list" || bad "script: $wf gave exit $RC: $OUT" +done +bash "$NAMES" "$WORK/np.json" >/dev/null 2>&1; [ $? = 2 ] && ok "script: a missing argument exits 2" || bad "script: a missing argument did not exit 2" echo printf 'ci-gate: %d passed, %d failed\n' "$PASS" "$FAIL" [ "$FAIL" -eq 0 ] diff --git a/tests/fixtures/ci-gate/bad-glob-caller.yml b/tests/fixtures/ci-gate/bad-glob-caller.yml new file mode 100644 index 00000000..197b5769 --- /dev/null +++ b/tests/fixtures/ci-gate/bad-glob-caller.yml @@ -0,0 +1,6 @@ +# Negative control: a caller whose workflow_run list is the retired glob. +name: CI gate +on: + workflow_run: + workflows: ["**", "!CI gate"] + types: [completed] diff --git a/tests/fixtures/ci-gate/bad-template.yml b/tests/fixtures/ci-gate/bad-template.yml index fe5d7ac8..2c1e1f2b 100644 --- a/tests/fixtures/ci-gate/bad-template.yml +++ b/tests/fixtures/ci-gate/bad-template.yml @@ -3,7 +3,7 @@ name: CI gate on: pull_request: workflow_run: - workflows: ["**"] + workflows: ["Example check"] types: [completed] concurrency: group: ci-gate-x