Keeping your CI pipeline fast and reliable: caching, parallel jobs and flaky tests
A continuous integration (CI) pipeline is one of the tools a team interacts with most every day, yet it is rarely treated as a piece of software in its own right. When it is first set up, it finishes in a few minutes; over time, new tests, new steps and temporary workarounds pile up. One day you notice the pipeline takes twenty minutes, one run in ten turns red for no apparent reason, and developers have grown used to pressing "re-run" instead of waiting for the result.
A slow and unreliable CI does not just waste time; it teaches the team not to trust the signal it gives. In this post I walk through how I keep a CI pipeline fast and reliable: what the pipeline should prove, the order of its stages, dependency caching, parallel jobs, flaky tests and the security of the pipeline itself. The examples use GitHub Actions and .NET, but the principles carry over to other tools.
The pipeline's contract: what does green mean?
Before optimizing, you need to be clear about what the pipeline proves. For me, a green run means this: the code builds in a clean environment, dependencies resolve to the versions in the lock file, the tests pass, and the resulting artifact is ready to deploy. Each part of that sentence maps to a step; steps that map to none of it should be questioned.
This contract has two important consequences. First, the main branch should always be releasable; a red main branch becomes the team's priority. Second, the pipeline's result must be deterministic: if two runs of the same commit produce different results, the signal is broken, and that is a more serious problem than slowness.
Measuring feedback time and ordering the stages
The first step in speeding up a pipeline is seeing where the time goes. Tracking the duration of each step for a few weeks often shows that intuition is wrong: you expect tests to be the slow part, while the real time goes to downloading dependencies or to a tool that is installed from scratch on every run.
Ordering matters as much as speed. Cheap checks that fail often should run first: formatting, compilation and unit tests. Integration and end-to-end tests that need a database or a browser come after them. Getting feedback in one minute instead of a red result fifteen minutes later because of a typo lets the developer fix it without losing context.
Cancelling older runs when there are consecutive pushes to the same branch is another easy win. Nobody waits for the result of a commit that is no longer current, and there is no reason to keep runners busy with it.
Dependency caching: tie the key to the lock file
Downloading every package again on every run is, in most pipelines, the easiest time to win back. The right cache key is a hash of the file that precisely describes the dependencies: packages.lock.json in .NET, package-lock.json on the Node side. As long as the lock file does not change, the cache is used; when it changes, the cache is rebuilt.
name: ci
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
global-json-file: global.json
cache: true
cache-dependency-path: "**/packages.lock.json"
- run: dotnet restore --locked-mode
- run: dotnet build --no-restore -c Release
- run: dotnet test --no-build -c Release --filter "Category!=Integration"
--locked-mode makes the restore fail if the resolved dependencies do not match the lock file, so no silent difference can creep in between the cache and the actual dependencies. On the Node side, npm ci plays the same role instead of npm install. Pinning the SDK version with global.json also ensures the same compiler is used locally and in CI.
Be careful about what you put in the cache. The package store and build tools are good candidates; build outputs, on the other hand, can lead to stale code being tested because of a wrong cache key. A cache should only make things faster; it must never change the result.
Parallel jobs: run independent work at the same time
A single long job makes independent steps wait for one another. Unit tests, integration tests, the frontend build and static analysis often do not need each other; as separate jobs they can run at the same time. Where there is a real dependency between jobs, it should be expressed explicitly, for example by starting the deploy job only after the tests have passed.
For large test suites, splitting the tests into several shards is also possible. Watch the balance here: every new job starts its own runner, checks out the code and restores dependencies. If that fixed cost is larger than the time of the tests being split, parallelism slows the pipeline down. Do not split without measuring.
Matrix builds, that is, running the same job for several operating systems or runtimes, are valuable for libraries. For an application deployed to a single target, running a wide matrix on every pull request is usually an unnecessary cost; a separate nightly job can take that on.
Flaky tests: own them instead of retrying them
A flaky test is one that sometimes passes and sometimes fails without any code change. In isolation it looks like a small problem; but once a few accumulate in a pipeline, a red result loses its meaning, and real failures get dismissed with "it's that test again".
The causes of flakiness are usually familiar: fixed-duration sleeps, data shared between tests, order dependence, the real clock and time zones, calls to external services. Each of these has a known fix: waiting on a condition, giving every test its own data, a controllable time source and test doubles for external dependencies.
Automatically retrying the whole pipeline is tempting but dangerous; it does not fix the problem, it only hides it. The approach I prefer is this: a test identified as flaky is moved into a separate category with a record and an owner. It no longer blocks the main pipeline, but it keeps running and being reported. Quarantine is a parking spot, not a graveyard; a test that is not fixed within a set period is either fixed or deleted after rethinking what it actually verifies.
Build once, deploy the same artifact
The pipeline's reliability does not end at the test stage. The code that passed the tests and the code that goes to production need to be the same. Rebuilding for each environment opens the door for a different dependency version or a different build setting to slip in.
Instead, the build happens once, the resulting artifact (a package or a container image) is tagged with the commit hash, and the same artifact is promoted to the test, staging and production environments. The only difference between environments is configuration. This approach also makes rollbacks simple: going back to the previous version means redeploying the previous artifact.
Securing the pipeline itself
A CI pipeline is a system with access to the source code, package registries and often the production environment, which makes it attractive to attackers. A few basic measures reduce the risk considerably.
Give the workflow read-only permissions by default and grant write permissions only to the job that needs them; the permissions: contents: read line in the example above does exactly that. Secrets should not be available to pull requests coming from outside; run deploy and release jobs only on the main branch and in protected environments. Pinning third-party actions to a commit hash instead of a tag prevents a change in that action's repository from running unnoticed in your pipeline. Receiving dependency updates through automated tools as small, regular pull requests reduces both security vulnerabilities and the pain of large version jumps.
Finally, the pipeline definition should be reviewed like code. A change to a workflow file can be as consequential as a change to the application code.
A fast and reliable CI pipeline is not something you set up once and leave; it is a product that needs maintenance. Measuring durations, moving cheap checks to the front, tying the cache to the lock file, running independent jobs in parallel, owning flaky tests and promoting the same artifact to every environment: none of these is a big task on its own. Applied together, they let the team trust a green result again, and CI stops being an obstacle you wait on and becomes the tool for shipping changes with confidence.