A developer pushes a small code change.
The tests pass locally in seconds. The pull request looks ready. But the CI/CD pipeline takes 20 minutes to finish.
The developer switches to another task, loses context, and eventually returns to check the result.
This happens repeatedly throughout the day.
A slow pipeline doesn't just delay deployments. It interrupts development, slows down feedback, and makes small changes more expensive than they should be.
I've worked on improving CI/CD pipelines using GitHub Actions and Docker. In one project, we reduced deployment time from 40 minutes to 6 minutes.
The important lesson wasn't that every pipeline needs more powerful runners or aggressive caching.
The biggest improvements often come from understanding where time is actually being spent.
Here's how I'd approach a slow CI/CD pipeline, what to optimize first, and which shortcuts can create more problems than they solve.
1. Start by Measuring the Pipeline
Before changing your GitHub Actions workflow, identify the slowest stages.
A typical pipeline might include:
Checkout code | Install dependencies | Run linting | Run tests | Build Docker image | Push image | Deploy It's tempting to optimize the test suite because it looks complex.
But what if most of the time is spent downloading dependencies or rebuilding the same Docker layers?
You won't know until you measure.
Consider this hypothetical breakdown:
| Stage | Duration |
|---|---|
| Checkout | 15 seconds |
| Install dependencies | 4 minutes |
| Lint and type checking | 2 minutes |
| Tests | 5 minutes |
| Docker build | 7 minutes |
| Deploy | 2 minutes |
| Total | 20 min 15 sec |
These are example numbers, not benchmark results.
The Docker build and dependency installation account for more than half the total duration.
That's where I'd investigate first.
Also, distinguish between total compute time and wall-clock time.
If three jobs each take five minutes but run in parallel, they may finish in roughly five minutes, not fifteen.
Your optimization target is the critical path: the sequence of dependent steps that determines when the entire workflow finishes.
2. Fix Docker Layer Caching
One of the easiest ways to slow down a Docker build is to invalidate the dependency layer whenever application code changes.
Consider this Dockerfile:
FROM node:22-alpine WORKDIR /app COPY . . RUN npm ci RUN npm run build Every source change can invalidate the COPY . . layer.
Since npm ci comes afterward, dependency installation may run again even when package-lock.json hasn't changed.
A better approach separates dependency files from application code:
FROM node:22-alpine WORKDIR /app COPY package.json package-lock.json ./ RUN npm ci COPY . . RUN npm run build Now Docker can reuse the dependency-installation layer when the dependency files haven't changed.
The build still runs when application code changes, but it doesn't need to reinstall unchanged dependencies just because a TypeScript file was edited.
This is a small structural change with potentially meaningful results.
Docker's official documentation recommends organizing instructions to preserve reusable build layers.
Reference: Docker Build Cache Optimization
Don't forget .dockerignore
A large build context can also slow down builds and cause unnecessary cache invalidation.
For a Node.js project, a .dockerignore file might include:
node_modules .git coverage dist .env .env.* *.log Adjust these rules to your project. For example, don't exclude a file that your build actually requires.
A smaller build context means fewer unnecessary files are sent to the builder and considered by COPY instructions.
It also helps prevent sensitive local files from accidentally entering a Docker build context.
3. Understand the Difference Between Docker Cache and CI Cache
Optimizing a Dockerfile isn't always enough.
GitHub-hosted runners are generally fresh environments. A cache generated during one workflow run may not automatically be available during the next.
This is why a Docker build that runs quickly on your laptop can still be slow in CI.
There are two related but different caching problems:
Dependency caching helps package managers avoid downloading the same dependencies repeatedly.
Docker build caching helps Docker reuse previously completed build steps.
GitHub Actions supports dependency caching, while Docker BuildKit supports external build caches that can be shared across builds.
For example, a Node.js workflow can use dependency caching:
name: CI on: pull_request: push: jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: "22" cache: npm - run: npm ci - run: npm test The cache: npm option caches npm's package download cache, not the entire node_modules directory.
The workflow still runs npm ci to install dependencies according to the lockfile.
For Docker builds, consider an external cache using BuildKit when runners don't share local cache state.
The exact configuration depends on your registry, build tooling, and CI environment.
One warning: never store secrets in CI caches. Restored cache contents should also be treated as untrusted.
Reference: GitHub Actions Dependency Caching
4. Run Independent Checks in Parallel
Many pipelines run everything sequentially:
Lint → Type Check → Unit Tests → Build But linting, type checking, and unit tests often don't depend on each other's results.
They can run concurrently.
GitHub Actions lets you define separate jobs:
name: Checks on: pull_request: jobs: lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - run: npm ci - run: npm run lint typecheck: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - run: npm ci - run: npm run typecheck test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - run: npm ci - run: npm test This assumes the repository provides the corresponding npm scripts and a committed lockfile.
The jobs can execute independently when runner capacity is available.
However, parallelism has a cost.
Each job needs its own environment and may repeat setup work.
If dependency installation takes longer than the checks themselves, splitting everything into separate jobs might provide little benefit.
Parallel execution can also increase total runner minutes.
I'd compare wall-clock duration, total compute usage, and maintenance complexity before deciding whether to split jobs.
5. Stop Rebuilding the Same Artifact
Another common inefficiency is rebuilding the application separately for testing, staging, and production.
For example:
Build for tests | Build for staging | Build again for production If the build output is identical across environments, rebuilding introduces unnecessary work.
It can also create subtle inconsistencies when dependency resolution or build-time configuration differs between runs.
A better pattern is to build once and promote the same immutable artifact:
Build + Test | Create Docker Image | Push Image | Deploy to Staging | Verify | Promote Same Image to Production For container deployments, using an image digest makes the deployed artifact explicit.
The production environment can reference the exact image that passed earlier checks.
This works best when environment-specific configuration is supplied at runtime.
If the application embeds environment-specific values during compilation, you'll need to adjust that design before reusing the same artifact.
The main benefit isn't only speed.
It reduces the chance that staging and production are running different builds.
6. Don't Optimize by Removing Important Tests
When a pipeline is slow, skipping tests can seem like an easy solution.
But reducing pipeline duration by removing useful validation is not necessarily an improvement.
A better approach is to understand which tests provide which feedback.
For example:
| Test type | Execution strategy |
|---|---|
| Linting and static checks | Every pull request |
| Unit tests | Every pull request |
| Integration tests | Before merge or deployment, based on risk |
| End-to-end tests | Critical flows before release; broader suites on a schedule |
| Performance tests | Scheduled or before relevant releases |
This is an example strategy, not a universal recommendation.
High-risk systems may need integration or end-to-end validation on every change.
The important part is to maintain an explicit quality requirement rather than removing tests just to make the pipeline look faster.
If tests are slow, investigate fixture setup, redundant database initialization, external network calls, and opportunities for safe parallel execution.
A fast pipeline that regularly allows broken releases creates more work than it saves.
7. Measure the Business Impact, Not Just Build Time
Reducing pipeline duration is useful, but it's not the final goal.
The real goal is to help engineers deliver reliable changes with less friction.
After optimization, I'd track:
- Pipeline wall-clock duration: How long does feedback take?
- Time to first useful failure: How quickly can a developer identify a problem?
- Deployment frequency: Can the team release changes more easily?
- Failure and flaky-test rates: Is the pipeline still trustworthy?
- Runner usage and cost: Did speed improvements increase infrastructure spending?
- Change failure rate: Are faster deployments maintaining release quality?
Look at p50 and p95 pipeline duration rather than relying only on averages.
A pipeline that usually takes five minutes but occasionally takes thirty can still interrupt development.
Also, don't assume that cutting build time automatically increases deployment frequency.
Approval processes, release policies, and operational readiness may still determine when changes reach production.
Pipeline optimization removes one potential bottleneck. It doesn't solve every delivery problem.
8. A Practical Optimization Order
When I encounter a slow pipeline, I prefer making incremental changes instead of redesigning the whole workflow.
My order of investigation is usually:
- Measure each stage. Find the critical path and repeated expensive work.
- Fix unnecessary rebuilds. Check Dockerfile ordering, build contexts, and artifact reuse.
- Improve caching. Cache stable dependencies and build layers where the savings justify it.
- Parallelize independent tasks. Compare speed improvements against runner cost.
- Improve slow tests. Keep important coverage while eliminating unnecessary setup.
- Re-measure. Verify that the improvement holds across typical and cold-cache runs.
This approach makes it easier to determine which change actually helped.
It also avoids a common problem with large pipeline rewrites: several things change at once, and nobody knows which change caused the improvement or a new failure.
Final Thoughts
A slow CI/CD pipeline is often a collection of small inefficiencies rather than one major technical problem.
The fix isn't always more hardware or a new CI platform.
Three lessons stand out:
- Measure the critical path first. Optimizing a stage that doesn't delay completion may not improve feedback time.
- Avoid doing the same work repeatedly. Effective caching, dependency management, and artifact reuse can eliminate unnecessary effort.
- Keep reliability part of the goal. Faster pipelines are valuable only when developers can trust their results.
For engineering teams, shorter feedback loops mean less waiting, faster iteration, and fewer interruptions during development.
That's the real reason pipeline performance is worth improving.
What's the slowest part of your CI/CD pipeline right now: dependency installation, tests, Docker builds, or deployment?`