GitLab CI Cache Restore Error Fix
In this tutorial, you'll learn about GitLab CI Cache Restore Error Fix. We cover key concepts, practical examples, and best practices.
Your GitLab CI job reports No cache found or Restoring cache — skipped — the cache key doesn't match any existing cache, or the cache scope prevents sharing between branches.
The Problem
# WRONG — cache key that changes every time
build:
script:
- npm ci
cache:
key: npm-cache-$CI_COMMIT_SHA
paths:
- node_modules/
Checking cache for npm-cache-abc123... no cache found
$CI_COMMIT_SHA changes on every commit. The cache key is unique every time, which defeats the purpose of caching. No cache is ever reused.
Step-by-Step Fix
1. Use a stable cache key with fallback
build:
script:
- npm ci
cache:
key:
files:
- package-lock.json
paths:
- node_modules/
GitLab automatically computes a hash of package-lock.json. The cache is valid until the lock file changes.
2. Use prefix-based cache key with multiple files
cache:
key:
files:
- package-lock.json
- .nvmrc
prefix: $CI_JOB_NAME
paths:
- node_modules/
3. Set cache scope for branch sharing
cache:
key: $CI_COMMIT_REF_SLUG
paths:
- vendor/bundle
$CI_COMMIT_REF_SLUG includes the branch name. Caches are shared across pipelines on the same branch but not across branches. Use a fixed prefix to share across branches.
4. Use separate caches for different jobs
npm-cache:
variables:
npm_config_cache: "$CI_PROJECT_DIR/.npm"
cache:
key:
files:
- package-lock.json
paths:
- .npm
bundle-cache:
cache:
key:
files:
- Gemfile.lock
paths:
- vendor/bundle
Expected output:
Checking cache for package-lock.json hash... cache hit
Restoring cache from job "build"...
✓ node_modules restored from cache
Prevention Tips
- Use
key:files:with lock files for stable cache keys - Use
prefix:to group caches by job name - Set
key: $CI_COMMIT_REF_SLUGfor branch-specific caching - Use separate cache entries for different dependency types
- Clear caches manually from Settings > CI/CD when needed
Common Mistakes with ci cache error
- Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
These mistakes appear frequently in real-world GITLAB code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro