How to configure npm build cache for nodejs build nodes in Jenkins?
After configuring the build cache for the build nodes of a Jenkins instance, reusable downloaded dependencies can be used in the Jenkins pipeline build process, reducing build time, effectively improving pipeline build efficiency, lowering network bandwidth consumption, and controlling dependencies.
This article will explain how to configure npm build cache for Node.js build nodes in your Jenkins instance.
Introduction to npm
npm (Node Package Manager) is a package management tool for JavaScript, used to manage and distribute JavaScript dependencies.
When used as a build cache for Node.js build nodes, the following scenarios can be satisfied:
-
In projects where multiple developers collaborate, build cache can reduce the time and network bandwidth spent on each developer repeatedly downloading the same dependencies.
-
In CI/CD pipelines, build cache can reduce the time for building and deployment, improving the efficiency of the entire development process.
-
In environments without internet access, build cache can help you build projects offline.
-
In resource-constrained environments, build cache can reduce resource consumption and improve build speed.
Prerequisites
-
A deployed Jenkins instance on the platform.
-
If using PVC (Persistent Volume Claim) as a cache, prepare PVC in advance in the namespace where the Jenkins instance is located.
Procedure
-
After entering the Platform Management view, click on Toolchain Management > Instance Management in the left navigation bar.
-
Click on the name of your Jenkins instance.
-
Under the Build Nodes tab, click on the Node.js node to be configured for caching.
-
Click on Update Build Node.
-
Add a Build Cache and configure the relevant parameters according to the following instructions.
Storage Type Parameter Description Notes Host Path Volume Host Path: /root/.npm
Cache Type:Custom
Cache Path: For example:/root/.npmThe actual storage location is the path on the node where the task Pod is running. If the scheduling node is not fixed, the cache is invalid, so it is recommended to use the Persistent Volume Claim type of storage. Persistent Volume Claim Cache Type: Custom
Cache Path: For example:/root/.npm
Indicates that the selected PVC will be mounted to the/root/.npmdirectory of the container at runtime.PVC may be slower than downloading directly from the dependency repository when reading and writing small files. Therefore, when using PVC as a cache, make sure the storage system has sufficient performance. -
Click Update.
Verify if the cache is effective
-
Switch to the DevOps view, click on CI/CD > Jenkins in the left navigation bar.
-
Create a new pipeline or update an existing one that includes a Node.js build task.
-
Select the Node.js node configured with the build cache as the Build Node.
-
In the build command of the Node.js build task, enter the following command to configure the cache path.
Note: Replace nodejs’s cache path in the command with the Cache Path configured for the Node.js build node in the previous steps.
npm set cache=<nodejs’s cache path> -
Execute the pipeline.
-
Log in to the node where the Jenkins instance component’s Pod is located. If a cache file
_cacachehas been generated in the Cache Path configured for the Node.js build node mentioned above, it indicates that the build cache configuration is successful and effective.