Pipeline Optimization — Cache Directories

· 1 min read · 176 Words · -Views -Comments

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

https://static.1991421.cn/2025/2025-01-25-102018.jpeg

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.

References

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover