DevOps / GitLab CI Basics Interview Questions
What is pipeline caching and how do you configure cache keys?
Cache in GitLab CI speeds up jobs by storing files (typically dependencies like node_modules/) between pipeline runs. The cache key determines which jobs share a cache and when the cache is invalidated.
# Basic cache with branch-scoped key: cache: key: "$CI_COMMIT_REF_SLUG" # different cache per branch paths: - .npm/ - node_modules/ # Cache keyed to dependency file (invalidates when file changes): cache: key: files: - package-lock.json # cache key changes when lockfile changes prefix: "$CI_COMMIT_REF_SLUG" paths: - node_modules/ # Cache policies (pull-push is default): job-with-readonly-cache: cache: key: $CI_COMMIT_REF_SLUG paths: - node_modules/ policy: pull # only download cache, never update it job-that-updates-cache: cache: policy: push # only upload, skip downloading # Per-job cache: build: cache: key: "build-$CI_COMMIT_REF_SLUG" paths: [dist/] test: cache: key: "deps-$CI_COMMIT_REF_SLUG" paths: [node_modules/]
| Policy | Behaviour |
|---|---|
| pull-push (default) | Download cache at start, upload updated cache at end |
| pull | Download cache only - never update (read-only) |
| push | Upload cache only - never download (write-only) |
Use policy: pull for most jobs that just use the cache, and one dedicated job with policy: push or pull-push to update it. This prevents multiple parallel jobs from overwriting each other's cache updates.
More Related questions...