Artifacts, reports and cache

What a job hands on, what GitLab keeps, and what only speeds things up.

Intermediate13 min · lesson 4 of 17
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (19 files, 7 KB): artifacts.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-gitlab/artifacts.tar.gz && tar -xzf artifacts.tar.gz, which creates ~/lab/artifacts/. SHA-256: dde807b0cd92eae0039533dcef99ed5f6ae32a7d19da0574039b9e071a75a7b0

The tests failed on Friday night, and on Monday morning nobody can download the coverage report, because it was only kept when the job passed. Another team's pipeline breaks whenever its cache is cleared, because a job quietly relied on files that were never guaranteed to be there. Both are about the same question: what leaves a job, and what can the next job count on?

Artifacts and cache are different promises

ArtifactsCache
PurposeHand files to later jobs and keep a job's outputSpeed up a job by reusing files from earlier runs
Guaranteed?Yes: a job that needs them gets them or failsNo: a job must work with an empty cache
Stored byGitLab, attached to the jobThe runner (here a local volume), or object storage
Visible to peopleYes: download from the job page or the APINo
Typical contentBuild output, test reports, scan reportsPackage downloads, a scanner's database
app/.gitlab-ci.yml
# Artifacts carry a job's output to later jobs and keep it after the pipeline.
# The cache only speeds things up: a job must still work when it is empty.
default:
image: secopslog-py:3.15
stages: [build, test, package]
build:
stage: build
script:
- mkdir -p dist
- tar --sort=name --mtime=@0 --owner=0 --group=0 -czf dist/app.tar.gz calc.py requirements.txt
- (cd dist && sha256sum app.tar.gz > app.tar.gz.sha256)
artifacts:
paths: [dist/]
expire_in: 1 week
test:
stage: test
needs: []
cache:
key:
files: [requirements.txt] # a new key whenever the dependency list changes
paths: [.deps/]
script:
- |
if [ -f .deps/marker ]; then echo "cache hit, $(cat .deps/marker)"
else echo "cache miss"; mkdir -p .deps; echo "filled by job $CI_JOB_ID" > .deps/marker; fi
- python -m pytest -q --junitxml=report.xml
artifacts:
paths: [report.xml] # a plain artifact: by default kept only when the job succeeds
reports:
junit: report.xml # a report: GitLab collects it whatever the result
package:
stage: package
needs: [build] # downloads build's artifacts, and only those
script:
- ls dist
- (cd dist && sha256sum -c app.tar.gz.sha256)

build keeps dist/ for a week. package uses needs: [build], which also means it downloads build's artifacts and no others. test keeps report.xml twice: as a plain artifact anyone can download, and as a JUnit report that GitLab parses into test counts. It also uses a cache keyed on requirements.txt, so the key changes whenever the dependency list does.

