Sub topic 4
Finding the critical path through your jobs
This chapter assumes you can read a workflow file and know that needs orders jobs. It also assumes you have opened a run in the Actions UI and seen the per-job durations from the earlier chapters.
Why total time is not the sum of job times
Jobs with no needs start at the same time, as long as runners are available. A job with needs waits until every job it lists has finished successfully. The run therefore finishes when its longest chain of dependent jobs finishes. That chain is the critical path.
Speeding up a job that is not on the critical path does not shorten the run. Finding the critical path tells you which job to optimize first.
A worked example
This workflow runs lint and four test shards in parallel, then deploys.
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 needs keyword on deploy creates the graph below.
Suppose the job durations are the ones in the diagram labels:
| Chain | Calculation | Length |
|---|---|---|
| lint, then deploy | 1 + 2 | 3 min |
| shard 2, then deploy | 5 + 2 | 7 min |
| shard 3, then deploy | 7 + 2 | 9 min |
The critical path is shard 3 followed by deploy, about 9 minutes. Making lint twice as fast saves nothing. Making shard 3 two minutes faster saves two minutes, until shard 1 or 4 (6 minutes each) becomes the slowest.
A matrix counts as one needs target. A job that depends on test waits for every shard, so the slowest shard sets the pace.
Measuring the path from real data
Do not rely on estimated durations. Read the real ones from the run's job list in the UI, or print them with the command below.
gh run view 1234567890 --json jobs --jq '.jobs[] | {name, seconds: ((.completedAt | fromdateiso8601) - (.startedAt | fromdateiso8601))}'Then work through these steps:
- List each job and its
needs. - Add up execution times along each chain, from a job with no
needsto the last job. - Add each job's queue time to its chain, because waiting for a runner delays everything after it.
- Take the chain with the largest total as the critical path.
The critical path can change from run to run. A cache miss, a slow runner or a long queue can move it to a different job. Check several runs before you commit to a fix.
Shortening the path
Once you know which jobs are on the path, you have three main options.
- Speed up a job on the path. Use caching, a faster runner or finer test sharding.
- Remove an unneeded dependency. If
testhasneeds: lintonly out of habit, the two jobs run one after the other and the path becomes lint plus the slowest shard. Remove theneedsline and they run side by side. - Move work off the path. A slow job that nothing depends on, such as a report upload, can run without delaying the deploy.
Each parallel job repeats setup work such as checkout and npm ci. Splitting a 10-minute job into five 2-minute jobs may give a path of 4 minutes or more, not 2. The setup cost is paid in every job, and the sharded jobs also need runners at the same time.
Common mistakes
- Optimizing the longest single job without checking whether it is on the critical path.
- Adding
needsto enforce order when the jobs do not share outputs or artifacts. - Treating
fail-fast: falseas a speed setting. It keeps the other shards running after one fails. It does not change the path when all shards pass.
Exercise
A workflow has jobs build (4 min), unit (3 min, needs build), e2e (8 min, needs build) and docs (6 min, no needs). Ignoring queue time, what is the critical path, and does shortening docs by 3 minutes help?
Show answer
The critical path is build then e2e, 4 + 8 = 12 minutes. The chain build then unit is 7 minutes and docs is 6 minutes. Shortening docs by 3 minutes does not help, because docs is already shorter than the critical path. Speeding up e2e or build does help.