Cache vs Artifacts
GitLab has two ways to keep files after a job, and mixing them up is one of the most common pipeline bugs.
artifacts |
cache |
|
|---|---|---|
| Holds | results of a job: binaries, reports, dist/ |
inputs reused to save time: downloaded dependencies |
| Flows | to later jobs in the same pipeline (and downloadable from the UI) | to the same job in later pipelines (and other jobs with the same key) |
| Guarantee | always there for the jobs that need them | best effort: a cache may be missing, old, or on another runner |
| Stored | on the GitLab server, with expire_in |
on the runner, or in shared object storage (S3, GCS) if configured |
| Typical paths | dist/, report.xml, coverage/ |
.npm/, .cache/pip, .m2/repository, vendor/ |
The rule of thumb: if a later job would be wrong without the files, they are artifacts. If it would only be slower, they are a cache.
Both in one pipeline
The session runs a two-stage pipeline twice. build downloads "dependencies" into a cached folder and writes its result to dist/ as an artifact; test looks for the artifact, the cache and an undeclared scratch file. --shell-isolation gives each job its own copy of the repository, as separate runners would:
mkdir shop && cd shop && git init -q
git commit -q --allow-empty -m "init"
git remote add origin https://gitlab.example.com/acme/shop.git
git update-ref refs/remotes/origin/main HEAD && git remote set-head origin main
echo '{"lodash": "4.17.21"}' > package-lock.json
cat > .gitlab-ci.yml <<'YML'
stages: [build, test]
build:
stage: build
cache:
key:
files: [package-lock.json] # the key is a hash of the lockfile
paths: [.downloads/]
script:
- mkdir -p .downloads dist
- if [ -f .downloads/lodash.tgz ]; then echo "cache hit"; else echo "cache miss, downloading"; echo pkg > .downloads/lodash.tgz; fi
- echo "console.log('shop')" > dist/app.js
- echo "scratch" > notes.txt
artifacts:
paths: [dist/]
expire_in: 1 week
test:
stage: test
script:
- ls dist/ .downloads/ 2>&1 || true
- cat notes.txt 2>&1 || true
YML
git add . && git commit -qm "ci: cache and artifacts"
echo "### pipeline 1"
gitlab-ci-local --concurrency 1 --shell-isolation 2>&1 | grep -E ' > |cache|artifacts'
echo "### pipeline 2"
gitlab-ci-local --concurrency 1 --shell-isolation build 2>&1 | grep -E ' > |cache'### pipeline 1
build $ if [ -f .downloads/lodash.tgz ]; then echo "cache hit"; else echo "cache miss, downloading"; echo pkg > .downloads/lodash.tgz; fi
build > cache miss, downloading
build $ echo "console.log('shop')" > dist/app.js
build $ echo "scratch" > notes.txt
build cache created in '.gitlab-ci-local/cache/0_package-lock-a7d1c4bcc1c16a747c34f0ff0f0005ef7a32e91114cbf3e608fde04c9f6bf2c8' in 66 ms
build exported artifacts in 60 ms
build copied artifacts to cwd in 50 ms
test imported artifacts in 50 ms
test > ls: cannot access '.downloads/': No such file or directory
test > dist/:
test > app.js
test > cat: notes.txt: No such file or directory
### pipeline 2
build imported cache '0_package-lock-a7d1c4bcc1c16a747c34f0ff0f0005ef7a32e91114cbf3e608fde04c9f6bf2c8' in 61 ms
build $ if [ -f .downloads/lodash.tgz ]; then echo "cache hit"; else echo "cache miss, downloading"; echo pkg > .downloads/lodash.tgz; fi
build > cache hit
build $ echo "console.log('shop')" > dist/app.js
build $ echo "scratch" > notes.txt
build cache created in '.gitlab-ci-local/cache/0_package-lock-a7d1c4bcc1c16a747c34f0ff0f0005ef7a32e91114cbf3e608fde04c9f6bf2c8' in 67 ms
Line by line:
- Pipeline 1,
build: cache miss, so it "downloads"; then the runner saves.downloads/under a key derived from the lockfile hash and uploadsdist/as an artifact. - Pipeline 1,
test: receivesdist/app.js(artifact), but not.downloads/(the cache belongs to jobs that declare it) and notnotes.txt(undeclared files vanish with the job). - Pipeline 2,
build: imports the cache and hits. Changepackage-lock.jsonand the key changes, giving a miss and a fresh cache.
Cache options that matter
build:
cache:
- key:
files: [package-lock.json]
prefix: npm # key = npm-<hash>
paths: [.npm/]
policy: pull-push # default: download at start, upload at end
fallback_keys: [npm-main] # if the exact key misses, try these
script:
- npm ci --cache .npm --prefer-offline
test:
cache:
- key:
files: [package-lock.json]
prefix: npm
paths: [.npm/]
policy: pull # read-only: only build updates the cachepolicy: pullon jobs that only read the cache saves the upload time on every run.- Cache downloads (
.npm), then run the installer: never cachenode_modulesfrom another branch as if it were correct. - Protected branches get separate caches by default (
-protectedsuffix), so a merge request pipeline can't poison the cache thatmain's release jobs use. cache:pathsandartifacts:pathsmust be inside the project directory; point tools there (--cache .npm,PIP_CACHE_DIR: .cache/pip).
Artifact options that matter
| Option | Use |
|---|---|
expire_in: 1 week |
old artifacts cost storage; the latest artifacts of the default branch are kept by default |
when: always |
upload test reports and logs even when the job failed |
reports: junit / coverage_report / dotenv |
GitLab reads them: test results in MRs, coverage, variables for later jobs |
dependencies: [] or needs: [{job: build, artifacts: false}] |
don't download artifacts a job doesn't need |
exclude: |
leave out files inside the listed paths |
The dotenv report deserves a mention: a job writes VERSION=1.4.7 to a file declared as artifacts: reports: dotenv: build.env, and every later job that depends on it gets $VERSION as a variable, the GitLab equivalent of GitHub job outputs.
Test yourself
A deploy job needs the binary built two stages earlier. Cache or artifact?
- Cache
- Artifact
- Either
- Variables
In the session, why didn't test see notes.txt?
- It was deleted by test
- Files not declared as artifacts or cache disappear with the job's working copy
- The cache expired
- It was in .gitignore
What does cache:key:files: [package-lock.json] achieve?
- Caches the lockfile
- Derives the cache key from the lockfile's content, so dependency changes produce a new cache
- Encrypts the cache
- Shares the cache across projects