Skip to content

Lesson 8 of 9 · 25m

CI Integration That Stays Green: Sharding, Artifacts, and Retries

Configure Playwright in CI with explicit browsers, shards, traces, reports, and retry semantics that expose instability rather than hiding it.

CI is where optimistic local testing meets cold machines, constrained CPUs, shared networks, missing fonts, and time zones. The goal is not merely to run Playwright in CI. It is to produce a trustworthy signal: green means the product passed, red means someone must look, and flakes are measured rather than quietly retried into oblivion.

Make CI boring and reproducible

Install exact dependencies and browser versions. Playwright's container images are useful because browsers and system dependencies are already aligned:

e2e:
  runs-on: ubuntu-latest
  container: mcr.microsoft.com/playwright:v1.49.1-noble
  steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-node@v4
      with: { node-version: 22, cache: npm }
    - run: npm ci && npm run build
    - run: npx playwright test
      env: { BASE_URL: https://staging.example.test }

If you are not using the container, npx playwright install --with-deps chromium installs browser dependencies. Do not casually test every browser on every commit unless the runtime cost buys real release confidence.

Retries are telemetry, not a fix

Playwright can retry failures; retries: process.env.CI ? 2 : 0 is a reasonable starting point.

Retries are useful because they identify flaky tests while preventing transient noise from blocking every merge. But a test that passes on retry is not a pass in spirit. Report it as flaky. If the dashboard hides retried passes, the team will slowly train itself to accept instability.

A practical policy treats first-try passes as healthy, retried passes as flaky, and final failures as failing. More than two retries is usually self-deception.

Shard by time and ownership, not blindly

When the suite grows, split it across CI jobs: one job runs npx playwright test --shard=1/4 and another runs --shard=2/4.

Sharding distributes tests but does not fix a bad bottleneck. If every shard spends five minutes booting the same backend, you pay setup four times. Build once, reuse artifacts, seed once per isolated environment where safe, and measure the critical path. Playwright's blob reports can merge shard output with npx playwright merge-reports --reporter=html ./blob-reports.

Artifacts are the interface to debugging

Configure trace, screenshots, and videos so failures explain themselves without rerunning the world:

export default defineConfig({
  use: {
    trace: 'retain-on-failure',
    screenshot: 'only-on-failure',
    video: 'retain-on-failure',
  },
});

Trace is the most valuable artifact because it captures actions, network, console output, snapshots, and timing. Screenshots are immediate but shallow. Video helps with animation and timing but is large. Upload artifacts even when jobs fail: upload playwright-report with actions/upload-artifact@v4 and if: ${{ !cancelled() }}.

Gate the merge on the right thing

End-to-end tests belong after unit and integration checks that can fail faster. A typical ordering is typecheck and unit tests, application build, deploy to ephemeral/staging environment, then browser tests. If E2E runs against a shared staging environment, deployment locking and data isolation become part of CI design. The test suite is not the only concurrent system; your pipelines are concurrent too.

Field notes

  • Browser version drift between local and CI causes baffling rendering and behavior differences. Pin both.
  • CI workers are smaller than developer laptops. Parallelism that is stable locally may overload a two-core runner.
  • Secrets in CI need least privilege. E2E users should not be production administrators.
  • Never print auth tokens, session cookies, magic links, or test customer payment details in logs.
  • A green suite with high retry consumption is operationally red. Track flake rate as a metric.

Was this lesson helpful?

Sign in to rate