ubuntu@secopslog-cicd:~/lab/artifacts · GitLab 19.5.3
$ ./up.sh
runner lab-runner: online (tags: lab,docker) GitLab 19.5.3 is up on http://127.0.0.1:8929
$ ./seed.sh app app/ && ./pipeline.sh app/
pushed app: f2c33ab first commit --- job build (build): success $ mkdir -p dist $ tar --sort=name --mtime=@0 --owner=0 --group=0 -czf dist/app.tar.gz calc.py requirements.txt $ (cd dist && sha256sum app.tar.gz > app.tar.gz.sha256) Uploading artifacts... dist/: found 3 matching artifact files and directories Uploading artifacts as "archive" to coordinator... 201 Created correlation_id=01M741JCZNAME2EW6QSQA2ZA32 id=1 status=201 token=64_RRivma --- job test (test): success Restoring cache Checking cache for 0_requirements-0a2b4e0e2f3819e8129d46376f0611987eda6375-protected... No URL provided, cache will not be downloaded from shared cache server. Instead a local version of cache will be extracted. WARNING: Cache file does not exist Failed to extract cache $ if [ -f .deps/marker ]; then echo "cache hit, $(cat .deps/marker)" # collapsed multi-line command cache miss $ python -m pytest -q --junitxml=report.xml . [100%] 1 passed in 0.03s Saving cache for successful job Creating cache 0_requirements-0a2b4e0e2f3819e8129d46376f0611987eda6375-protected... .deps/: found 2 matching artifact files and directories No URL provided, cache will not be uploaded to shared cache server. Cache will be stored only locally. Created cache Uploading artifacts... report.xml: found 1 matching artifact files and directories Uploading artifacts as "archive" to coordinator... 201 Created correlation_id=01M741JJ89HTREKGJKWDENJ4Y5 id=2 status=201 token=64_-x3d6Y Uploading artifacts... report.xml: found 1 matching artifact files and directories Uploading artifacts as "junit" to coordinator... 201 Created correlation_id=01M741JJM9JYA7BTW5TN45D8NC id=2 status=201 token=64_-x3d6Y --- job package (package): success Removing report.xml Downloading artifacts $ ls dist app.tar.gz app.tar.gz.sha256 $ (cd dist && sha256sum -c app.tar.gz.sha256) app.tar.gz: OK pipeline #1: success

build uploaded three entries from dist/: the directory, the tarball and its checksum. test found no cache (Cache file does not exist), filled .deps/ itself, and after it passed the runner saved the cache under a key built from the hash of requirements.txt, ending in -protected because main is a protected branch. Then it uploaded report.xml twice, once as "archive" and once as "junit". package downloaded only build's artifacts and verified the checksum: app.tar.gz: OK.

Taking artifacts out of GitLab

Artifacts can be fetched by branch and job name, which is how a release script or another system picks up the latest build:

ubuntu@secopslog-cicd:~/lab/artifacts · GitLab 19.5.3
$ mkdir -p out && ./gl.sh "/projects/root%2Fapp/jobs/artifacts/main/raw/dist/app.tar.gz?job=build" -o out/app.tar.gz && ./gl.sh "/projects/root%2Fapp/jobs/artifacts/main/raw/dist/app.tar.gz.sha256?job=build" -o out/app.tar.gz.sha256 && (cd out && sha256sum -c app.tar.gz.sha256)
app.tar.gz: OK
$ ./gl.sh "/projects/root%2Fapp/pipelines/$(./gl.sh '/projects/root%2Fapp/pipelines?per_page=1' | jq -r '.[0].id')/test_report_summary" | jq -c '.total | {count, success, failed}'
{"count":1,"success":1,"failed":0}

The checksum written by build still matches. The test report is parsed by GitLab, so its counts are available over the API and on the merge request, without anyone opening a log.

The cache on a second run

ubuntu@secopslog-cicd:~/lab/artifacts · GitLab 19.5.3
$ echo "# second run" >> app/calc.py && ./commit.sh app/ "second run" && ./pipeline.sh app/ | grep -E "^(--- job|cache|Checking cache|Restoring cache|Successfully extracted|No URL provided|Creating cache|Created cache|pipeline)"
pushed: 36ae471 second run to main --- job build (build): success --- job test (test): success Restoring cache Checking cache for 0_requirements-0a2b4e0e2f3819e8129d46376f0611987eda6375-protected... No URL provided, cache will not be downloaded from shared cache server. Instead a local version of cache will be extracted. Successfully extracted cache cache hit, filled by job 2 Creating cache 0_requirements-0a2b4e0e2f3819e8129d46376f0611987eda6375-protected... No URL provided, cache will not be uploaded to shared cache server. Cache will be stored only locally. Created cache --- job package (package): success pipeline #2: success
$ echo "requests==2.32.5" >> app/requirements.txt && ./commit.sh app/ "add requests" && ./pipeline.sh app/ | grep -E "^(--- job test|cache|Checking cache|Restoring cache|Successfully extracted|pipeline)"
pushed: 5bf3415 add requests to main --- job test (test): success Restoring cache Checking cache for 0_requirements-36df5d8b56f7c8a56a77e64ecfd92f56b0c658a7-protected... cache miss pipeline #3: success

