CI: sharding, Docker and fast reruns
A run is split across machines with the --shard=x/y flag, and the shards' blob reports are merged into one HTML report with merge-reports. The shard matrix uses fail-fast: false, unique artifact names and a report-merging job under the !cancelled() condition. The official Docker image of the same version as @playwright/test evens out fonts and libraries. --only-changed gives quick feedback in a PR, but the full run before merge stays.
Playwright · Config and CI
A run takes 40 minutes on one machine. How do you split it without losing the report?
The suite grows, and a single CI machine already holds the tests for forty minutes. Adding workers does not help: the runner has two cores. There are two requirements: make the run faster and keep one clear report instead of four scattered ones. Sharding splits tests between machines, the blob reporter lets you assemble the report back, and the official Docker image evens out the environment. This lesson breaks the pipeline into parts and shows what breaks in it most often.
How it works and why. The --shard=x/y flag splits the list of tests into y parts, and each machine runs its own, the x-th. The machines do not talk to each other: each takes its part of the list by the same deterministic rule, so shards do not overlap. Each shard sees only part of the tests, so its HTML report is incomplete. That is what the blob reporter is for: a shard stores its result in an intermediate form, and a separate job collects all the blobs and builds a single report. Without fullyParallel the split goes by files, so uneven files cause a skew; with it, shards take individual tests and balance more evenly.
Shards, blobs and one merge job
Example: from one job to a matrix. Before: a single npx playwright test step taking 40 minutes. After: a matrix of four shards in the official image.
jobs:
e2e:
strategy:
fail-fast: false
matrix:
shardIndex: [1, 2, 3, 4]
shardTotal: [4]
runs-on: ubuntu-latest
# image version = the @playwright/test version from package.json
container:
image: mcr.microsoft.com/playwright:v1.65.0-noble
options: --user 1001
steps:
- uses: actions/checkout@v6
- run: npm ci
- run: npx playwright test --shard=${{ matrix.shardIndex }}/${{ matrix.shardTotal }}
env:
CI: true
- uses: actions/upload-artifact@v5
if: ${{ !cancelled() }}
with:
name: blob-report-${{ matrix.shardIndex }}
path: blob-report
retention-days: 1
The blob reporter is turned on in the config through process.env.CI, not with a flag in every step. A separate job collects the artifacts:
merge-reports:
if: ${{ !cancelled() }}
needs: [e2e]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- run: npm ci
- uses: actions/download-artifact@v5
with:
path: all-blob-reports
pattern: blob-report-*
merge-multiple: true
- run: npx playwright merge-reports --reporter html ./all-blob-reports
- uses: actions/upload-artifact@v5
with:
name: html-report
path: playwright-report
// playwright.config.ts
export default defineConfig({
fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? '50%' : undefined,
reporter: process.env.CI ? 'blob' : 'html',
use: { trace: 'on-first-retry' },
});
A few details people trip on. fail-fast: false is needed so that one failed shard does not cancel the rest: otherwise the report is incomplete. !cancelled() (and not success()) in the artifact step and in the merge job gives a report exactly when tests have failed. The image version must match @playwright/test, otherwise the needed browser is not found. The image provides identical fonts and system libraries, which matters for screenshot tests. Without Docker, browsers are installed with npx playwright install --with-deps.
The artifact name of a shard
The same name for all shards
name: blob-report. v4 artifacts are immutable: a second shard trying to upload the same name fails with a conflict, and part of the blobs is lost.
The shard index in the name
blob-report-${{ matrix.shardIndex }}. Each shard has its own name, and the merge takes everything by the blob-report-* pattern.
Fast reruns. --last-failed reruns only the tests that failed in the previous run, and --only-changed runs test files changed relative to HEAD or a given branch, plus those that import changed files (--only-changed=origin/main). On CI, comparing with a branch needs git history: actions/checkout with the default shallow clone may not have it, so fetch-depth: 0 is added. The first is handy locally after a failure, the second gives fast feedback in a PR. They do not replace a full run before the merge: a change in the app can break a test whose file did not change.
The same image locally. To reproduce a CI failure or generate screenshot baselines, the image is run on your own machine too:
docker run --rm --network host --ipc=host -v $(pwd):/work -w /work -it \
mcr.microsoft.com/playwright:v1.65.0-noble /bin/bash
# inside: npm ci && npx playwright test checkout.spec.ts
--ipc=host is recommended for Chromium so that shared memory in the container does not run out. This way a local run uses the same fonts and libraries as the pipeline.
Limits and alternatives. Sharding shortens the run time but does not cure unstable tests and does not reduce the amount of work: the total runner minutes grow. There should be no more shards than is useful: each one has to install dependencies and bring up the environment, so overhead quickly eats the gain. An alternative within Playwright: shrink the set run in a PR with --only-changed or tags. For comparison, in Cypress the split between machines comes from Cypress Cloud: it hands out specs dynamically, by the duration of previous runs, not by a fixed list. In Selenium you build sharding yourself, through Grid and distribution in CI.
Common mistakes.
- The same artifact name in all shards: jobs fail with a conflict, and the report is incomplete.
- An image of a different version than in
package.json: the browser is not found or the render differs from screenshot baselines. - Merging the report without
!cancelled(): when a shard fails there is no report exactly when it is needed. - Default
fail-fast: the first failed shard cancels the rest, and you see only part of the problems. --only-changedas the only run: a change in the app breaks a test whose file did not change, and nobody sees it until the merge.
Check yourself
One of the four shards failed, and the pipeline has no HTML report. What do you check in the job configuration?
Answer
Whether the merge job has if: ${{ !cancelled() }} (by default a job is skipped when a job in its needs failed) and whether fail-fast is off so the other shards finish. Also whether shard artifacts are uploaded under !cancelled() and not only on success.
Five files take 2 minutes each, one takes 20. Without fullyParallel there are four shards. Why does the run still take 20+ minutes?
Answer
Without fullyParallel the unit of the split is a file, and the big file falls entirely into one shard. With fullyParallel: true its tests would be spread across shards.
In a PR tests run with --only-changed=origin/main and fail with an error about a missing branch. What is wrong with the checkout?
Answer
A shallow clone has no history of the main branch, so there is nothing to compare with. You need fetch-depth: 0 in actions/checkout or an explicit fetch of that branch.
--shard and assemble the reports from blobs into one HTML. Middle/Senior: I run tests in the official Docker image of the same version as @playwright/test, split with a matrix using fail-fast: false and unique artifact names, and merge the report through merge-reports under !cancelled(). fullyParallel balances the shards, and --only-changed gives fast feedback in a PR, but a full run stays before the merge.