Pipeline caching can help reduce build time by allowing the outputs or downloaded dependencies from one run to be reused in later runs, reducing or avoiding the cost to recreate or redownload the same files again.
Recently, we at Pyramid Consulting started DevOps for our projects, one of the existing features on DevOps is CI/CD, and to implement the same we used the pipeline. Currently, we are using it in the Angular project, so the following are tasks we performed in Agent to deploy/build the code. So, let’s consider this as a default agent pipeline.
Now when we run this pipeline
While using hosted build agents; each time a build is triggered a new VM is allocated thus any previously cached npm packages are lost. So, whenever the pipeline runs, it downloads the npm package before the build. This will spike the network and build cost, and we have also remembered the deployment is in DevOps is not fully free.
Now, the question is how can we overcome the same? There are a couple of ways to do this; one is Self-Hosted Agent and the other is Artifacts but the simplest and most effective what I feel in this scenario is “Cache Task” introduced by Microsoft.
As Per Microsoft “Caching is especially useful in scenarios where the same dependencies are downloaded over and over at the start of each run” So we added “Cache Task” in our project and run the build again.
Now, if you compare with the earlier Pipeline, you will see one additional task “Cache” and configure the same. We will discuss configuration a little later, for now, let’s see what changes. When we run Pipeline, the very first time you don’t see any changes as we start caching initially and there won’t be any cache hence no changes in execution time.
Now comes the magic time, when you execute the pipeline a second time, now compare npm install from 5 minutes to 26 seconds and total pipeline execution time from 10 minutes to 3 minutes we save almost 70% time of build execution.
It is worth noting that caching is currently supported in CI and deployment jobs, but not in classic release jobs.