npm包存储方案咨询:Jenkins+OpenShift4.3下AngularJS构建提速
Absolutely! You can absolutely store npm packages locally (via caching) or in a private registry to slash that hour-long build time—this is a standard optimization for CI/CD pipelines dealing with slow dependency installs. Let’s break down the most practical solutions for your stack:
1. Leverage Local Caching (Quick Win)
The easiest way to avoid re-downloading packages every build is to reuse npm’s built-in cache across runs. Here’s how to set this up for your environment:
For Jenkins:
- Configure your Jenkins agent to persist the npm cache directory between builds. You can do this by:
- Setting a custom cache path in your build steps with
npm ci --cache /path/to/persistent/cache/dir(replace the path with a directory retained on your Jenkins node). - If using Jenkins pipelines, define a workspace cache or use a Docker agent with a volume mount dedicated to the cache.
- Setting a custom cache path in your build steps with
For OpenShift 4.3:
- Add a build cache volume to your OpenShift BuildConfig. This mounts a persistent volume to the npm cache directory (usually
~/.npmornode_modules/.cache/npm) so packages are retained between builds. Example snippet for your BuildConfig:spec: strategy: sourceStrategy: from: kind: ImageStreamTag name: node:16-alpine env: - name: NPM_CONFIG_CACHE value: /tmp/npm-cache volumes: - name: npm-cache persistentVolumeClaim: claimName: npm-cache-pvc volumeMounts: - name: npm-cache mountPath: /tmp/npm-cache
2. Set Up a Private npm Registry (Long-Term Solution)
For even faster and more reliable dependency pulls, host a private npm registry inside your network. Popular options include:
- Verdaccio: Lightweight, open-source, and easy to deploy on OpenShift or a dedicated server.
- Sonatype Nexus or JFrog Artifactory: Enterprise-grade tools with additional features like dependency scanning and access control.
How to use it:
- Deploy your private registry (for Verdaccio, use its official Docker image and deploy it to OpenShift with a persistent volume).
- Configure your project to use the private registry:
- Add a
.npmrcfile to your AngularJS repo with:registry=http://your-private-registry-url:4873/ - Or set the registry dynamically in your Jenkins build step:
npm config set registry http://your-private-registry-url:4873/
- Add a
- Sync commonly used packages from npmjs.org to your private registry (most tools do this automatically when a package is first requested).
3. Offline Package Archive (For Strictly Air-Gapped Environments)
If your environment can’t access external registries at all, you can pre-package your dependencies into an archive:
- Run
npm installlocally on a machine with internet access. - Zip up the
node_modulesfolder and thepackage-lock.jsonfile. - Store this archive in an internal storage location (like Jenkins Shared Library, OpenShift PVC, or your company’s file server).
- In your Jenkins build step, download the archive, unzip it, and run
npm install --offlineto reuse the pre-downloaded packages. Just remember to update the archive whenever yourpackage.jsondependencies change!
Key Tips for Success:
- Use
npm ciinstead ofnpm installin your builds—it’s faster and uses the exact versions frompackage-lock.json, avoiding unexpected updates. - Regularly clean old cache entries to prevent disk bloat (use
npm cache clean --forceperiodically, or set a retention policy on your persistent volume). - For private registries, ensure your Jenkins agents and OpenShift build pods can reach the registry over your internal network.
内容的提问来源于stack exchange,提问作者Mansi

