Pipeline Optimization — Cache Directories
Continuous integration pipelines often rebuild frontend projects, and package installs happen constantly. If every job starts from scratch—fresh checkout, fresh
npm install
—the feedback loop crawls. Caching pays off quickly.
Typical Case: Cache npm on the Host
Scenario: the CI agent installs dependencies directly on the host with npm install
. Persist the host’s npm cache so subsequent runs reuse it. This doesn’t help Docker-based builds because installing inside the container doesn’t touch the host’s node_modules
.
Docker Builds: Mount the npm Cache
pipeline {
agent any
stages {
stage('npm cache') {
steps {
script {
docker.image('node:14').inside('-v /root/.npm/:/root/.npm/') {
sh 'npm install'
}
}
}
}
}
}
Scenario: the CI pipeline builds the frontend Docker image, and the npm install
step happens inside the container. Mount the host’s npm cache into the container to avoid redownloading packages.
Timing Comparison
Measure your pipeline both ways. In my tests, caching slashed repeated builds from minutes to seconds.
Final Thoughts
Whether it’s frontend or backend, look for caches that can be safely shared between runs to boost throughput.