package.json, it’s crucial to invalidate the existing cache so Jenkins reinstalls packages based on the updated lock file. Below is a step-by-step guide to demonstrate how cache invalidation works in a Jenkins pipeline using the Job Cache plugin’s cacheValidityDecidingFile option.
1. Navigate to Your Repository
Switch back to your Solar System project:2. Add a New Dependency
Install a new package (e.g.,localtunnel) to trigger a change in package-lock.json:
package.json and package-lock.json, altering the lock file’s hash.
3. Commit & Push
Stage and commit the updated dependency files:4. Observe Cache Invalidation in Jenkins
In the pipeline’s console output, look for entries related to the cache plugin:- Jenkins computes a new hash for
package-lock.json. - Since the hash differs from the previous build, the old cache is skipped.
- A fresh cache is created based on the updated dependencies.
On subsequent runs, if
package-lock.json remains unchanged, Jenkins will restore the cache instead of reinstalling all packages.5. Cache Invalidation Logic
| Condition | Action |
|---|---|
package-lock.json has changed | Invalidate old cache & recreate |
package-lock.json is unchanged | Restore existing cache |
cacheValidityDecidingFile: package-lock.json in your Job Cache plugin configuration, you ensure your Node.js dependencies are always in sync with the lock file.