What is the difference between a build artifact and a cache in CI/CD?
Understand the difference between build artifacts and caches in CI/CD: required outputs versus best-effort speedups, with examples and keying tips.
Expected Interview Answer
A build artifact is a deliberate output of a job that you want to keep and pass to later stages or download, while a cache is a performance optimization that stores reusable dependencies to speed up future runs.
Artifacts are about correctness and hand-off: compiled binaries, test reports, or bundles that downstream jobs depend on, so they are reliably uploaded and re-downloaded exactly. A cache is about speed and is best-effort: things like `node_modules` or a package registry cache that can be rebuilt if missing. The pipeline must never depend on a cache being present, whereas it can and should depend on artifacts. Artifacts are tied to a specific pipeline and expire on a policy; caches are keyed and shared across pipelines and branches.
- Artifacts guarantee correct hand-off of outputs between stages
- Caches cut build time by reusing dependencies across runs
- Clear separation prevents brittle pipelines that rely on cache presence
- Artifacts can be downloaded and inspected after the pipeline finishes
- Caches are keyed so unrelated branches don't collide
AI Mentor Explanation
An artifact is the official match scorecard you file with the league — a required output others depend on. A cache is the pitch-condition notes the groundsman keeps to prep faster next time; handy if present, but the match still proceeds without them. You never cancel a game because last week's prep notes went missing, but you cannot skip filing the scorecard.
Step-by-Step Explanation
Step 1
Identify required outputs
Decide which job outputs later stages or users truly depend on — those become artifacts.
Step 2
Declare artifacts
List the paths under an artifacts block so the CI system uploads and re-downloads them for downstream jobs.
Step 3
Identify reusable inputs
Find directories that are expensive to rebuild but reconstructable, like node_modules or ~/.m2 — those become caches.
Step 4
Key the cache
Give the cache a key (often a lockfile hash) so it is shared correctly and invalidated when dependencies change.
Step 5
Set expiry and fallbacks
Give artifacts an expiration policy and never let job logic assume the cache exists — rebuild if it's a miss.
What Interviewer Expects
- Knows artifacts are deliberate outputs, caches are best-effort speedups
- Understands pipelines may depend on artifacts but never on caches
- Can give examples: binaries/test reports vs node_modules
- Aware artifacts expire and are pipeline-scoped while caches are keyed and shared
- Explains why relying on cache presence makes a pipeline brittle
Common Mistakes
- Using a cache to pass required outputs between jobs
- Assuming the cache is always present and skipping the rebuild step
- Storing huge dependency folders as artifacts, bloating storage
- Forgetting to key the cache, causing collisions across branches
- Not setting artifact expiration, wasting storage over time
Best Answer (HR Friendly)
“An artifact is a finished output the pipeline needs to keep, like a built app or test report, and it is reliably passed along. A cache just stores things like downloaded libraries to make future runs faster, and the pipeline still works fine even if the cache is missing.”
Code Example
build:
stage: build
image: node:20
cache:
key:
files:
- package-lock.json
paths:
- node_modules/ # reusable, best-effort speedup
script:
- npm ci
- npm run build
artifacts:
paths:
- dist/ # required output passed downstream
expire_in: 1 weekFollow-up Questions
- Why should a pipeline never depend on a cache being present?
- How would you key a cache so branches don't collide?
- What is a good artifact expiration policy and why?
- How do artifacts get passed to a job in a later stage?
- When would you use a dependency proxy or remote cache instead of a local cache?
MCQ Practice
1. Which statement is true about caches in CI/CD?
A cache is a performance optimization; jobs must still work when the cache is absent.
2. Which is the correct use of an artifact?
Artifacts are deliberate outputs meant to be handed off downstream or downloaded.
3. What is a common way to key a dependency cache?
Keying on a lockfile hash shares the cache when dependencies match and invalidates it when they change.
Flash Cards
Artifact vs cache in one line? — Artifact = required output you keep and hand off; cache = best-effort speedup you can rebuild.
Can a pipeline depend on a cache? — No — caches may be missing; jobs must still succeed by rebuilding. Depend on artifacts instead.
Typical artifact examples? — Compiled binaries, bundles, test reports, coverage files, deployment packages.
Typical cache examples? — node_modules, ~/.m2, pip wheels, Go module cache — reconstructable dependencies.