diff --git a/.gitea/workflows/quality-gates.yml b/.gitea/workflows/quality-gates.yml index 5bec0833..0f4e9a3a 100644 --- a/.gitea/workflows/quality-gates.yml +++ b/.gitea/workflows/quality-gates.yml @@ -123,6 +123,10 @@ jobs: if: ${{ gitea.event_name != 'workflow_dispatch' || (inputs.stage != 'documentation' && inputs.stage != 'main') }} runs-on: ubuntu-latest timeout-minutes: 45 + container: + image: "mcr.microsoft.com/playwright:v1.62.0-noble@sha256:baed2032d533817f3dbe6425de795788430ba345e819a1201337009ba17c9d07" + env: + CI_RUNNER_IMAGE: "mcr.microsoft.com/playwright:v1.62.0-noble@sha256:baed2032d533817f3dbe6425de795788430ba345e819a1201337009ba17c9d07" steps: - uses: https://github.com/actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 with: @@ -134,9 +138,9 @@ jobs: run: | corepack enable corepack pnpm install --frozen-lockfile --ignore-scripts - - name: Install Playwright browsers + - name: Install visual-test fonts run: | - corepack pnpm exec playwright install --with-deps chromium firefox webkit + $(command -v sudo || true) apt-get update $(command -v sudo || true) apt-get install -y fonts-noto-core fonts-noto-cjk fonts-noto-cjk-extra fonts-liberation - name: "FE-GATE-008 / e2e" if: ${{ !cancelled() }} diff --git a/config/ci/gates.json b/config/ci/gates.json index 5ec9f082..2e90db25 100644 --- a/config/ci/gates.json +++ b/config/ci/gates.json @@ -2222,7 +2222,8 @@ "kind": "frozen-install" }, { - "kind": "browser-install" + "kind": "browser-install", + "image": "mcr.microsoft.com/playwright:v1.62.0-noble@sha256:baed2032d533817f3dbe6425de795788430ba345e819a1201337009ba17c9d07" }, { "kind": "run-gate" diff --git a/scripts/contracts/ci-gates.ts b/scripts/contracts/ci-gates.ts index 16e67306..371257d6 100644 --- a/scripts/contracts/ci-gates.ts +++ b/scripts/contracts/ci-gates.ts @@ -333,7 +333,18 @@ const environmentBindingSchema = z const checkoutStep = z.object({ kind: z.literal("checkout") }).strict(); const setupNodeStep = z.object({ kind: z.literal("setup-node") }).strict(); const frozenInstallStep = z.object({ kind: z.literal("frozen-install") }).strict(); -const browserInstallStep = z.object({ kind: z.literal("browser-install") }).strict(); +/** + * `image`: the job runs in Playwright's own image for the pinned + * @playwright/test version, which already holds the browsers and their OS + * libraries (tests/unit/ci-browser-image.test.ts keeps the versions equal). + * Without it the step downloads them, about 900 MB and 3 to 4 minutes a job. + */ +export const PLAYWRIGHT_JOB_IMAGE_PATTERN = + /^mcr\.microsoft\.com\/playwright:v(\d+\.\d+\.\d+)-noble@sha256:[0-9a-f]{64}$/u; +const browserInstallStep = z.object({ + kind: z.literal("browser-install"), + image: z.string().regex(PLAYWRIGHT_JOB_IMAGE_PATTERN).optional(), +}).strict(); const runGateStep = z.object({ kind: z.literal("run-gate") }).strict(); const archiveCandidateStep = z .object({ @@ -1001,6 +1012,9 @@ function validateContractSemantics( const browserSteps = job.steps.filter(({ kind }) => kind === "browser-install").length; const expected = job.browserGateIds.length > 0 ? 1 : 0; if (browserSteps !== expected) issue(`browser install step drift: ${job.id}`); + // A matrix job's non-browser lanes would run in the browser image too. + const imaged = job.steps.some((step) => step.kind === "browser-install" && step.image !== undefined); + if (imaged && job.kind !== "gate-sequence") issue(`browser job image outside a gate sequence: ${job.id}`); } for (const cycle of findCycles(contract.jobs.map((job) => [job.id, job.needs] as const))) { issue(`job dependency cycle: ${cycle.join(" -> ")}`); diff --git a/scripts/generate-ci-workflow.ts b/scripts/generate-ci-workflow.ts index 85b69b21..4ef8e811 100644 --- a/scripts/generate-ci-workflow.ts +++ b/scripts/generate-ci-workflow.ts @@ -190,6 +190,8 @@ function renderJob( " runs-on: ubuntu-latest", ` timeout-minutes: ${job.timeoutMinutes}`, ); + const image = jobImage(job); + if (image) lines.push(" container:", ` image: ${yamlScalar(image)}`); if (job.kind === "immutable") { const archive = job.steps.find((step) => step.kind === "archive-candidate"); if (!archive || archive.kind !== "archive-candidate") throw new TypeError("immutable job lacks archive step"); @@ -213,12 +215,13 @@ function renderJob( lines.push(" outputs:", " deployed: ${{ steps.deploy.outputs.deployed }}"); } const jobEnvironment = job.environment.filter((binding) => !isStepScopedSecret(job, binding)); - if (jobEnvironment.length > 0) { - lines.push(" env:"); - for (const binding of jobEnvironment) { - lines.push(` ${binding.name}: ${yamlScalar(binding.value)}`); - } - } + // The build records the image it ran in (scripts/lib/build-environment.ts): + // a job in its own image names that one, not the runner label's. + const environmentLines = [ + ...(image ? [` CI_RUNNER_IMAGE: ${yamlScalar(image)}`] : []), + ...jobEnvironment.map((binding) => ` ${binding.name}: ${yamlScalar(binding.value)}`), + ]; + if (environmentLines.length > 0) lines.push(" env:", ...environmentLines); if (job.kind === "gate-matrix") { lines.push(" strategy:", " fail-fast: false", " matrix:", " include:"); const includesBrowser = job.steps.some(({ kind }) => kind === "browser-install"); @@ -263,6 +266,14 @@ function gateHostPrefix(gate: Readonly<{ hostEnvironment?: "system-container" | return gate?.hostEnvironment === "system-container" ? "scripts/run-in-system-container.sh " : ""; } +/** The Playwright image a browser job runs in (`browser-install.image`), if any. */ +function jobImage(job: CiWorkflowJob): string | undefined { + for (const step of job.steps) { + if (step.kind === "browser-install" && step.image) return step.image; + } + return undefined; +} + function renderStep( job: CiWorkflowJob, step: CiWorkflowStep, @@ -290,6 +301,16 @@ function renderStep( " corepack pnpm install --frozen-lockfile --ignore-scripts", ]; case "browser-install": + if (step.image) { + // The job image holds the browsers and their OS libraries; the goldens' + // faces are still OS packages it lacks, and it ships no package lists. + return [ + " - name: Install visual-test fonts", + " run: |", + " $(command -v sudo || true) apt-get update", + ` $(command -v sudo || true) ${VISUAL_TEST_FONT_INSTALL_ARGUMENTS}`, + ]; + } return [ " - name: Install Playwright browsers", ...(job.kind === "gate-matrix" ? [" if: ${{ matrix.browser }}"] : []), diff --git a/tests/unit/__snapshots__/ci-workflow-generation.test.ts.snap b/tests/unit/__snapshots__/ci-workflow-generation.test.ts.snap index fe24920e..a06b7e03 100644 --- a/tests/unit/__snapshots__/ci-workflow-generation.test.ts.snap +++ b/tests/unit/__snapshots__/ci-workflow-generation.test.ts.snap @@ -126,6 +126,10 @@ jobs: if: \${{ gitea.event_name != 'workflow_dispatch' || (inputs.stage != 'documentation' && inputs.stage != 'main') }} runs-on: ubuntu-latest timeout-minutes: 45 + container: + image: "mcr.microsoft.com/playwright:v1.62.0-noble@sha256:baed2032d533817f3dbe6425de795788430ba345e819a1201337009ba17c9d07" + env: + CI_RUNNER_IMAGE: "mcr.microsoft.com/playwright:v1.62.0-noble@sha256:baed2032d533817f3dbe6425de795788430ba345e819a1201337009ba17c9d07" steps: - uses: https://github.com/actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 with: @@ -137,9 +141,9 @@ jobs: run: | corepack enable corepack pnpm install --frozen-lockfile --ignore-scripts - - name: Install Playwright browsers + - name: Install visual-test fonts run: | - corepack pnpm exec playwright install --with-deps chromium firefox webkit + $(command -v sudo || true) apt-get update $(command -v sudo || true) apt-get install -y fonts-noto-core fonts-noto-cjk fonts-noto-cjk-extra fonts-liberation - name: "FE-GATE-008 / e2e" if: \${{ !cancelled() }} diff --git a/tests/unit/ci-browser-image.test.ts b/tests/unit/ci-browser-image.test.ts new file mode 100644 index 00000000..127c9b1c --- /dev/null +++ b/tests/unit/ci-browser-image.test.ts @@ -0,0 +1,52 @@ +import { readFileSync } from "node:fs"; + +import { describe, expect, it } from "vitest"; + +import { loadCiGateContract, PLAYWRIGHT_JOB_IMAGE_PATTERN } from "../../scripts/contracts/ci-gates.ts"; +import { renderCiWorkflow } from "../../scripts/generate-ci-workflow.ts"; + +/** + * merge_browser runs in Playwright's own image so it does not download the + * browsers and their OS libraries in every job (about 900 MB, 200 to 240 s on + * the runner). The image's browsers are the ones @playwright/test of the same + * version expects; a Playwright update without the image left the browsers + * missing (or the image without the version's revisions). + */ +describe("browser job image", () => { + it("is Playwright's image for the pinned @playwright/test version, by digest", async () => { + const pinned = ( + JSON.parse(readFileSync("package.json", "utf8")) as { devDependencies: Record } + ).devDependencies["@playwright/test"]; + const contract = await loadCiGateContract(process.cwd()); + const images = contract.jobs.flatMap((job) => + job.steps.flatMap((step) => (step.kind === "browser-install" && step.image ? [[job.id, step.image] as const] : [])), + ); + expect(images.map(([jobId]) => jobId)).toEqual(["merge_browser"]); + for (const [, image] of images) { + expect(PLAYWRIGHT_JOB_IMAGE_PATTERN.exec(image)?.[1]).toBe(pinned); + } + }); + + it("is the job container, and the runner image the build records", async () => { + const contract = await loadCiGateContract(process.cwd()); + const image = contract.jobs + .find(({ id }) => id === "merge_browser")! + .steps.find((step) => step.kind === "browser-install"); + const workflow = renderCiWorkflow(contract); + const job = workflow.slice(workflow.indexOf(" merge_browser:\n"), workflow.indexOf(" release_gate:\n")); + expect(image?.kind === "browser-install" && image.image).toBeTruthy(); + const ref = image?.kind === "browser-install" ? image.image! : ""; + expect(job).toContain(` container:\n image: "${ref}"\n`); + expect(job).toContain(` env:\n CI_RUNNER_IMAGE: "${ref}"\n`); + expect(job).not.toContain("playwright install"); + }); + + it("refuses an image outside Playwright's registry or without a digest", async () => { + for (const image of [ + "mcr.microsoft.com/playwright:v1.62.0-noble", + "docker.io/library/ubuntu:24.04@sha256:" + "a".repeat(64), + ]) { + expect(PLAYWRIGHT_JOB_IMAGE_PATTERN.test(image), image).toBe(false); + } + }); +}); diff --git a/tests/unit/ci-workflow-generation.test.ts b/tests/unit/ci-workflow-generation.test.ts index ef831823..765ef665 100644 --- a/tests/unit/ci-workflow-generation.test.ts +++ b/tests/unit/ci-workflow-generation.test.ts @@ -1641,17 +1641,25 @@ describe("CI workflow generation", () => { // them, after `--with-deps` has refreshed the package lists. it("installs the visual-test font packages in every browser step", async () => { const workflow = renderCiWorkflow(await loadCiGateContract(process.cwd())); - const browserSteps = workflow - .split(" - name: ") - .filter((step) => step.startsWith("Install Playwright browsers")); - expect(browserSteps).toHaveLength(2); - for (const step of browserSteps) { + const steps = workflow.split(" - name: "); + const fontsInstall = `apt-get install -y ${VISUAL_TEST_FONT_PACKAGES.join(" ")}`; + // A job without its own image downloads the browsers with their OS + // libraries; `--with-deps` refreshes the package lists first. + const downloading = steps.filter((step) => step.startsWith("Install Playwright browsers")); + // merge_browser runs in Playwright's image, which has the browsers but no + // package lists (tests/unit/ci-browser-image.test.ts). + const imaged = steps.filter((step) => step.startsWith("Install visual-test fonts")); + expect([downloading.length, imaged.length]).toEqual([1, 1]); + for (const step of downloading) { const playwright = step.indexOf("playwright install --with-deps"); - const fonts = step.indexOf( - `apt-get install -y ${VISUAL_TEST_FONT_PACKAGES.join(" ")}`, - ); expect(playwright).toBeGreaterThan(0); - expect(fonts).toBeGreaterThan(playwright); + expect(step.indexOf(fontsInstall)).toBeGreaterThan(playwright); + } + for (const step of imaged) { + expect(step).not.toContain("playwright install"); + const update = step.indexOf("apt-get update"); + expect(update).toBeGreaterThan(0); + expect(step.indexOf(fontsInstall)).toBeGreaterThan(update); } expect(VISUAL_TEST_FONT_PACKAGES).toEqual([ "fonts-noto-core",