60
loading...
This website collects cookies to deliver better user experience
While building/compiling angular or node application on Local Build Machine
or Azure Pipelines
or GitHub Workflows
or Netflix
or Heroku
. The main issue we face is npm install
or npm ci
which takes large amount of time to finish. Which slows-down the productivity of the team. With GitHub Workflows
dependency caching you can reduce the install time to half. Therefore, in this article we will learn how can you leverage the GitHub caching
workflow dependencies mechanism to improve speed and save network utilization or bandwidth of your build server.
node_modules
from workflow.skip npm install
when a cached version is available.invalidate the cache
when our dependencies are changed.A workflow is a unit of automation from start to finish, including the definition of what triggers the automation, what environment or other aspects should be taken account during the automation, and what should happen as a result of the trigger.(GitHub, 2020).
A job is a section of the workflow, and is made up of one or more steps. In this section of our workflow, the template defines the steps that make up the build job.(GitHub, 2020).
A step represents one effect of the automation. A step could be defined as a GitHub Action, or another unit, like printing something to the console.(GitHub, 2020).
A GitHub Action is a piece of automation written in a way that is compatible with workflows. Actions can be written by GitHub, by the open source community, or you can write them yourself!(GitHub, 2020).
actions/cache@v2
to save and restore npm dependencies.node_modules
folder in node or angular applications.package-lock.json
file changes cache action will create new cache with cache key. The Cache key uses context and expression to generate a new cache key that includes the runner’s operating system and a SHA-256 hash of the package-lock.json
file. When key doesn’t match an existing cache, it’s called a cache miss , and a new cache is created if the job completes successfully.package-lock.json
file is not changed. Then cache action will compare the cache key and if it matches an existing cache, it is called as cache hit. And the cache action will restore the cached node_modules
files to the path
directory.restore-keys
.actions/cache@v2
action.actions/cache@v2
action in your GitHub workflow. We want to cache node_modules
folder. Therefore, lets set path: node_modules
.package-lock.json
file changes we want to recreate cache. Therefore, let’s use package-lock.json
in our key. Also key will have os
and cach-name
.cache-hit as true
. Therefore, this express steps.cache-nodemodules.outputs.cache-hit
will be true
steps.cache-nodemodules.outputs.cache-hit
will be true
then we must not run npm install
.- name: Install Dependencies
if: steps.cache-nodemodules.outputs.cache-hit != 'true'
# 👆 check if cache-hit happened then do not execute npm ci
run: npm ci
.github\workflows\main.yml
file and add below script.Cache node modules
action will not be able to find the cache.node_modules
in GitHub cache. It will install all node packages on Install Dependencies
step. And notice Install Dependencies
step took total 18.878s. Therefore, total build time will be around 38sCache will be saved
. The step name will be updated to Post Cache node modules
and will only execute once Job is successful.package-lock.json
file. Total Workflow Job time is 34s.node_modules
from the cache.Install Dependencies
step skipped.cache
action will print below info about the Cache Hit. That proves that cache restoration happened successfully.Post job cleanup.
Cache hit occurred on the primary key Linux-build-cache-node-modules-c473bbd9f33d84fd892675fbfce1a74d9c8b3f61d4e37c05ad92e29a23790116, not saving cache.
package-lock.json
is changed workflow will be slow. However, in a team we rarely update package-lcok.json
file and you will get good speed at other builds where you only change the project files.Rupesh
and you can ask doubts/questions and get more help, tips and tricks.Your bright future is awaiting for you so visit today FullstackMaster and allow me to help you to board on your dream software company as a new Software Developer, Architect or Lead Engineer role.