The more expensive a suite is in time, resources and instability, the less often and the later it runs. A PR gets fast, stable checks (lint, unit, API, e2e on critical paths) as required checks, a merge into main gets a wider suite and a smoke run after the deploy to staging, and the nightly run gets the full regression and quarantined tests. Cheap stages run first (fail-fast).
CI/CD · Pipeline
Full e2e on every commit: a 40-minute wait. What should really block a PR?
A team turns on the full e2e suite for every PR. At first that feels safe. A month later a PR waits 40 minutes, developers start the next task and stop looking at red statuses. Automated tests nobody reads protect nothing. This lesson is about how to spread suites across a PR, a merge and a nightly run.
How it works and why. The main rule: the more expensive a suite is (time, resources, instability), the less often and the later it runs. Every level answers its own question and has its own time budget.
Three run levels
Figure 1. Only the first level blocks a merge, the rest signal. The time budgets are rough, tune them per team.
Pull request: lint and typecheck, unit, API and contract tests, the build, e2e only on critical paths (login, the main product action). These are required checks: no merge without a green status.
Merge to main: a wider suite and a deploy to staging with a smoke run after it. A failure here means the PR suite missed something and should be extended.
Nightly on a schedule: the full regression, all browsers, visual and performance checks, a run of quarantined tests. The result is a report and a notification, not a merge block.
Manual run (workflow_dispatch): repeat a long suite on any branch or environment.
Stages in a run follow fail-fast: cheap checks (lint, unit) go first, slow e2e after them through needs. A failure that lint could catch in a minute should not wait for a browser. Independent jobs (for example API tests and the build) run in parallel.
Example: one file for three levels. Triggers decide what runs, and an if on a job keeps the expensive suite away from PRs:
smoke is filtered by an @smoke tag in the test title (--grep @smoke), so the "critical path" is a team agreement, not separate code. What blocks the merge: on a PR only fast and smoke are required, and full is skipped on a PR (status skipped). Playwright's own CI config, sharding and Docker are in the lesson “CI: sharding, Docker and fast reruns”.
A quality gate is the set of conditions code must meet to move on: all required checks green, no new critical defects, coverage of new code above a threshold, no flaky tests in the blocking suite. The fewer conditions rest on someone's word, the better: every condition is checked automatically. Required checks are set in branch protection or rulesets by job name.
What blocks a PR
Wrong
Everything on every PR
Full e2e, all browsers and visual tests. Time grows, a flaky test blocks other people's changes, people stop looking at statuses.
Right
A fast core on PR, the rest later
On a PR only what catches a critical-path break within minutes. The full regression on main and nightly, with a report.
Figure 2. Only a fast and stable suite deserves to block a PR.
Limits and alternatives. If a workflow is skipped because of a path or branch filter, its required check stays Pending and the merge is blocked. A job skipped through if counts as successful. So either filter at job level, or add a summary job and make it the only required one. For a small project three levels are overkill: one run per PR plus a nightly is enough.
Typical mistakes.
A full regression on every commit: feedback is slow and developers work around it.
A nightly suite with no owner: it stays red for weeks and nobody reacts.
A required check whose name no longer exists after a job rename: the PR hangs in Pending.
Gate conditions rest on someone's word instead of automatic checks.
A deploy to staging with no smoke run after it: a broken environment is found later.
Check yourself
Why did a PR stop being mergeable after a job was renamed in the workflow, although every run is green?
Answer
A required check in branch protection is bound to the job name. Nobody reports the old name any more, so the status stays pending. The fix: update the required checks to the new name, or use a summary job with a stable name.
What should stay in the PR suite and what should move to nightly?
Answer
Keep the fast and stable on the PR: lint, unit, API, the build and e2e of critical paths. Move the slow and wide to nightly: all browsers, visual tests, long flows, performance, quarantine. The criteria are time, stability and risk of breakage.
Related lessons: “Flaky tests in CI: retries, quarantine, re-runs”, “How to keep PR feedback within 10 minutes”, “A workflow skeleton for Playwright in GitHub Actions”.
In an interview.Junior: A PR runs fast checks: lint, unit and a few e2e tests on the main path. The full regression runs after a merge or at night, otherwise developers would wait too long. Middle/Senior: I spread suites by cost and risk: a PR within ~10 minutes with required checks, a wider suite on main with a smoke run after the deploy, nightly with all browsers and quarantine. The order is fail-fast, the quality gate is automatic, and required checks are tied to a stable job name. A failure on main is a signal to extend the PR suite.
Flaky tests in CI: retries, quarantine, re-runs
A flaky test passes and fails on the same code, and the policy has three tools. A retry detects, quarantine isolates, a re-run copes with infrastructure failures. CI allows one or two retries, and a test that passed only on a retry gets a ticket. An unstable test that blocks others goes into quarantine with an owner and a deadline, but is not deleted.
CI/CD · Pipeline
A test failed, we re-ran it, it passed. Was that "all good" or an incident we just hid?
A red PR. The author clicks Re-run, the test passes, merge. A week later the same test fails for another developer, then a third. Nobody knows whether it is a product bug or a test bug, and the team starts treating red as noise. A flaky test costs nothing while nobody notices it, and costs a lot once ignoring it becomes a habit. That is why a policy is needed, not "let's re-run once more".
How it works and why. A flaky test passes or fails on the same code. There are three tools and each has its own job: a retry detects, quarantine isolates, a re-run copes with infrastructure.
Life of a flaky test
Figure 1. A retry does not cure, it turns "sometimes red" into a visible flaky status and a ticket.
Retries allow 1-2 repeat attempts in CI and none locally, so a developer sees instability at once. A test that passed only on a retry is marked flaky by Playwright: it lands in the report and in metrics. Collecting a trace on the first retry is convenient. Config options are covered in the lesson “Parallelism, workers and sharding”.
import { defineConfig } from'@playwright/test';
exportdefaultdefineConfig({
// two retries in CI, none locally
retries: process.env.CI ? 2 : 0,
use: {
// a trace is collected only on the first retry
trace: 'on-first-retry',
},
});
The first attempt failed. This is a real failure: the cause is in it, not in the retry.
The retry passed. It is what turns the job green, so the red disappears from view.
The flaky line. The only place where any sign of it is left. The exit code is zero, and without a report or a metric nobody will see this test.
What to do
File a ticket with an owner and a deadline, count such lines in the flaky rate metric, and open the trace of the first retry to find the cause.
Figure 2. Output of the list reporter (shortened): the test passed only on a retry.
Quarantine. An unstable test that blocks others is moved out of the required suite: a tag or a separate project that the nightly run starts on its own (test.skip and test.fixme do not fit: such a test does not run at all). It is not deleted: it has an owner, a cause and a deadline, and the nightly run shows whether it recovered. Quarantine without a deadline becomes a graveyard of tests.
// a tag in the title: the blocking suite skips ittest('card payment @quarantine', async ({ page }) => {
// TODO QA-412: race after the bank redirect, owner: Ira, due 15 Dec
});
// PR: npx playwright test --grep-invert @quarantine// nightly: npx playwright test --grep @quarantine
Re-run. When infrastructure fails (a runner, the network, a rate limit), GitHub Actions has Re-run failed jobs: only the failed jobs restart, for example one shard, not the whole pipeline. The same from a console: gh run rerun --failed. It saves time but does not replace analysis: a re-run with no entry in metrics hides the problem.
Retries: two approaches
Wrong
retries: 5 for everything
Real race bugs pass on the fifth attempt and turn green. The pipeline gets longer and nobody sees the instability.
Right
1-2 retries + flaky tracking
Every flaky test goes into metrics and a ticket, and one above a threshold goes to quarantine.
Figure 3. A retry is useful only when its result is visible.
Limits and alternatives. Retries do not help when tests depend on shared state: a retry inherits the dirty data. Causes of flakiness are found in the trace and in execution order, not in the number of attempts; a method is in the lesson “CI metrics and failure triage”. Dedicated services (flaky analytics over run history) help in large suites, but to start the Playwright report and tickets are enough.
Typical mistakes.
A global retries: 5: it hides real race bugs.
Automatic re-runs of red runs with no entry in metrics.
A skip with no ticket link, owner or deadline.
A quarantine nobody reads at night.
Retries locally: a developer stops seeing instability.
Check yourself
A test passed only on the second attempt and the job is green. Can we stop there?
Answer
No. A green job only means the final result succeeded. A test that needed a retry is marked flaky and needs a ticket, a cause and an owner, otherwise instability accumulates.
How does quarantine differ from deleting a test?
Answer
In quarantine a test stays in the suite but does not block PRs: it has an owner, a cause and a deadline and runs at night. A deleted test stops protecting the feature and nobody sees the problem.
Related lessons: “What to run on a PR, on merge and nightly”, “How to keep PR feedback within 10 minutes”, “CI metrics and failure triage”.
In an interview.Junior: A flaky test passes or fails on the same code. In CI I allow one or two retries, and a test that passed on a retry is not ignored: I file a ticket. Middle/Senior: For me a retry is a sensor, not a cure: flaky tests go into metrics and a trace is collected on the first retry. An unstable test that blocks others goes to quarantine with an owner and a deadline and runs at night. I use re-run failed jobs for infrastructure failures, not instead of analysis, and watch the share of flaky tests as a health metric of the suite.
How to keep PR feedback within 10 minutes
PR feedback is kept within 10 minutes by shortening the critical path: the longest chain of dependent jobs. First measure the breakdown by step, p50 and p90 and queue time, since often install or build is slow, not the tests. Then apply parallel jobs, caching, e2e sharding and selective runs, keeping a fallback full run on main and nightly for the latter.
CI/CD · Pipeline
A PR hangs for 25 minutes. Where does the time go: tests, install or the runner queue?
After ~10 minutes a developer switches to another task, and coming back to a red PR costs context. Often a pipeline is slow not because of tests: 6 minutes go to install, 4 to the build, 3 to the queue. Without measurements, optimizing blindly means speeding up what is already fast.
How it works and why. Measure first. You need a breakdown by job and step (checkout, install, build, tests), p50 and p90 duration and runner wait time. A run's duration is set by the critical path: the longest chain of dependent jobs. Only that is worth speeding up, because shortening a parallel branch outside it does not change the run time.
Sequential versus parallel
Figure 1. The numbers are made up: the picture shows the effect of parallel jobs, not a real project.
Levers, from cheap to expensive:
Parallel jobs. lint, unit, the build, API and e2e do not wait for each other if there is no dependency.
Cache. The npm cache, Docker layers, build output; the official docs advise against caching Playwright browsers, installing only the one you need is faster. Details in the lesson “Cache, matrix, concurrency and sharding”.
E2E sharding.--shard=i/N in a matrix; duration ≈ the slowest shard, so evenness matters.
Selective runs. Path filters and affected-package analysis in a monorepo: a PR that changed documentation does not run e2e.
The pyramid. Move checks from e2e to API and component tests; leave on the PR only the e2e of the critical path.
Stability. Retries and re-runs also eat time: a lower flaky share speeds the pipeline up.
Stronger runners. The last option: the bottleneck is usually not CPU.
Example. Skipping documentation by paths:
on:
pull_request:
paths-ignore:
- 'docs/**'
- '**/*.md'# changes in shared code should run everything# paths:# - 'packages/shared/**'
This has the trap from the lesson “What to run on a PR, on merge and nightly”: if a workflow is skipped by a path filter, its required check stays Pending. So a required workflow always runs and checks the changed files itself: a step compares the changes with the base branch, and the expensive job gets an if on its result. A job skipped through if reports success and does not block the merge. Selective runs have one more risk: missing a dependency. A fallback is needed: a rule "a change to a shared library runs everything" and a full run on main and nightly.
How to speed up
Wrong
Blind optimization
Take a stronger runner and add workers. If the time goes to install and the queue, the run barely speeds up.
Right
Measure, then the longest chain
Break time down by step, find the critical path, remove the costliest part: cache, shards, duplicates.
Figure 2. Without a per-step breakdown, optimization has no target.
Limits and alternatives. The 10-minute budget belongs to the PR, not to the whole pipeline: slow things move to main and nightly. Shards shorten e2e but add install overhead to every job, so past a certain number of shards the gain disappears. Run metrics and failure triage are in the lesson “CI metrics and failure triage”.
Typical mistakes.
Optimizing without measurements.
Too many shards: install and browser start eat the gain.
Selective runs without a fallback full run.
Caching node_modules instead of the npm cache.
Ignoring time spent in the runner queue.
Check yourself
A run has lint (2 min), unit (3 min), a build (4 min) and e2e (12 min) in sequence. What should be done first?
Answer
Run the independent stages in parallel: lint, unit and the build do not depend on each other. The longest chain sets the time, and the next step is to shard e2e.
Why can path filters not be relied on completely?
Answer
A filter can miss indirect dependencies: a change to a shared library affects a service whose files did not change. So keep a fallback full run on main and nightly and a rule for shared code.
Related lessons: “What to run on a PR, on merge and nightly”, “Flaky tests in CI: retries, quarantine, re-runs”, “Cache, matrix, concurrency and sharding”.
In an interview.Junior: To speed up a pipeline I run independent stages in parallel, cache dependencies and split e2e into shards. Middle/Senior: I start with measurements: a per-step breakdown, p50 and p90, queue time. Then I shorten the critical path: parallel jobs, cache, sharding, selective runs with a full run on main as insurance. I move some checks from e2e to API tests and keep the flaky level low, because retries cost time too.