Cut CI/CD Pipeline Time in Half by Caching Dependencies
Your pipeline re-downloads the same packages every single run — caching stops this waste.
What
CI/CD caching stores your project's dependencies (node_modules, pip packages, Go modules) between pipeline runs. Instead of downloading and installing everything from scratch each time, the pipeline restores the cached versions and only fetches what's changed.
Why It Matters
Dependency installation is often the slowest step in CI pipelines. A Node.js project might spend 2-3 minutes on npm install every run, even when nothing changed. With caching, that drops to seconds. Across hundreds of daily runs, this saves hours and reduces costs.
Example
# GitHub Actions — cache node_modules
- name: Cache dependencies
uses: actions/cache@v4
with:
path: ~/.npm
key: npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
npm-
- name: Install dependencies
run: npm ci
# The cache key uses package-lock.json hash
# Same lockfile = cache hit = skip downloadCommon Mistake
Caching node_modules directly instead of the npm/yarn cache directory. The node_modules folder can have platform-specific binaries that break across different CI runners.
Quick Fix
Cache the package manager's cache directory (~/.npm, ~/.cache/yarn, ~/.cache/pip) and let npm ci / yarn install rebuild from that cache. This is faster and more reliable.
Key Takeaways
- 1Same deps downloaded every run = wasted time
- 2Cache dependencies between pipeline runs
- 3Key on lockfile hash (package-lock.json)
- 4Cache the package manager cache, NOT node_modules
- 5Result: 2-3 min install → 10 seconds
Was this tip helpful?
Help us improve the DevOpsPath daily collection