diff --git a/.gitea/workflows/quality-gates.yml b/.gitea/workflows/quality-gates.yml index a718a9e2..f281f13f 100644 --- a/.gitea/workflows/quality-gates.yml +++ b/.gitea/workflows/quality-gates.yml @@ -142,6 +142,8 @@ jobs: corepack pnpm install --frozen-lockfile --ignore-scripts - name: Build candidate once and verify local evidence run: corepack pnpm ci:gate -- FE-GATE-015 + - name: Admit the candidate to the declared release target + run: corepack pnpm ci:gate -- FE-GATE-027 - name: Archive and validate the exact candidate file set id: candidate run: | diff --git a/docs/operations/ci-quality-gates.md b/docs/operations/ci-quality-gates.md index ecc0213d..4943f218 100644 --- a/docs/operations/ci-quality-gates.md +++ b/docs/operations/ci-quality-gates.md @@ -329,6 +329,15 @@ Repository variables required by higher tiers: `ubuntu-latest` runner label; the workflow exposes it to gates as `CI_RUNNER_IMAGE` (required by every tier) - `HOSTING_BASE_URL` for live header verification +- `APP_PROFILE` for the runtime profile the release candidate is built from + (`local`, `development`, `staging`, `production`; + `scripts/generate-runtime-config.ts`). An unset value builds `local`; an empty + value fails the build. +- `RELEASE_TARGET` for the environment FE-GATE-027 admits the candidate to + (`scripts/check-release-admission.ts`). It has no default. The immutable_build + job runs FE-GATE-015 and then FE-GATE-027, and FE-GATE-027 refuses the + candidate when `APP_PROFILE` is set and differs from `RELEASE_TARGET`. Both are + repository-wide variables, so set them together for each release. - `FIELD_WEB_VITALS_INPUT` for the privacy-approved field sample document - `MIN_ELIGIBLE_SAMPLES` after the baseline decision - `VULNERABILITY_PROVIDER_COMMAND` and `PROVENANCE_PROVIDER_COMMAND` for the diff --git a/scripts/check-ci-contract.ts b/scripts/check-ci-contract.ts index d242427a..63949858 100644 --- a/scripts/check-ci-contract.ts +++ b/scripts/check-ci-contract.ts @@ -10,7 +10,11 @@ import { loadCiGateContract, indexCiGateContract, } from "./contracts/ci-gates.ts"; -import { generateCiWorkflow, renderCiWorkflow } from "./generate-ci-workflow.ts"; +import { + generateCiWorkflow, + renderCiWorkflow, + workflowGateExecutionFailures, +} from "./generate-ci-workflow.ts"; import { ciContractReportSchema, } from "./lib/ci-contract-report.ts"; @@ -113,6 +117,7 @@ try { } catch (error) { failures.push(`generated workflow is unreadable: ${error instanceof Error ? error.message : String(error)}`); } +failures.push(...workflowGateExecutionFailures(contract, checkedWorkflowBytes.toString("utf8"))); const report = ciContractReportSchema.parse({ schemaVersion: 2, nodeVersion, diff --git a/scripts/check-release-admission.ts b/scripts/check-release-admission.ts index ff852074..be06a6d6 100644 --- a/scripts/check-release-admission.ts +++ b/scripts/check-release-admission.ts @@ -40,6 +40,17 @@ async function main(): Promise { process.exitCode = 1; return; } + // 13-01: the build profile and the admission target are separate repository + // variables; a profile left on staging must not ship to production. + const profile = process.env["APP_PROFILE"]; + if (profile !== undefined && profile !== declared) { + process.stderr.write( + `release admission refused: the artifact was built with APP_PROFILE=${profile} ` + + `but RELEASE_TARGET=${declared}. Set both to the same environment.\n`, + ); + process.exitCode = 1; + return; + } let document: unknown; try { diff --git a/scripts/generate-ci-workflow.ts b/scripts/generate-ci-workflow.ts index f7d6ac61..6424d2eb 100644 --- a/scripts/generate-ci-workflow.ts +++ b/scripts/generate-ci-workflow.ts @@ -196,10 +196,16 @@ function renderStep( " run: corepack pnpm exec playwright install --with-deps chromium firefox webkit", ]; case "run-gate": { - const gateId = job.kind === "gate-matrix" ? "${{ matrix.gate }}" : job.gateIds[0]; - if (!gateId) throw new TypeError(`run-gate step lacks ownership: ${job.id}`); - const name = job.id === "documentation_gate" ? "Run documentation gate" : job.id === "immutable_build" ? "Build candidate once and verify local evidence" : "Run blocking gate"; - return [` - name: ${name}`, ` run: corepack pnpm ci:gate -- ${gateId}`]; + if (job.kind === "gate-matrix") { + return [" - name: Run blocking gate", " run: corepack pnpm ci:gate -- ${{ matrix.gate }}"]; + } + if (job.gateIds.length === 0) throw new TypeError(`run-gate step lacks ownership: ${job.id}`); + // 12-02: every gate the job owns runs, in order. Rendering only the first + // left FE-GATE-027 (release admission) out of the workflow. + return job.gateIds.flatMap((gateId) => [ + ` - name: ${runGateStepName(job.id, gateId)}`, + ` run: corepack pnpm ci:gate -- ${gateId}`, + ]); } case "archive-candidate": { const archive = shellDoubleQuoted(step.archivePath); @@ -346,6 +352,38 @@ function shellDoubleQuoted(value: string): string { return `"${escaped}"`; } +function runGateStepName(jobId: string, gateId: string): string { + if (gateId === "FE-GATE-015") return "Build candidate once and verify local evidence"; + if (gateId === "FE-GATE-027") return "Admit the candidate to the declared release target"; + return jobId === "documentation_gate" ? "Run documentation gate" : "Run blocking gate"; +} + +/** + * 12-02 / D-12. Every contract gate must be executed by the workflow text: a + * literal `ci:gate -- ` run line, or a matrix entry of a job whose run step + * is `ci:gate -- ${{ matrix.gate }}`. Owning a gate in the contract is not + * enough; the generator once dropped all but the first. + */ +export function workflowGateExecutionFailures( + contract: CiGateContract, + workflow: string, +): string[] { + const executed = new Set(); + for (const job of workflow.split(/^(?= {2}[a-z_]+:$)/mu)) { + for (const match of job.matchAll(/^ +run: corepack pnpm ci:gate -- (FE-GATE-\d{3})$/gmu)) { + executed.add(match[1]!); + } + if (/^ +run: corepack pnpm ci:gate -- \$\{\{ matrix\.gate \}\}$/mu.test(job)) { + for (const match of job.matchAll(/^ +- \{ gate: (FE-GATE-\d{3}),/gmu)) { + executed.add(match[1]!); + } + } + } + return contract.gates + .filter(({ id }) => !executed.has(id)) + .map(({ id }) => `gate is not executed by any workflow run step: ${id}`); +} + function humanize(value: string): string { return value.replaceAll("-", " "); } diff --git a/tests/unit/__snapshots__/ci-workflow-generation.test.ts.snap b/tests/unit/__snapshots__/ci-workflow-generation.test.ts.snap index 3e54e757..4794345a 100644 --- a/tests/unit/__snapshots__/ci-workflow-generation.test.ts.snap +++ b/tests/unit/__snapshots__/ci-workflow-generation.test.ts.snap @@ -145,6 +145,8 @@ jobs: corepack pnpm install --frozen-lockfile --ignore-scripts - name: Build candidate once and verify local evidence run: corepack pnpm ci:gate -- FE-GATE-015 + - name: Admit the candidate to the declared release target + run: corepack pnpm ci:gate -- FE-GATE-027 - name: Archive and validate the exact candidate file set id: candidate run: | diff --git a/tests/unit/ci-workflow-generation.test.ts b/tests/unit/ci-workflow-generation.test.ts index eb6f17eb..1b457e54 100644 --- a/tests/unit/ci-workflow-generation.test.ts +++ b/tests/unit/ci-workflow-generation.test.ts @@ -20,6 +20,7 @@ import { createCiWorkflowGenerator, generateCiWorkflow, renderCiWorkflow, + workflowGateExecutionFailures, type CiWorkflowFileSystem, } from "../../scripts/generate-ci-workflow.ts"; import { @@ -1411,6 +1412,33 @@ describe("CI workflow generation", () => { expect(actionUses.every((uses) => /^https:\/\/github\.com\/[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+@[0-9a-f]{40}$/u.test(uses!))).toBe(true); }); + // 12-02 / 13-01: immutable_build owns FE-GATE-015 and FE-GATE-027, but only + // the first gate of a non-matrix job was ever rendered, so release admission + // never ran in CI. + it("runs every gate a job owns, in order", async () => { + const contract = await loadCiGateContract(process.cwd()); + const workflow = renderCiWorkflow(contract); + const immutable = workflow.slice( + workflow.indexOf(" immutable_build:\n"), + workflow.indexOf(" vulnerability_provider:\n"), + ); + expect([...immutable.matchAll(/ci:gate -- (FE-GATE-\d{3})$/gmu)].map((match) => match[1])) + .toEqual(["FE-GATE-015", "FE-GATE-027"]); + expect(workflowGateExecutionFailures(contract, workflow)).toEqual([]); + }); + + it.each([ + ["a non-matrix gate step", " run: corepack pnpm ci:gate -- FE-GATE-027\n", "FE-GATE-027"], + ["a matrix entry", " - { gate: FE-GATE-020, name: removability, browser: false }\n", "FE-GATE-020"], + ])("rejects a workflow that drops %s", async (_name, line, gateId) => { + const contract = await loadCiGateContract(process.cwd()); + const workflow = renderCiWorkflow(contract); + expect(workflow).toContain(line); + expect(workflowGateExecutionFailures(contract, workflow.replace(line, ""))).toEqual([ + `gate is not executed by any workflow run step: ${gateId}`, + ]); + }); + it("derives download artifact names from the typed upload producer", async () => { const contract = await loadCiGateContract(process.cwd()); const candidate = JSON.parse(JSON.stringify(contract)) as Record; diff --git a/tests/unit/deployment-admission.test.ts b/tests/unit/deployment-admission.test.ts index b0e71a39..2d3c57cb 100644 --- a/tests/unit/deployment-admission.test.ts +++ b/tests/unit/deployment-admission.test.ts @@ -1,4 +1,6 @@ -import { readFile, readdir } from "node:fs/promises"; +import { spawnSync } from "node:child_process"; +import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; import path from "node:path"; import { describe, expect, it } from "vitest"; @@ -201,4 +203,35 @@ describe("runtime config profiles", () => { .length, ).toBeGreaterThan(0); }); + + // 13-01: the build profile and the admission target are two repository + // variables. A staging profile left in place for a production release has to + // stop the release, whatever the built document happens to say. + it.each([ + ["staging", 1], + ["production", 0], + ] as const)("admits a production artifact under APP_PROFILE=%s only when it names the target", async (profile, status) => { + const root = await mkdtemp(path.join(tmpdir(), "release-admission-profile-")); + try { + const config = await generateRuntimeConfig("production", { + VITE_BUILD_ID: "20260815.42", + RELEASE_ID: "r-2026.08.15-1", + }); + await mkdir(path.join(root, "dist"), { recursive: true }); + await writeFile(path.join(root, "dist/config.json"), JSON.stringify(config)); + const result = spawnSync( + process.execPath, + [path.resolve("scripts/check-release-admission.ts")], + { + cwd: root, + encoding: "utf8", + env: { ...process.env, APP_PROFILE: profile, RELEASE_TARGET: "production" }, + }, + ); + expect(result.status, result.stderr).toBe(status); + if (status === 1) expect(result.stderr).toMatch(/APP_PROFILE=staging/u); + } finally { + await rm(root, { recursive: true, force: true }); + } + }); });