If you are following agile then continuous integration is must. I have setup CI/CD in Azure DevOps and I personally found my Angular build is taking lot of time. The npm install step itself takes like around 3 minutes or so to download all node_modules files. If you are using nrwl.Nx monorepo then you will notice your
npm install or
npm ci step will take around 8 to 10 minutes. Every-time you push the build it will download all of the node packages from npm.org and will spend same amount of time even-though you have not changed their versions. That is waste of time and resources.
Fortunately, Azure DevOps now offers a new build task called cache task (Pipeline Caching). In this article I will give you steps to create a Build Definition by using Cache Task. I will demonstrate how you can speedup your build process more than 50% or so.
After restoring from cache during build time. If I use the node package to run some program it should work lets test it.
add build script in package.json
build: node index.js
const chalk = require('chalk'); console.log(chalk.blue('Hello world!'));
Check the log of
npm install or
In my case it says
D:\\a\\.npm which means it is
$(Pipeline.Workspace)/.npm in azure devops machine.
In this example it says cache location is
After first time build if you do not change package-lock.json file and rebuild then your build should be super fast even less than a second.
Create new pipeline and select classic editor and start adding task as mentioned below.
Pipeline caching can help reduce build time by allowing the outputs or downloaded dependencies from one run to be reused in later runs, thereby reducing or avoiding the cost to recreate or re-download the same files again.
name: Cache npm Dependencies key: **/package-lock.json, !**/node_modules/**/package-lock.json, !**/.*/**/package-lock.json path:$(Build.SourcesDirectory)/node_modules
Inside Control options:
- set enabled
- Run this task: Select Custom Condition
eq(variables['CacheRestored'],False)in Custom Condition Text.
Add new npm task and call it
Add your build script from your package.json
pool: name: Azure Pipelines demands: npm steps: - task: Cache@2 displayName: 'Cache npm Dependencies' inputs: key: '**/package-lock.json, !**/node_modules/**/package-lock.json, !**/.*/**/package-lock.json' path: '$(Build.SourcesDirectory)/node_modules' cacheHitVar: CacheRestored - task: Npm@1 displayName: 'npm ci' inputs: command: ci verbose: false condition: eq(variables['CacheRestored'],False) - task: Npm@1 displayName: 'npm build' inputs: command: custom verbose: false customCommand: 'run build'
If you want to become full stack developer and grow your carrier as new software developer or Lead Developer/Architect. Consider subscribing to our full stack development training programs. We have All-Access Monthly membership plans and you will get unlimited access to all of our video courses, slides, source code & Monthly video calls.
- Please subscribe to All-Access Membership PRO plan to access current and future angular, node.js and related courses.
- Please subscribe to All-Access Membership ELITE plan to get everything from PRO plan. Additionally, you will get access to monthly live Q&A video call with Rupesh and you can ask doubts/questions and get more help, tips and tricks.
You bright future is waiting for you so visit today FullstackMaster and allow me to help you to board on your dream software company as a Developer,Architect or Lead Engineer role.