Unverified Commit 1a1d1c97 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

ci: run Linux Playwright jobs in prebuilt Microsoft container (#93794)

## Summary

Runs Linux `build_reusable.yml` jobs that need Playwright inside
Microsoft's prebuilt
[`mcr.microsoft.com/playwright:v1.58.2-noble`](https://mcr.microsoft.com/en-us/product/playwright/about)
image. The image already contains Chromium, Firefox, WebKit, and their
system dependencies under `/ms-playwright`, so Linux test shards no
longer spend each job downloading browsers and installing apt deps.

- Adds a `playwrightDockerImage` input, defaulting to the Ubuntu 24.04
(`-noble`) Playwright image so glibc matches the `ubuntu-latest` runners
that build `next-swc.linux-x64-gnu.node`.
- Adds a `usePlaywrightContainer` input so jobs can opt out.
`test-next-swc-wasm` opts out because its `afterBuild` step needs
`rustup`, which is not in the Playwright image.
- Skips `pnpm playwright install --with-deps` for Linux jobs running in
the image; Windows jobs and opted-out jobs still install Playwright at
runtime.
- Enables the pnpm store cache inside container jobs because the
container filesystem is ephemeral even when the host runner is
persistent.

### Container condition

The Playwright image is used when all of the following are true:

- `inputs.needsPlaywright != 'no'`
- `inputs.skipInstallBuild != 'yes'`
- `inputs.usePlaywrightContainer != 'no'`
- The runner is not Windows

Otherwise `container.image` evaluates to `''` and the job runs directly
on the runner.

### Compatibility fixes

The Microsoft image is browser-focused, so this PR adds a few CI setup
fixes before the existing test workflow runs:

| Concern | Fix |
| --- | --- |
| Missing `jq` / `unzip` / native build toolchain | Install `jq`,
`unzip`, and `build-essential` inside Linux container jobs. |
| Firefox refuses `$HOME` not owned by the current user | Re-own GitHub
Actions' mounted `/github/home` directory for the root user used by the
job. |
| Git sees the mounted workspace as dubious ownership | Mark
`$GITHUB_WORKSPACE` as a safe directory for Git. |
| Standalone tests bind to Docker's generated `HOSTNAME` | Set container
`HOSTNAME=0.0.0.0`. |
| `getStorageDirectory()` treats Docker storage as ephemeral | Add
`NEXT_IGNORE_IS_DOCKER=1` for CI container jobs and honor it in
`cache-dir.ts` so deterministic-build tests keep using `dist/cache`. |
| `localhost` IPv4/IPv6 mismatch in analyze tests | Let the analyze
server listen on both IPv4 and IPv6 localhost addresses. |
| Nx fixture can race in CI before Next.js starts | Disable Nx isolated
plugins for the `nx-handling` fixture scripts. |

## Timing

Earlier comparison runs showed the Playwright install/setup portion
across 82 matched Linux jobs dropping from about **3 h 03 m total** on
canary to about **39 m 24 s total** with the containerized setup,
roughly **1 m 45 s saved per job** on average. Test execution itself is
excluded from that comparison because shard balancing makes it noisy.

## Test plan

- [x] Latest PR CI run
[`26121297957`](https://github.com/vercel/next.js/actions/runs/26121297957)
completed successfully with 0 failed jobs.
- [x] Linux Playwright matrices are green, including `test dev`, `test
prod`, `test turbopack *`, `test cache components *`, `test node streams
*`, and changed-test lanes.
- [x] Linux non-Playwright jobs remain green on the bare runner,
including `lint`, `types and precompiled`, `rust check`, `test cargo
unit`, and `test unit`.
- [x] `test-next-swc-wasm` is green while opted out of the container via
`usePlaywrightContainer: 'no'`.
- [x] Windows jobs are green and continue to use runtime Playwright
installation.

<!-- NEXT_JS_LLM_PR -->

---------
Co-authored-by: 's avatarCursor <cursoragent@cursor.com>
parent 4023e48d
......@@ -521,6 +521,9 @@ jobs:
with:
skipNativeBuild: 'yes'
skipNativeInstall: 'yes'
# `afterBuild` invokes `rustup target add`, which is not preinstalled
# in the Playwright Docker image; run this job on the bare runner.
usePlaywrightContainer: 'no'
afterBuild: |
rustup target add wasm32-unknown-unknown
node ./scripts/normalize-version-bump.js
......
......@@ -101,6 +101,29 @@ on:
required: false
type: string
default: 'yes'
playwrightDockerImage:
description: >
Docker image to use on Linux runners that need Playwright. Pinned to
the same version as the `playwright` dependency in `package.json`;
the image ships Chromium/Firefox/WebKit and all system deps
preinstalled, so the per-job install step is skipped on Linux.
Use the `-noble` (Ubuntu 24.04) variant so its glibc (>=2.39)
matches the `ubuntu-latest` host runners that build the
`next-swc.linux-x64-gnu.node` native binary; an older base (e.g.
`-jammy`) fails to dlopen the native binary at runtime with
`GLIBC_2.39 not found`.
required: false
type: string
default: 'mcr.microsoft.com/playwright:v1.58.2-noble'
usePlaywrightContainer:
description: >
When 'no', run the job directly on the Linux runner and install
Playwright at runtime instead of wrapping the job in
`playwrightDockerImage`. Use this for jobs that need tooling not
shipped in the container image (e.g. `rustup` for the wasm tests).
required: false
type: string
default: 'yes'
env:
NAPI_CLI_VERSION: 2.18.4
TURBO_VERSION: 2.9.4
......@@ -152,6 +175,36 @@ jobs:
timeout-minutes: ${{ inputs.timeout_minutes }}
runs-on: ${{ fromJson(inputs.runs_on_labels) }}
# Run inside Microsoft's prebuilt Playwright image on Linux when the job
# actually needs browsers, so the per-job "Install Playwright browsers"
# step (download + apt-based `--with-deps`) disappears entirely. When the
# expression evaluates to '', GitHub Actions runs the job directly on the
# runner (Windows jobs, build-only jobs, and any job that opts out via
# `needsPlaywright: 'no'` or `skipInstallBuild: 'yes'`).
container:
image: ${{ (inputs.needsPlaywright != 'no' && inputs.skipInstallBuild != 'yes' && inputs.usePlaywrightContainer != 'no' && !contains(inputs.runs_on_labels, 'windows')) && inputs.playwrightDockerImage || '' }}
# --ipc=host avoids Chromium running out of memory in /dev/shm.
# --init reaps zombie browser processes properly.
options: --ipc=host --init
env:
# The Next.js standalone server template binds to
# `process.env.HOSTNAME || '0.0.0.0'` (see
# `packages/next/src/build/utils.ts`). Docker auto-sets `HOSTNAME`
# to the container ID, which `bash -l` then preserves, so the
# standalone server ends up listening on the container hostname
# (e.g. `3363ea41c9fa`) instead of `0.0.0.0`. That breaks tests
# like `test/production/standalone-mode/**` that fetch
# `http://localhost:<port>` and now get `ECONNREFUSED`. Force the
# standalone default here so the bound address matches what tests
# expect.
HOSTNAME: 0.0.0.0
# Opt out of Next.js's Docker auto-detection so build artifacts use
# the persistent on-disk cache. Without this, `cache-dir.ts`
# short-circuits to ephemeral storage on every build and breaks
# tests asserting determinism between consecutive builds in the
# same workspace (e.g. `test/production/deterministic-build`).
NEXT_IGNORE_IS_DOCKER: '1'
defaults:
run:
shell: bash -leo pipefail {0}
......@@ -179,6 +232,32 @@ jobs:
echo "found=false" >> $GITHUB_OUTPUT
fi
# The Microsoft Playwright image is browser-focused and does not ship
# `jq`/`unzip` (required by the fnm install + `fnm env --json` parse
# below) or `build-essential` (required by `node-gyp` when test
# fixtures install native modules like `node-pty`). Install them
# unconditionally inside the container.
- name: Install container build dependencies (Linux container)
if: ${{ runner.os == 'Linux' && inputs.needsPlaywright != 'no' && inputs.skipInstallBuild != 'yes' && inputs.usePlaywrightContainer != 'no' && !contains(inputs.runs_on_labels, 'windows') }}
run: |
apt-get update
apt-get install -y --no-install-recommends jq unzip build-essential
# GitHub Actions sets `HOME=/github/home` for every step in a
# container job and overrides anything set via `container.env`.
# Playwright's GitHub Actions example runs the container as a
# non-root user, but this reusable workflow needs root so it can
# install jq/unzip/build-essential from the upstream image before
# bootstrapping. Firefox still requires $HOME to be owned by the
# current user, so re-own GitHub's mounted home directory in the
# Playwright container before tests run.
- name: Take ownership of /github/home (Linux container)
if: ${{ runner.os == 'Linux' && inputs.needsPlaywright != 'no' && inputs.skipInstallBuild != 'yes' && inputs.usePlaywrightContainer != 'no' && !contains(inputs.runs_on_labels, 'windows') }}
run: |
if [ "$(id -u)" = "0" ] && [ -d /github/home ]; then
chown -R root:root /github/home
fi
- name: Install fnm
if: steps.check-fnm.outputs.found != 'true'
run: |
......@@ -235,9 +314,19 @@ jobs:
with:
fetch-depth: 25
# Cache pnpm store on GitHub-hosted runners (self-hosted runners have their own persistent storage)
- name: Mark workspace as safe for Git (Linux container)
if: ${{ runner.os == 'Linux' }}
run: |
if [ "$(id -u)" = "0" ]; then
git config --global --add safe.directory "$GITHUB_WORKSPACE"
fi
# Cache pnpm store on GitHub-hosted runners (self-hosted runners have
# their own persistent storage). Also fire inside the Playwright
# container since the container filesystem is always ephemeral, even
# when the host runner is persistent.
- name: Get pnpm store directory
if: ${{ runner.environment == 'github-hosted' }}
if: ${{ runner.environment == 'github-hosted' || (inputs.needsPlaywright != 'no' && inputs.skipInstallBuild != 'yes' && inputs.usePlaywrightContainer != 'no' && !contains(inputs.runs_on_labels, 'windows')) }}
id: get-store-path
run: echo STORE_PATH=$(pnpm store path) >> $GITHUB_OUTPUT
......@@ -310,7 +399,7 @@ jobs:
# risk caching an incomplete store
# If keep conditions in sync breaks, we can split into restore and save
# steps where saving runs based on the outcome of the install step
if: ${{ runner.environment == 'github-hosted' && (inputs.skipInstallBuild != 'yes' || inputs.needsNextest == 'yes') }}
if: ${{ (runner.environment == 'github-hosted' || (inputs.needsPlaywright != 'no' && inputs.skipInstallBuild != 'yes' && inputs.usePlaywrightContainer != 'no' && !contains(inputs.runs_on_labels, 'windows'))) && (inputs.skipInstallBuild != 'yes' || inputs.needsNextest == 'yes') }}
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
timeout-minutes: 5
id: cache-pnpm-store
......@@ -331,28 +420,8 @@ jobs:
if: ${{ inputs.skipInstallBuild != 'yes' }}
- name: Install Playwright browsers
if: ${{ inputs.skipInstallBuild != 'yes' && inputs.needsPlaywright != 'no' }}
run: |
PW_VERSION=$(pnpm playwright --version 2>/dev/null || echo "unknown")
CACHE_DIR="$HOME/.cache"
VERSION_FILE="$CACHE_DIR/playwright-version"
STAMP="$CACHE_DIR/playwright-deps-$(echo "${{ inputs.browser }}" | tr ' ' '-')"
# Clear stamp files when Playwright version changes
if [ -f "$VERSION_FILE" ] && [ "$(cat "$VERSION_FILE")" != "$PW_VERSION" ]; then
echo "Playwright version changed ($(cat "$VERSION_FILE") -> $PW_VERSION), clearing stamps"
rm -f "$CACHE_DIR"/playwright-deps-*
fi
mkdir -p "$CACHE_DIR"
echo "$PW_VERSION" > "$VERSION_FILE"
if [ -f "$STAMP" ]; then
pnpm playwright install ${{ inputs.browser }}
else
pnpm playwright install --with-deps ${{ inputs.browser }}
touch "$STAMP"
fi
if: ${{ inputs.skipInstallBuild != 'yes' && inputs.needsPlaywright != 'no' && (runner.os == 'Windows' || inputs.usePlaywrightContainer == 'no') }}
run: pnpm playwright install --with-deps ${{ inputs.browser }}
- name: Download pre-built test timings
if: ${{ inputs.testTimingsArtifact != '' }}
......
......@@ -203,7 +203,8 @@ function startServer(dir: string, port: number): Promise<void> {
server.on('error', onError)
server.listen(port, 'localhost', () => {
// Listen on localhost (both IPv4 and IPv6)
server.listen(port, () => {
const address = server.address()
if (address == null) {
reject(new Error('Unable to get server address'))
......
......@@ -2,6 +2,16 @@ import path from 'path'
import isDockerFunction from 'next/dist/compiled/is-docker'
export function getStorageDirectory(distDir: string): string | undefined {
// Allow CI environments (notably the Microsoft Playwright Docker image used
// by our own CI) to opt out of the Docker auto-detection. The detection
// assumes ephemeral storage and short-circuits to `undefined`, which forces
// preview/server-action keys to be regenerated on every build and breaks
// tests asserting determinism between consecutive builds in the same
// workspace.
if (process.env.NEXT_IGNORE_IS_DOCKER === '1') {
return path.join(distDir, 'cache')
}
const isLikelyEphemeral = isDockerFunction()
if (isLikelyEphemeral) {
......
......@@ -13,9 +13,12 @@ describe('nx-handling', () => {
private: true,
packageManager: 'npm@10.9.2',
scripts: {
build: 'rm -rf dist; nx run next-nx-test:build',
dev: 'nx run next-nx-test:dev',
start: 'nx run next-nx-test:serve:production',
// Nx's isolated plugin worker can race in CI and crash before Next.js
// starts, so keep this fixture focused on Next.js integration.
build:
'rm -rf dist; NX_ISOLATE_PLUGINS=false nx run next-nx-test:build',
dev: 'NX_ISOLATE_PLUGINS=false nx run next-nx-test:dev',
start: 'NX_ISOLATE_PLUGINS=false nx run next-nx-test:serve:production',
},
dependencies: {
react: '19.0.0',
......
......@@ -3,9 +3,9 @@
"version": "0.0.0",
"license": "MIT",
"scripts": {
"build": "nx run next-nx-test:build",
"dev": "nx run next-nx-test:dev",
"start": "nx run next-nx-test:serve:production",
"build": "NX_ISOLATE_PLUGINS=false nx run next-nx-test:build",
"dev": "NX_ISOLATE_PLUGINS=false nx run next-nx-test:dev",
"start": "NX_ISOLATE_PLUGINS=false nx run next-nx-test:serve:production",
"repro": "rm -rf dist && npm run build && npm run start"
},
"private": true,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment