How to Run Only Affected E2E Tests in GitHub Actions
On pull requests, run only the specs affected by the diff; run the full suite on main to keep coverage honest.
Use Playwright --only-changed=<base> on PRs to limit the run to tests touched since the base branch, with a full-suite fallback for pushes to protected branches.
Steps
- Fetch enough history so the base branch is available (
fetch-depth: 0). - On pull_request, run
--only-changed=origin/${{ github.base_ref }}. - On push to main, run the full suite.
Workflow
.github/workflows/ci.yml
jobs:
e2e:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with: { fetch-depth: 0 }
- uses: actions/setup-node@v4
with: { node-version: 20, cache: npm }
- run: npm ci
- run: npx playwright install --with-deps
- name: Affected on PR
if: github.event_name == 'pull_request'
run: npx playwright test --only-changed=origin/${{ github.base_ref }}
- name: Full suite on main
if: github.event_name == 'push'
run: npx playwright testGotchas
--only-changedneeds the base commit fetched, so shallow clones miss it; usefetch-depth: 0.- Always run the full suite on the default branch so nothing rots undetected.
Related guides
How to Shard Playwright Tests Across a Matrix in GitHub ActionsSplit a Playwright suite across parallel GitHub Actions jobs with --shard and a matrix, then merge the blob r…
How to Cache Playwright Browser Binaries in GitHub ActionsCache Playwright browser downloads in GitHub Actions keyed on the Playwright version, skipping the browser in…