
Slow CI is a tax on every pull request. You push, you wait, you switch tabs, and you lose your train of thought.
Slow pipelines usually share the same three problems. They reinstall everything on every run. They run steps one after another that could run side by side. And they keep spending minutes on commits that are already out of date.
This post fixes those in order. I'll use a Node.js project, but the ideas work for any stack. Whether you actually land under three minutes depends on your test suite, so treat it as a target, not a promise.
Versions: the examples use
actions/checkout@v7,actions/setup-node@v7andactions/cache@v6, which were the latest major versions when I checked in November 2026. Check each action's releases page before copying.
Here is where we're headed. Every job starts at the same time, and the whole run takes as long as the slowest one, not the sum of all of them.
flowchart LR
P[Push] --> L[Lint]
P --> T1[Test shard 1]
P --> T2[Test shard 2]
P --> T3[Test shard 3]
P --> B[Build]
L --> D[All checks green]
T1 --> D
T2 --> D
T3 --> D
B --> DStep 0: Find out where the time goes
Open a recent run, click into the job, and look at the time next to each step. Write down the three slowest.
Everything below targets those three. If your install takes 10 seconds, skip the caching section and spend your effort on the tests.
Step 1: Cache your dependencies
This is often the biggest win for the least effort. Without a cache, every run downloads every package again.
The easy way is the cache option on the setup action:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 24
cache: npm
- run: npm ciThe cache key comes from a hash of your lockfile, so it refreshes itself whenever your dependencies change.
Heads up: this caches npm's download cache, not your
node_modulesfolder.npm cistill runs, but it reads packages from disk instead of the network. Python, Java, Go, Ruby and .NET have the same shortcut in their own setup actions.
Caching build output yourself
For things the setup actions don't cover, like a compiler or bundler cache, use actions/cache directly:
- uses: actions/cache@v6
with:
path: .build-cache
key: ${{ runner.os }}-build-${{ hashFiles('package-lock.json') }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-build-${{ hashFiles('package-lock.json') }}-
${{ runner.os }}-build-The key ends in github.sha on purpose. A cache can't be changed once it exists, so every commit saves a fresh one. If the exact key isn't found, GitHub tries each restore-keys prefix in order and restores the most recent cache that matches, so your tool only has to do the difference.
The cache is saved at the end of the job, and only if the job succeeds. Because every commit writes a new entry, keep the cached folder small.
How caches behave
A run can restore caches from its own branch or from the default branch. A cache created by a pull request run can only be restored by re-runs of that same pull request.
Run CI on pushes to
main, so new branches have a warm cache to start from.Entries that haven't been used for 7 days are deleted. The default limit is 10 GB per repository, and the least recently used entries go first.
Never put secrets in a cached folder. Anyone who can open a pull request can read the caches on the base branch.
Step 2: Run independent work in parallel
Jobs in a workflow run in parallel by default. They only wait for each other if you add needs. So the fix is to stop cramming lint, test and build into one job:
jobs:
lint:
runs-on: ubuntu-latest
steps:
# checkout, setup-node, npm ci, npm run lint
test:
runs-on: ubuntu-latest
steps:
# checkout, setup-node, npm ci, npm test
build:
runs-on: ubuntu-latest
steps:
# checkout, setup-node, npm ci, npm run buildUse needs only when order truly matters, for example a deploy job that must wait for tests: needs: [test, build].
Heads up: every job gets its own fresh machine, so it pays for startup, checkout and install again. That's why Step 1 comes first. Don't split steps that take a few seconds, and remember that your plan caps how many jobs can run at the same time.
Step 3: Use a matrix, for coverage or for speed
A matrix turns one job definition into many jobs, one per combination of values. It has two uses.
For coverage, run the same job on several versions:
strategy:
matrix:
node: [22, 24]
steps:
- uses: actions/setup-node@v7
with:
node-version: ${{ matrix.node }}
cache: npmFor speed, split one slow test suite into shards that run side by side:
strategy:
fail-fast: false
matrix:
shard: [1, 2, 3]
steps:
- run: npm test -- --shard=${{ matrix.shard }}/3Jest, Vitest and Playwright all have a --shard flag. Jest and Vitest split by test file, not by individual test, so one giant file won't be divided. For other runners, look for the equivalent option.
A few matrix rules worth knowing:
fail-fastdefaults totrue, so one failing job cancels the rest of the matrix. That saves minutes. Set it tofalsewhen you want to see every failure in a single run.max-parallelcaps how many matrix jobs run at once.A matrix can create at most 256 jobs per workflow run.
A matrix alone doesn't make a slow suite faster. Running the same tests on two versions just means two slow jobs. Sharding is what divides the work.
Step 4: Cancel runs that no longer matter
If you push three commits in a row, only the last run matters. The concurrency setting cancels the older ones:
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: trueOn a pull request, the group is the workflow name plus the branch name, so a new push cancels the run still going for the old commit. github.head_ref only exists on pull request events. On a push to main it's empty, so the group falls back to the unique run ID, and runs on main never cancel each other.
Heads up: don't try to protect
mainwithcancel-in-progress: falseon a shared group. A group holds at most one running and one pending run, so extra pending runs get cancelled anyway.
Step 5: Skip work you don't need
Docs-only changes don't need a full test run. Path filters can skip them:
on:
pull_request:
paths-ignore:
- '**.md'
- 'docs/**'Heads up: don't use path or branch filters on a workflow that is a required check. GitHub's docs warn against it, because a skipped workflow can leave the required check pending and block the merge.
Also give every job a timeout-minutes. The default is 360 minutes (six hours), which is a long time for a hung test to burn through your quota.
The full workflow
Here's everything together. Replace the npm scripts with your own.
name: CI
on:
push:
branches: [main]
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
permissions:
contents: read
jobs:
lint:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 24
cache: npm
- run: npm ci
- run: npm run lint
test:
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
fail-fast: false
matrix:
shard: [1, 2, 3]
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 24
cache: npm
- run: npm ci
- run: npm test -- --shard=${{ matrix.shard }}/3
build:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 24
cache: npm
- run: npm ci
- run: npm run buildTriggering on push to main plus pull_request also means a branch push with an open pull request runs once, not twice.
Cheat sheet
Change | Cuts time on | Watch out for |
|---|---|---|
Dependency cache | Installs | Evicted after 7 days unused; only restores from your branch or the default branch |
Parallel jobs | Total run time | Startup cost per job; your plan's concurrent job limit |
Matrix and sharding | Slow suites, multi-version testing |
|
Concurrency | Runs on outdated commits | Give |
Path filters and timeouts | Needless runs and hung jobs | No path filters on required checks |
Start with the cache. For most projects it's a one-line change and often the biggest win. Then read the step timings again and go after whatever is slowest now.
Comments(0)
Join the discussion by logging into your account.
No comments yet. Be the first to comment!