Cache Configuration
You'll learn how to configure GitLab CI cache to dramatically speed up pipeline runs by reusing dependencies instead of downloading them every time.
What is Cache?
Cache in GitLab CI stores files between pipeline runs to avoid redundant work. Think of it like keeping your groceries on the kitchen counter instead of walking back to the store every time you cook—you reuse what you already fetched.
While artifacts pass data between jobs in the same pipeline, cache persists data across multiple pipeline runs for the same branch or project.
How Cache Works
You define cache in your .gitlab-ci.yml using the cache: keyword. Specify which files or directories to preserve, and GitLab saves them after job completion and restores them when the next pipeline runs.
Basic cache configuration:
build-job:
stage: build
script:
- npm install
- npm run build
cache:
paths:
- node_modules/
This saves the node_modules/ directory so future runs skip the npm install download time.
Cache Keys
Use key: to control cache sharing and invalidation:
cache:
key: $CI_COMMIT_REF_SLUG
paths:
- vendor/
Different branches get separate caches. Change the key when you need fresh dependencies.
When to Use Cache
Cache is perfect for:
- Package manager dependencies (
node_modules/,vendor/) - Build tool caches (Maven
.m2/, pip cache) - Downloaded binaries that rarely change
Key Takeaway: Cache configuration dramatically reduces pipeline execution time by reusing dependencies across runs—define paths and keys strategically to maximize the performance benefit without stale data issues.