Reader small image

You're reading from  GitHub Actions Cookbook

Product typeBook
Published inApr 2024
PublisherPackt
ISBN-139781835468944
Edition1st Edition
Concepts
Right arrow
Author (1)
Michael Kaufmann
Michael Kaufmann
author image
Michael Kaufmann

Michael Kaufmann believes that developers and engineers can be happy and productive at work. He loves DevOps, GitHub, Azure, and modern work. Microsoft has awarded him with the title Microsoft Regional Director (RD) and Microsoft Most Valuable Professional (MVP) – the latter in the category of DevOps and GitHub. Michael is also the founder and managing director of Xebia Microsoft Services, Germany – a consulting company that helps its customers become digital leaders by supporting them in their cloud, DevOps, and digital transformation. Michael shares his knowledge in books, training, and as a frequent speaker at international conferences.
Read more about Michael Kaufmann

Right arrow

Using caching in workflows

In this recipe, we will use caching to optimize the speed of workflows.

Getting ready

Switch to a new branch:

$ git switch -c cache-npm-packages

How to do it…

  1. Edit the .github/workflows/ci.yml file. After the setup-node action, add the following script to get the npm cache directory for the correct npm version and store it as an output variable:
    - name: Get npm cache directory
      id: npm-cache-dir
      run: echo "dir=$(npm config get cache)" >> "${GITHUB_OUTPUT}"
  2. Add the actual cache step right after that. Also, give it a name and create a key from the hash of the package-lock.json file:
    - uses: actions/cache@v3
      id: npm-cache
      with:
        path: ${{ steps.npm-cache-dir.outputs.dir }}
        key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
        ...
lock icon
The rest of the page is locked
Previous PageNext Chapter
You have been reading a chapter from
GitHub Actions Cookbook
Published in: Apr 2024Publisher: PacktISBN-13: 9781835468944

Author (1)

author image
Michael Kaufmann

Michael Kaufmann believes that developers and engineers can be happy and productive at work. He loves DevOps, GitHub, Azure, and modern work. Microsoft has awarded him with the title Microsoft Regional Director (RD) and Microsoft Most Valuable Professional (MVP) – the latter in the category of DevOps and GitHub. Michael is also the founder and managing director of Xebia Microsoft Services, Germany – a consulting company that helps its customers become digital leaders by supporting them in their cloud, DevOps, and digital transformation. Michael shares his knowledge in books, training, and as a frequent speaker at international conferences.
Read more about Michael Kaufmann