Artifacts, reports and cache
What a job hands on, what GitLab keeps, and what only speeds things up.
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: dde807b0cd92eae0039533dcef99ed5f6ae32a7d19da0574039b9e071a75a7b0The 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
| Artifacts | Cache | |
|---|---|---|
| Purpose | Hand files to later jobs and keep a job's output | Speed up a job by reusing files from earlier runs |
| Guaranteed? | Yes: a job that needs them gets them or fails | No: a job must work with an empty cache |
| Stored by | GitLab, attached to the job | The runner (here a local volume), or object storage |
| Visible to people | Yes: download from the job page or the API | No |
| Typical content | Build output, test reports, scan reports | Package downloads, a scanner's database |
# 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.15stages: [build, test, package]build:stage: buildscript:- 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 weektest:stage: testneeds: []cache:key:files: [requirements.txt] # a new key whenever the dependency list changespaths: [.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.xmlartifacts:paths: [report.xml] # a plain artifact: by default kept only when the job succeedsreports:junit: report.xml # a report: GitLab collects it whatever the resultpackage:stage: packageneeds: [build] # downloads build's artifacts, and only thosescript:- 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.
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:
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
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.
-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..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.
dist/app.tar.gz, and a deploy job in a later stage needs it. Which mechanism fits?artifacts: paths: [coverage/] and reports: junit: report.xml, with no when. The tests fail. What does GitLab keep?artifacts:when, which defaults to on_success.report.xml could not be downloaded until when: always was added.pip downloads keyed on requirements.txt. Someone adds a package. What happens on the next run?requirements.txt changed.