GitLab CI has two features that sound alike (artifacts and cache), and mixing them up slows pipelines down fast.
Artifacts: passing build outputs downstream
Use artifacts for intermediate build outputs created by one job and needed by later jobs in the same pipeline (like .next/ static assets or test outputs):
build_job:
artifacts:
paths:
- .next/**/*
expire_in: 1h
test_job:
needs:
- job: build_job
script:
- yarn test
Artifacts are scoped to one pipeline and downloaded every time a dependent job runs, so keep expire_in short enough not to fill up GitLab server storage.
Cache: sharing downloaded dependencies across pipelines
Use cache for downloaded dependencies like node_modules/, keyed by yarn.lock or package-lock.json:
.global_cache:
cache:
key:
files:
- yarn.lock
paths:
- node_modules/
policy: pull-push
Downstream lint and test jobs should override policy: pull. Leaving the default pull-push on every job forces each one to download, unzip, use, re-zip, and re-upload node_modules every time. Keep in mind that runner-local caches only help when jobs land on the same runner (or share a distributed cache).
Extracting files out of a Docker image
If you build inside a multi-stage Docker image and still want to upload static assets to a CDN or test compiled files without rebuilding, extract just the directory you need into a .tar artifact:
build_job:
script:
- docker run --entrypoint '' --rm "$IMAGE_TAG" tar -cf - -C /app .next > dot_next.tar
artifacts:
paths:
- dot_next.tar
expire_in: 1h
GitLab already compresses artifacts, so plain tar saves CPU while keeping test and CDN-upload jobs fast.