On the second run the key was the same, so the runner extracted the cache and the job printed cache hit, filled by job 2. Adding a line to requirements.txt changed the hash, so the third run looked for a different key and got cache miss. That's what a key from a file is for: the cache follows the dependency list instead of going stale. Note the line from the first run, Saving cache for successful job. By default a cache is saved only when the job succeeds.

Watch out
A cache is shared by every job with the same key on the same runner, including jobs from other branches with the same protection status. GitLab keeps separate caches for protected and unprotected refs by default (the -protected suffix above), so a feature branch can't feed main. Keep that setting on, and remember what it doesn't cover: every protected ref shares the protected cache, including tags and any release/* branch Developers may push, and a shared S3 cache is open to every runner holding its credentials. Never let a job trust a cache entry without checking it.
Watch out
Anyone who can read the project can download its artifacts, and on a public project with public pipelines, that is everyone. Never put .env files, kubeconfigs or Terraform plans in artifacts. For outputs only the team should see, set artifacts:access: developer.

Hands-on

Start a fresh project with ./seed.sh app app/. Break add with sed -i 's/a + b/a - b/' app/calc.py, push with ./commit.sh app/ "break add" and run ./pipeline.sh app/. Check the test report summary as in the report step, then try to download the failed job's report.xml with ./gl.sh /projects/root%2Fapp/jobs/JOB_ID/artifacts/report.xml, taking JOB_ID from ./gl.sh '/projects/root%2Fapp/jobs?per_page=5' | jq '.[] | {id, name}'. Which of the two did GitLab keep? Then copy solutions/.gitlab-ci.yml, which adds when: always, push again, and repeat the download with the new test job's ID. In the pipeline output, the first Uploading artifacts line belongs to build, not test. Finish with ./down.sh.

What you should see: with add broken, the failed test job uploaded report.xml only as "junit". The summary API reports {"count":1,"failed":1}, yet downloading report.xml returns 404. GitLab kept the parsed report, because artifacts:reports are uploaded whatever the job's result, but not the plain artifact, because paths default to when: on_success. After when: always, the failed job uploaded report.xml as an archive too, and the download shows failures="1" tests="1". Keep failure evidence with when: always, or the file you most need is gone.

Remember

Later jobs and people rely on artifacts, while a job must work without its cache. Reports are collected whatever the result, but plain artifacts need when: always to survive a failure. Set expire_in on everything big, and never let a job depend on a cache entry being there.

Quick check
01A job builds dist/app.tar.gz, and a deploy job in a later stage needs it. Which mechanism fits?
Incorrect — A cache can be empty or evicted, and a deploy must not depend on it.
Correct — The build job declares them as artifacts, and they are guaranteed to reach jobs that ask for them.
Incorrect — Each job starts from a clean checkout. Files do not carry over.
Incorrect — Variables are for small configuration, not build output.
02A job has artifacts: paths: [coverage/] and reports: junit: report.xml, with no when. The tests fail. What does GitLab keep?
Incorrect — Plain artifacts follow artifacts:when, which defaults to on_success.
Incorrect — Reports are an exception: GitLab collects them whatever the result.
Incorrect — It is the other way round.
Correct — The lab showed it: the failed job's test counts were there, but report.xml could not be downloaded until when: always was added.
03A job fills its cache with pip downloads keyed on requirements.txt. Someone adds a package. What happens on the next run?
Correct — The lab showed a cache miss after requirements.txt changed.
Incorrect — A changed file means a different key, so the old cache is not restored.
Incorrect — A missing cache is never an error.
Incorrect — Caches are not merged. A new key starts an empty cache.

Related