Best practices for caching dependencies in GitHub Actions? #187175
-
Why are you starting this discussion?Question What GitHub Actions topic or product is this about?General Discussion DetailsI'm setting up CI pipelines using GitHub Actions. What are the best practices for caching dependencies (e.g. npm, pnpm, yarn) to speed up builds? Any real-world tips or examples? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Here are some proven best practices for caching dependencies in GitHub Actions:
Example setup (npm):
This approach typically reduces CI build times by 50–80% in real-world projects. |
Beta Was this translation helpful? Give feedback.
Here are some proven best practices for caching dependencies in GitHub Actions:
Use the official actions/cache action
Always prefer actions/cache@v4 for better performance and stability.
Cache based on lock files
Use package-lock.json, pnpm-lock.yaml, or yarn.lock in the cache key.
This ensures correct cache invalidation when dependencies change.
Separate cache per OS and Node version
Include runner.os and Node version in the cache key to avoid conflicts.
Cache the correct directories
Restore cache before installing dependencies
Make sure the cache restore step runs before npm install / pnpm install / yarn install.
Prefer p…