From 40 to under 10 minutes: cutting pipeline time with smart caching and parallel tests
A roadmap with numbers: measuring the critical path, dependency and Docker layer caching, remote builds, sharded tests, affected tests, bigger runners, and what to take off the PR path.
CI/CD$ npx jest --shard=1/4▍A 40-minute pipeline doesn’t just cost 40 minutes. It costs the context switch of the developer who went off to do something else, the PR that piled up more changes “while we’re at it,” the hotfix that took an hour to reach production, and the growing temptation to skip steps.
In another article we listed quick wins. Here we go deeper, with a roadmap based on a typical case: a monorepo with a backend, a frontend, and a test suite that grew out of control.
Step 0: measure the critical path
Before optimizing, find out where the time goes. In a pipeline with parallel jobs, what matters is the critical path — the longest sequence of dependent steps.
The pipeline in our example, before any changes:
| Step | Time | Notes |
|---|---|---|
| Checkout + dependency installation | 6 min | No cache, downloads everything on every run |
| Lint + type checking | 4 min | Runs after installation, sequentially |
| Backend and frontend build | 8 min | Recompiles everything, even with no changes |
| Docker image build | 7 min | No layer cache |
| Unit and integration tests | 12 min | A single job, running serially |
| End-to-end tests | 3 min | Flaky, with manual retries |
| Total | 40 min |
Track the duration of each step over time (CI platforms themselves and pipeline observability tools show this). Optimizing without measuring almost always targets the wrong step.
1. Dependency caching (−5 min)
Downloading the same dependencies on every run is the most common waste:
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
Equivalents exist for every language (setup-python, setup-java, setup-go with built-in caching, or actions/cache with a key based on the lockfile hash). The cache key must depend on the lockfile: when dependencies change, the cache changes.
2. Docker layer caching (−5 min)
Use BuildKit with a remote cache and order the Dockerfile from what changes least to what changes most:
- uses: docker/build-push-action@v6
with:
push: true
tags: ghcr.io/your-org/api:${{ github.sha }}
cache-from: type=registry,ref=ghcr.io/your-org/api:buildcache
cache-to: type=registry,ref=ghcr.io/your-org/api:buildcache,mode=max
And take advantage of cache mounts for package managers inside the build:
COPY package.json package-lock.json ./
RUN --mount=type=cache,target=/root/.npm npm ci
COPY . .
RUN npm run build
3. Incremental builds and remote build caching (−5 min)
In monorepos, the biggest savings come from not recompiling what hasn’t changed. Build tools with content-addressed caching — Turborepo, Nx, Gradle Build Cache, Bazel — store the output of each task keyed by the hash of its inputs. If nobody touched the payments package, its build is restored from cache in seconds.
With a shared remote cache, the output of a build in CI is reused by other PRs and even by developers’ machines.
4. Parallelize tests into shards (−8 min)
Twelve minutes of serial tests become three minutes across four parallel jobs:
tests:
needs: build
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: 22
cache: npm
- run: npm ci
- run: npx jest --shard=${{ matrix.shard }}/4
Other tools have the same feature: Playwright (--shard=1/4), pytest-xdist (-n auto, parallel on the same machine), and pytest-split (splitting across machines by execution time). Splitting by historical timing beats splitting by file count — otherwise one shard ends up with all the slow tests.
5. Run only the affected tests (−3 min on average)
In a PR that only changed the frontend, running the backend integration tests is wasted effort. Monorepo tools compute the dependency graph:
npx nx affected -t lint test build --base=origin/main
Run the full suite on the main branch or in a merge queue, and the affected suite on PRs. You gain speed without losing the guarantee before production.
6. Reorganize the job graph
- Lint and type checking in parallel with the build, not before it.
- Build once and reuse: the artifact produced by the build is passed to the test and image jobs (
upload-artifact/download-artifactor the published image itself), instead of every job recompiling. - Fail fast: the cheapest checks that fail most often go first.
- Cancel stale runs with
concurrencyandcancel-in-progress: true.
7. Bigger (or closer) runners
Sometimes the answer is hardware. An 8-core runner may cost more per minute, but if it finishes the build in a third of the time, the cost per run is about the same — and developer time is worth far more. Self-hosted runners (for example, with Actions Runner Controller on Kubernetes) also enable persistent local caches and faster access to internal registries.
8. Deal with flaky tests
Flaky tests are the biggest enemy of speed: the pipeline fails, someone clicks “re-run,” and the 15 minutes start over. A simple policy:
- Automatically detect tests that fail and pass on the same commit.
- Move them to quarantine (they run, but don’t block) with a ticket and an owner.
- Fix or delete them within a set deadline.
The result
| Step | Before | After |
|---|---|---|
| Dependency installation | 6 min | 1 min |
| Lint + types (in parallel) | 4 min | 2 min |
| Build (incremental, cached) | 8 min | 2 min |
| Docker image (cached) | 7 min | 2 min |
| Tests (4 shards, affected only on PRs) | 12 min | 3 min |
| End-to-end (stable, in parallel) | 3 min | 2 min |
| Critical path | 40 min | ≈ 9 min |
The exact numbers vary, but the proportion is common: caching and parallelism account for most of the gain.
What about security along the way?
Going faster can’t mean removing security checks. The answer is to make those checks run in parallel and only on what changed — the subject of our article on silent DevSecOps.
Checklist
- Per-step duration measured and tracked
- Dependency cache keyed on the lockfile
- Docker layer cache and cache mounts
- Remote build cache in the monorepo
- Sharded tests, split by timing
- Affected tests on PRs, full suite on main/merge queue
- A single build reused by downstream jobs
- Quarantine for flaky tests
A fast pipeline isn’t a luxury: it’s what keeps PRs small, deploys frequent, and the team willing to trust the process.