DevOps / GitLab CI Basics Interview Questions
What is the difference between cache and artifacts in GitLab CI?
Cache and artifacts are both mechanisms for persisting files across jobs, but they serve very different purposes and have different behaviours.
| Aspect | Cache | Artifacts |
|---|---|---|
| Purpose | Speed up jobs by reusing downloaded dependencies | Pass build outputs between stages/jobs |
| Storage | Runner's local disk or shared S3/GCS | GitLab server |
| Scope | Same runner, same branch (by cache key) | Any runner, same pipeline |
| Typical content | node_modules/, .m2/, pip cache | dist/, binaries, test reports |
| Reliability | Not guaranteed (may not exist) | Guaranteed (always transferred) |
| Downloaded by | Only the same runner (by default) | All jobs in later stages |
| Expiry | Configurable; can be cleared manually | expire_in (default: 30 days) |
# Cache example: reuse npm packages across pipeline runs cache: key: "$CI_COMMIT_REF_SLUG" # different cache per branch paths: - node_modules/ policy: pull-push # default: download at start, upload at end # Artifacts example: pass build output to next stage build: script: npm run build artifacts: paths: - dist/ # Best practice: use both together build-job: cache: paths: - node_modules/ # cache deps for speed script: - npm ci - npm run build artifacts: paths: - dist/ # pass output to deploy stage
Key insight: cache is not reliable for passing required build outputs between jobs - it is a performance optimisation only. For guaranteed file transfer between jobs, always use artifacts.
More Related questions...