Sub topic 6
Running one shard per job with a CI matrix
This chapter assumes you can run a test suite from the command line, read YAML, and know the basic CI terms: job, step, and runner. It also assumes your runner has a shard option, as covered in the previous chapter.
The idea
A CI matrix takes one job definition and expands it into several jobs. Each copy gets a different value for a variable. If that variable is the shard index, every job runs a different slice of the suite, and all the jobs run at the same time.
A working workflow
This GitHub Actions file starts four jobs. Each job passes its own index to Playwright's --shard=index/total option.
name: e2e
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
shard: [1, 2, 3, 4]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npx playwright install --with-deps
- run: npx playwright test --shard=${{ matrix.shard }}/4Read it in three parts:
matrix.shard: [1, 2, 3, 4]creates four jobs namedtest (1)totest (4).${{ matrix.shard }}is replaced with that job's value, so job 2 runs--shard=2/4.- Every job repeats checkout, install, and browser download. Each runner is a fresh machine, so nothing carries over between jobs.
The same pattern works for any runner that accepts index/total. Jest 28 and later does:
# Run the second of four shards with Jest
npx jest --shard=2/4Keeping the total in one place
The workflow above writes 4 twice: once in the list and once in the command. If you change one and forget the other, some tests never run. Shards 1 to 3 of 3 plus a stale /4 would skip a quarter of the suite, and CI would still pass.
Define the total once and reuse it:
strategy:
fail-fast: false
matrix:
shard: [1, 2, 3, 4]
env:
TOTAL_SHARDS: 4Then call --shard=${{ matrix.shard }}/${{ env.TOTAL_SHARDS }}. You still maintain the list and the number together, but both sit next to each other. A check step that fails when the list length differs from the total is a further safeguard.
Why `fail-fast: false`
By default, GitHub Actions cancels the other matrix jobs as soon as one fails. For sharded tests this hides information. You see one failing shard and not the others, so you fix one problem, push, and find the next. Setting fail-fast: false lets every shard finish, so one run reports all failures.
The trade-off is cost. If you prefer to save runner minutes on obviously broken commits, leave fail-fast on. The right choice depends on how long a shard takes and how often failures occur.
Common errors
| Symptom | Likely cause |
|---|---|
| A shard has zero tests and exits with an error | More shards than test files, or the runner needs an option to allow empty runs |
| Some tests never run | The total in `--shard` does not match the number of matrix entries |
| One shard takes far longer than the others | Count-based splitting ignores test duration |
| Shards pass alone but fail together in a report merge | Output files from different shards share one name and overwrite each other |
Exercise
Your matrix lists shards [1, 2, 3, 4], but the command says --shard=${{ matrix.shard }}/3. What happens, and how do you prevent it?
Show answer
Shard 4 of 3 is an invalid index, so that job will most likely fail with an error. The other jobs split the suite into thirds, but you lose clarity about what ran. Prevent it by defining the total once, for example in an env variable, and using it in both the matrix and the command.