Caching Dependencies for Faster Builds
Speed up CI pipelines by caching dependencies and build artifacts with the actions/cache action, keys, and restore-keys.
Caching Dependencies for Faster Builds is a free DevOps Bootcamp lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Cost of Reinstalling
Most CI time is spent downloading and installing the same dependencies on every run. Caching stores them between runs so subsequent builds reuse the work, often cutting pipeline time dramatically.
The cache Action
GitHub provides actions/cache. You give it a path to cache and a key that identifies the cache entry. On a hit, it restores the path; otherwise it saves it after the job.
- uses: actions/cache@v4
with:
path: ~/.npm
key: npm-${{ hashFiles('package-lock.json') }}Designing a Good Key
The key should change only when the cached content should change. Hashing the lockfile is ideal: a new lockfile means new dependencies, so a new cache entry is created.
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}Restore Keys
restore-keys provides fallback prefixes. If the exact key misses, the action restores the most recent cache whose key starts with a prefix, giving a partial warm start.
restore-keys: |
${{ runner.os }}-npm-Cache Hit vs Miss
On an exact key match you get a cache hit and the path is restored. On a miss, the job runs normally and the cache is saved at the end using the primary key, ready for next time.
Built-in Setup Caching
The language setup actions have caching built in. For Node, setup-node with cache: npm handles the dependency cache for you, no separate cache step needed.
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npmWhat to Cache
Cache package manager download directories, not the installed node_modules when possible, because download caches are portable and the install step still verifies integrity.
- Node:
~/.npm - Python: pip cache
- Java:
~/.m2or Gradle caches
Cache Scope and Limits
Caches are scoped to a branch (with fallback to the default branch) and have a size limit per repository. Unused caches are evicted over time, so design keys that churn cleanly.
Avoiding Stale Caches
If your key never changes, you can serve outdated dependencies. Always tie the key to the lockfile hash so updates invalidate the cache automatically.
Matrix and OS in Keys
In matrix builds across OSes or versions, include runner.os and the version in the key so each variant gets its own correct cache instead of clashing.
key: ${{ runner.os }}-node20-${{ hashFiles('package-lock.json') }}Measuring the Win
Compare job durations before and after caching, and watch the cache-hit indicator in logs. A consistently low hit rate signals a key that changes too often.
Quick Check
Test your understanding of dependency caching.
Recap
You sped up CI with dependency caching: the actions/cache action with a lockfile-hash key and restore-keys fallback, built-in caching in setup actions, choosing what to cache, including OS/version in matrix keys, and avoiding stale caches by tying the key to the lockfile.
Frequently asked questions
Is the “Caching Dependencies for Faster Builds” lesson free?
Yes — the full text of “Caching Dependencies for Faster Builds” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Caching Dependencies for Faster Builds”?
Speed up CI pipelines by caching dependencies and build artifacts with the actions/cache action, keys, and restore-keys. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Caching Dependencies for Faster Builds” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Workflow Triggers and Events
- Running Tests with GitHub Actions
- Linting and Code Quality Checks
- Caching Dependencies for Faster Builds