From 086e7626ea94cdc45fd2b34d07a3cac6e0ec1b97 Mon Sep 17 00:00:00 2001 From: wyuc Date: Wed, 19 Aug 2026 15:36:00 +0800 Subject: [PATCH] ci: cut wall-clock time and bound Playwright browser installs (#1146) Cache Playwright browsers and Next compile artifacts, retry a timed-out Chromium download, build the production bundle before Playwright starts, and run Prettier/ESLint/tsc/i18n in parallel. Unit tests stay sequential. Skip Playwright apt deps on ubuntu-latest. Closes #1145 --- .github/workflows/ci.yml | 87 +++++++++++++++++++---- .github/workflows/storage-pg-contract.yml | 1 + playwright.config.ts | 14 ++-- scripts/ci-run-parallel.sh | 58 +++++++++++++++ 4 files changed, 143 insertions(+), 17 deletions(-) create mode 100755 scripts/ci-run-parallel.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 81d085b..c256c5b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,11 +28,26 @@ jobs: check: name: Lint, Typecheck & Unit Tests runs-on: ubuntu-latest + timeout-minutes: 15 steps: - uses: actions/checkout@v4 with: fetch-depth: 0 + # Cheap bash-only check so a broken helper fails before pnpm install. + # Quiet annotations: the expected-failure case would otherwise paint + # the check with a fake ::error::. + - name: Parallel runner self-test + env: + CI_PARALLEL_ANNOTATE: '0' + run: | + set -euo pipefail + scripts/ci-run-parallel.sh ok 'true' ok2 'true' + if scripts/ci-run-parallel.sh ok 'true' bad 'exit 7'; then + echo 'ci-run-parallel.sh should have failed' + exit 1 + fi + - uses: pnpm/action-setup@v4 - uses: actions/setup-node@v4 @@ -101,18 +116,19 @@ jobs: fi echo "Tracked package files match the commit under test." - - name: Prettier - run: pnpm check - - - name: ESLint - run: pnpm lint - - - name: TypeScript - run: npx tsc --noEmit - - - name: i18n Key Alignment - run: pnpm check:i18n-keys + # These four do not share state. Sequential they were ~2 minutes; the + # slowest (ESLint) is the new bound. + - name: Prettier, ESLint, TypeScript, i18n + run: | + scripts/ci-run-parallel.sh \ + 'prettier' 'pnpm check' \ + 'eslint' 'pnpm lint' \ + 'tsc' 'npx tsc --noEmit' \ + 'i18n' 'pnpm check:i18n-keys' + # Keep these sequential. Running root vitest alongside the storage + # package on a 4-core runner made 5s-timeout tests flake (CPU + # contention, not a product regression). - name: Unit Tests run: pnpm test @@ -169,6 +185,7 @@ jobs: render-service: name: Render Service (typecheck + tests) runs-on: ubuntu-latest + timeout-minutes: 10 # The producer dep bundles puppeteer; skip its Chromium download — the unit # tests exercise the service's boundaries (unzip, admission, body caps) and # never launch a browser. @@ -204,6 +221,7 @@ jobs: e2e: name: E2E Tests runs-on: ubuntu-latest + timeout-minutes: 15 steps: - uses: actions/checkout@v4 @@ -216,8 +234,32 @@ jobs: - run: pnpm install --frozen-lockfile - - name: Install Playwright browsers - run: pnpm exec playwright install chromium --with-deps + # Browser tarballs live under ~/.cache/ms-playwright. A cold download of + # Chromium from the Playwright CDN has sat for 20+ minutes on this runner + # fleet; a lockfile-keyed cache turns that into a restore. + - name: Cache Playwright browsers + id: playwright-cache + uses: actions/cache@v4 + with: + path: ~/.cache/ms-playwright + key: ${{ runner.os }}-playwright-${{ hashFiles('pnpm-lock.yaml') }} + + # Browser tarball only. `install-deps` talks to Ubuntu apt; on this + # runner fleet that mirror has sat through the whole 15-minute job + # budget. ubuntu-latest already ships the libraries Chromium needs. + # Bound the Microsoft CDN download so a stall retries instead of hanging. + - name: Install Playwright Chromium + run: | + set -euo pipefail + for attempt in 1 2 3; do + if timeout -k 10 240 pnpm exec playwright install chromium; then + exit 0 + fi + echo "Playwright Chromium download attempt ${attempt} failed, retrying..." + sleep 5 + done + echo "::error::Playwright Chromium download failed after 3 attempts." + exit 1 # Measures the emitted cover cards in Chromium. Lives with the unit tests # (it needs the compiler), but only this job has a browser, so it runs @@ -264,6 +306,25 @@ jobs: fi done + # Incremental compilation artifacts only. Keyed on the lockfile plus + # next.config.ts so a dependency or webpack-rule change misses; source + # edits still reuse the restore-key prefix. + - name: Cache Next.js build + uses: actions/cache@v4 + with: + path: .next/cache + key: ${{ runner.os }}-nextjs-${{ hashFiles('pnpm-lock.yaml') }}-${{ hashFiles('next.config.ts') }} + restore-keys: | + ${{ runner.os }}-nextjs-${{ hashFiles('pnpm-lock.yaml') }}- + + # Dedicated so a cold `next build` is not charged against Playwright's + # webServer readiness budget (120s). NEXT_PUBLIC_MAIC_EDITOR_ENABLED is a + # build-time flag and must be set here, not only on `pnpm start`. + - name: Build + run: pnpm build + env: + NEXT_PUBLIC_MAIC_EDITOR_ENABLED: 'true' + - name: Run e2e tests run: pnpm exec playwright test diff --git a/.github/workflows/storage-pg-contract.yml b/.github/workflows/storage-pg-contract.yml index 4b4f3d7..afb336a 100644 --- a/.github/workflows/storage-pg-contract.yml +++ b/.github/workflows/storage-pg-contract.yml @@ -14,6 +14,7 @@ jobs: storage-pg-contract: name: Storage Contract (PostgreSQL 16) runs-on: ubuntu-latest + timeout-minutes: 10 services: postgres: image: postgres:16 diff --git a/playwright.config.ts b/playwright.config.ts index c52bab6..30fbefb 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -5,7 +5,9 @@ export default defineConfig({ fullyParallel: true, forbidOnly: !!process.env.CI, retries: process.env.CI ? 2 : 0, - workers: process.env.CI ? 1 : undefined, + // CI builds the production bundle in a dedicated workflow step, so this + // process only drives Chromium. Two workers fit a 4-core runner. + workers: process.env.CI ? 2 : undefined, reporter: process.env.CI ? 'html' : 'list', use: { baseURL: 'http://localhost:3002', @@ -19,13 +21,17 @@ export default defineConfig({ }, ], webServer: { - command: process.env.CI ? 'pnpm build && pnpm start' : 'pnpm dev', + // In CI the production build runs as a dedicated workflow step before + // Playwright, so this only boots the already-built server (`pnpm start`). + // The 120s budget covers startup, not the (much slower) build. Locally we + // run the dev server. + command: process.env.CI ? 'pnpm start' : 'pnpm dev', url: 'http://localhost:3002', reuseExistingServer: !process.env.CI, timeout: 120_000, // Enable the MAIC Editor (Pro mode) so editor e2e can reach it. This is a - // build-time NEXT_PUBLIC_* flag, so it must be set when the webServer runs - // `pnpm build` (CI) or `pnpm dev` (local). + // build-time NEXT_PUBLIC_* flag: in CI it must be set on the dedicated + // `pnpm build` step; locally `pnpm dev` reads it here. env: { PORT: '3002', NEXT_PUBLIC_MAIC_EDITOR_ENABLED: 'true' }, }, }); diff --git a/scripts/ci-run-parallel.sh b/scripts/ci-run-parallel.sh new file mode 100755 index 0000000..e2586fd --- /dev/null +++ b/scripts/ci-run-parallel.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash +# Run NAME/COMMAND pairs in parallel, print each log as a GitHub Actions group, +# and exit non-zero if any command failed. +# +# Usage: +# scripts/ci-run-parallel.sh NAME COMMAND [NAME COMMAND ...] +set -uo pipefail + +if [ "$#" -lt 2 ] || [ $(( $# % 2 )) -ne 0 ]; then + echo "usage: $0 NAME COMMAND [NAME COMMAND ...]" >&2 + exit 2 +fi + +logdir="${RUNNER_TEMP:-$(mktemp -d)}/ci-parallel-$$" +mkdir -p "$logdir" + +pids=() +names=() +logs=() +i=0 +while [ "$#" -ge 2 ]; do + name="$1" + cmd="$2" + shift 2 + log="$logdir/$i.log" + names+=("$name") + logs+=("$log") + ( + bash -c "$cmd" >"$log" 2>&1 + ) & + pids+=("$!") + i=$((i + 1)) +done + +fail=0 +for idx in "${!pids[@]}"; do + pid="${pids[$idx]}" + name="${names[$idx]}" + log="${logs[$idx]}" + if wait "$pid"; then + echo "::group::${name} (ok)" + cat "$log" + echo "::endgroup::" + else + status=$? + if [ "${CI_PARALLEL_ANNOTATE:-1}" != "0" ]; then + echo "::error::${name} failed with exit ${status}" + else + echo "${name} failed with exit ${status}" + fi + echo "::group::${name} (failed)" + cat "$log" + echo "::endgroup::" + fail=1 + fi +done + +exit "$fail"