Sub topic 7
Splitting work across parallel jobs
This chapter assumes you can read a workflow file, know what needs does, and have already found your critical path from the earlier chapters. Here you shorten that path by running independent work at the same time.
Why parallelism helps
Jobs without a needs relationship start together, each on its own runner. The total duration of a run is the length of the critical path. That is the longest chain of dependent jobs, plus the time each job waits for a runner.
Take a workflow where lint takes 2 minutes and tests take 12, run one after the other in a single job. The run takes about 14 minutes. If lint and tests are separate jobs, the run takes about 12 minutes. If you also split the tests into four equal shards, the run takes about 3 minutes plus overhead.
A lint job plus four test shards
The workflow below runs lint and four test shards at once. deploy waits for all of them through needs.
name: ci
on:
pull_request:
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run lint
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
shard: [1, 2, 3, 4]
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npx jest --shard=${{ matrix.shard }}/4
deploy:
needs: [lint, test]
runs-on: ubuntu-latest
steps:
- run: echo "deploying"The matrix creates one job per value in shard, so test becomes four jobs. Each one runs a quarter of the test files. The --shard option needs Jest 28 or later. Other runners have equivalents, such as pytest-split or Playwright's --shard.
fail-fast: false keeps the other shards running when one fails. That way you see every failing shard in one run instead of fixing them one at a time. The default, true, cancels the remaining matrix jobs after the first failure.
The next diagram shows the resulting shape.
What parallelism costs
Every job repeats its own setup: it waits for a runner, checks out the code, and installs dependencies. If setup takes 1 minute and your tests take 12, four shards give you roughly 1 + 3 = 4 minutes per shard. Eight shards give 1 + 1.5 = 2.5 minutes. Going from four to eight shards saves only 1.5 minutes, and it doubles the setup work you pay for.
Two other limits apply:
- Billed minutes go up. Four shards that each run 4 minutes use 16 runner minutes, where the single job used 12.
- Your plan limits how many jobs run at once. If you ask for more jobs than the limit, the extra jobs wait in the queue, and queue time grows on the critical path.
Use the earlier tools to check these costs. Compare each shard's step durations in the Actions UI. If npm ci takes as long as the tests, add dependency caching before adding more shards.
Shards must be balanced
The run is only as fast as its slowest shard. Jest's --shard splits files by count and size, not by measured duration. One slow test file can leave a single shard at 6 minutes while the others finish in 2.
Check the per-job durations after you shard. With gh, this command lists them:
gh run view 1234567890 --json jobs --jq '.jobs[] | {name, seconds: ((.completedAt | fromdateiso8601) - (.startedAt | fromdateiso8601))}'If one shard is much slower than the rest, look at which files it ran. Splitting that one slow file, or using a tool that balances by recorded timings, often helps more than adding shards.
Steps to follow
- Identify the critical path and the longest job on it.
- Move independent work, such as lint and type checks, into separate jobs without
needsbetween them. - Shard the longest job. Start with a small count such as 2 or 4.
- Compare per-job durations across several runs. Check for imbalance and for setup time that now dominates.
Exercise
A test job takes 12 minutes, of which 1 minute is setup. You split it into 4 shards with equal test time. Each shard repeats the 1-minute setup. About how long does each shard take, and how many runner minutes does the run use in total?
Show answer
The 11 minutes of test work divides into 2.75 minutes per shard, so each shard takes about 3.75 minutes. Four shards use about 15 runner minutes in total, compared with 12 for the single job. The run is faster but costs more.
Exercise
After sharding, shard 3 takes 7 minutes and the other three take 3 minutes. What limits the run time, and what should you check first?
Show answer
The slowest shard sets the duration, so the test stage takes 7 minutes. Check which test files shard 3 ran. One slow file is the likely cause, and splitting it or balancing shards by recorded timings is the first fix to try.