Skip to content
New Atlanta
Topics

GitLab CI cache vs artifacts for build handoffs

Use a GitLab cache for dependencies a clean job can recover. Publish an artifact when a downstream job must receive exactly what an earlier job built.

Published 8 min read
Cache selection
Runner cache key
Artifact selection
Producing job
Cache miss
Rebuild from declared inputs
Expired job output
Recover or rerun the producer

Decision guide

Cache
Choose a cache for downloaded packages and other regenerable inputs when a miss only increases build time.
Job artifact
Choose a job artifact for a binary, report or manifest that another job or reviewer must retrieve from a particular build.
Deciding factor
Remove the saved directory mentally. If a clean job can regenerate it without changing what you intend to release, caching is appropriate. If its absence breaks the release handoff, name the producer and publish its output.

Comparison matrix

How a consumer finds files and what happens if they disappear Verified
Decision factorCacheJob artifact
Source of files Runner cache key Producing job
Recovery after loss Rebuild from declared inputs Recover or rerun the producer

Why does a green build leave the next job without its binary?

Publish the compiled package under the producing job's artifacts and have the release job fetch it. Reserve the cache for files that speed up a clean build.

Consider a project that builds a command-line client and runs smoke tests on a different runner. The build job writes a file under dist. Its green status says the script exited successfully. The smoke test starts with its own work directory. It needs a transport for the client. If dist was only cached, a different runner, a changed key or an empty cache can leave the smoke test with nothing. Worse, a reused key might supply a client built by another pipeline. A smoke test on that file gives a convincing result for the wrong commit.

Give the producer a stable job name and declare its output path relative to the project directory. Have it write a checksum alongside the package. The consumer downloads that job's artifacts, verifies the checksum and then runs the packaged client. The checksum confirms that the downloaded file matches the digest generated alongside it. Associate those files with the producing job and commit as well, so you know which build you tested.

How should a dependency cache behave on an empty runner?

The installation must succeed when the cache has never existed. Treat a cache hit as a shortcut for fetching packages whose identities already come from the lockfile.

For a Node project using npm, direct the package download cache into the checked-out project, for example .npm/. A clean install can then use those downloads and fall back to the registry when they are missing. Cache that directory and use the lockfile as an input to the key. This arrangement also avoids relying on a populated node_modules directory that the clean installation may remove. GitLab documents project-relative cache paths and provides a package-manager example for this setup.

Dependencies that include compiled outputs introduce another boundary. The same lockfile can be installed on different architectures, operating systems or runtime versions. Include those distinctions in a key when they change the bytes you intend to reuse. Avoid an indiscriminate cache of the entire working directory. A large archive costs time to upload at the end of one job and restore at the start of the next. Measure that cost against fetching only the packages the build actually needs.

Why do two jobs with matching cache keys behave differently?

Inspect where the runners store the archive as well as how the key is constructed. Matching key text cannot make a local disk available to another host.

A runner using local cache storage may see files from an earlier run on that machine. Another runner in an autoscaled fleet may start empty. A shared or distributed cache changes this behavior, subject to its own availability and lifecycle settings. Look at the executor, runner tags and restore log, then check branch protection and any suffixes added to the effective cache key. By default, GitLab separates caches for protected and unprotected branches. Record what the job actually requested when comparing a feature branch with the default branch.

Parallel jobs that write different content under one shared key can make results depend on which writer finishes last. Choose a producer responsible for updating a shared dependency archive and let jobs that only need to read it use a pull policy where appropriate. Keep a separate key for incompatible inputs. This saves upload work and makes a failed restore easier to reproduce. Leave any required output in an artifact even when all jobs happen to run on the same host today.

How do stages and needs change artifact downloads?

Select the producer explicitly when using a dependency graph. GitLab normally fetches earlier-stage artifacts into later jobs, while a job using needs can download artifacts only from its listed jobs.

For the client build, configure artifacts.paths to include the package and its checksum. For the smoke job, list build-client under needs and set artifacts to true on that entry. The smoke job may now start as soon as its producer completes. Other parallel jobs can continue running, and the smoke job does not have to download unrelated outputs. If a promotion requires the smoke result as well, make that dependency explicit in the promotion job.

A stage-based job can instead use dependencies to narrow which earlier-stage jobs supply artifacts. GitLab advises against combining dependencies and needs on the same job. Choose the scheduling model and use the corresponding mechanism. When diagnosing a missing report, first check the producer log for the actual upload and the paths matched. Next inspect the consumer graph and download log. Finally, verify the working directory the script examines. Changing retention cannot repair a misspelled producer path, and increasing runner memory cannot make an unlisted dependency transfer files.

Will the package still exist when approval arrives?

Set artifact retention from the latest time a reviewer or release job needs the exact build. Check the project's keep-latest behavior and instance defaults against that window.

A package built on Friday may wait through the weekend for a manual promotion. A short expiration can turn a successful test into a failed Monday release. GitLab allows an explicit expire_in on artifacts. It also documents special handling of artifacts for the most recent successful pipeline on a ref. Check the project's settings before estimating storage or relying on a particular cleanup date. Retries on older pipelines and access to old tag builds deserve their own test.

When an artifact really is gone, rerunning the producer creates a new output. It may have a different checksum if the toolchain, dependency source or build timestamp changed. Run the acceptance checks on those new bytes before promoting them. For releases that consumers must retrieve for a long time, consider a separately versioned package registry with an explicit retention policy. A pipeline artifact can still provide the evidence of what the job emitted while the registry provides the release distribution path.

What does a useful trial record?

Run the build once with an empty cache, once after a successful save and once on another eligible runner. Each test must consume the package from its own producer.

Record the runner version, executor, image, CPU and memory allocation, concurrent jobs and measurement period. Compare restore time, dependency install time and total elapsed time. Record the downloaded artifact size separately. When a warm run becomes slower than a cold one, a long extract or upload step is a good place to look. When the smoke job passes on the first host but fails on the second, match the digest and examine how the consumer obtained its package. This sequence distinguishes a speed regression from a broken release handoff.

Frequently asked questions

Can a job use both mechanisms?

Yes. A producer can reuse downloaded dependencies through its cache and publish the compiled result as an artifact. Keep their paths separate so that one restore cannot mask the other.

What should happen after the runner cache is cleared?

The install should fetch its locked dependencies again and the producer should create a fresh artifact. A required package that disappears permanently when the cache is cleared belongs in a different delivery path.

Methodology and sources

This example follows the GitLab CI YAML reference and runner cache documentation. The two-runner test is a reproducible troubleshooting procedure. No runner speed or storage savings are assumed.

About Nolan Greer
CI/CD, Build Systems & Reliability Editor

Editorial pen name. Covers build performance, release workflows and operational reliability.