Speed Up CI/CD

Caching is used to speed up CI/CD. Let's learn how to use caching in this lesson.

We'll cover the following

You have two GitHub workflows defined so far. For each new service, you will introduce a new workflow to truly keep services independent. It also helps you significantly speed up the CI/CD pipelines when only a single service has changed. When a pull request is merged to the master branch, the workflow files execute two jobs in sequence for a given service:

  • test
  • deploy

Both jobs start by installing the root NPM packages, and then all NPM packages for all services. When we work on a pull request and push changes frequently, that’s a lot of repetitive processing, and it takes roughly 5 minutes to run the entire workflow. As we potentially add more steps or install new NPM modules, that time only increases.

Leverage caching

We can leverage caching to speed the process up significantly! Click Run and start with creating a new branch:

git switch -c speed-up-CI-CD

All we need to do is add the following step after the actions/checkout@v2 step for both jobs in both workflow configurations.

- name: Restore dependencies
  id: cache
  uses: actions/cache@master
  with:
    path: |
      node_modules
      */*/node_modules
      */*/*/node_modules
      */*/*/*/node_modules
      key: ${{ runner.os }}-${{hashFiles('**/package-lock.json')}}

As long as none of the package-lock.json files changes, we will use the cached node_modules directories and save about 60 seconds for each workflow step. The time savings will continue to get better as we add more services, and hence more NPM packages in node_modules directories.

First, a note on the three node_modules paths with * above. This is to cover the following directories:

  • services/web/node_modules
  • services/web/firebase/node_modules
  • services/web/firebase/functions/node_modules

With more effort, this could be made even more efficient, but the above is a perfectly reasonable starting point to demonstrate the capabilities of caching.

Using cache variable

This cache step outputs a variable to tell us if the cache was hit or not. If we hit a cache, we can skip the following two steps:

  • Install dependencies
  • Bootstrap all services

To configure that, update the above two steps for both workflow configurations services-web-firebase-functions-src-firestoreposts-on-create-cross-post-to-devto.yml and services-web.yml to match the following:

- name: Install dependencies
  if: steps.cache.outputs.cache-hit != 'true'
  run: npm install
- name: Bootstrap all services
  if: steps.cache.outputs.cache-hit != 'true'
  run: npm run bootstrap

Click Run to save the changes.

Get hands-on with 1200+ tech skills courses.