DEV Community

0 seconds of 0 secondsVolume 90%
Press shift question mark to access a list of keyboard shortcuts
00:00
00:00
00:00
 
Brian Douglas for GitHub

Posted on • Edited on

10 3

Caching dependencies to speed up workflows in GitHub Actions

To make your workflows faster and more efficient, you can create and use caches for dependencies and other commonly reused files.

About caching workflow dependencies

GitHub Workflow runs often reuse the same downloaded dependencies from one run to another. For example, package and dependency management tools such as npm and Yarn keep a local cache of downloaded dependencies.

Jobs on GitHub-hosted runners start in a clean virtual environment and must download dependencies each time, causing increased network utilization, longer runtime, and increased cost. GitHub can cache dependencies you frequently use in workflows to help speed up the time it takes to recreate these files.

To cache dependencies for a job, you'll need to use GitHub's cache action. The action retrieves a cache identified by a unique key. For more information, see actions/cache.

GitHub logo actions / cache

Cache dependencies and build outputs in GitHub Actions

Example using the cache action

This example creates a new cache when the packages in the package-lock.json file change or when the runner's operating system changes. The cache key uses contexts and expressions to generate a key that includes the runner's operating system and a SHA-256 hash of the package-lock.json file.

name: Caching with npm

on: push

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Cache node modules
      uses: actions/cache@v2
      env:
        cache-name: cache-node-modules
      with:
        # npm cache files are stored in `~/.npm` on Linux/macOS
        path: ~/.npm
        key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-build-${{ env.cache-name }}-
          ${{ runner.os }}-build-
          ${{ runner.os }}-

    - name: Install Dependencies
      run: npm install

    - name: Build
      run: npm build

    - name: Test
      run: npm test
Enter fullscreen mode Exit fullscreen mode

To learn more about cache hits and the cache eviction policy, check out the GitHub Documentation.

This is part of my 28 days of Actions series. To get notified of more GitHub Action tips, follow the GitHub organization right here on Dev. Learn how to build action with Node.js

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (6)

Collapse
 
stevetaylor profile image
Steve Taylor

What's the point of this caching feature if I have to run npm install again anyway?

Collapse
 
andrewmcoupe profile image
Andy Coupe

It won't install all of your deps, it'll grab them from the cache.

Collapse
 
juanmendes profile image
juanmendes

That's right. And beyond that, it should use npm ci, right? It's unclear what having npm install in your own steps means.

Collapse
 
fabianaasara profile image
Fabiana Asara

If you are using setup-node actions in your workflow it caches dependencies using actions/cache under the hood so that you don't need to add all of that in your code.
Caching is turned off by default but you can enable it adding the cache input parameter:

steps:
     - name: Setup node
        uses: actions/setup-node@v3
        with:
          node-version: '16'
          cache: 'npm'
Enter fullscreen mode Exit fullscreen mode
Collapse
 
estruyf profile image
Elio Struyf

Immediately implemented into the workflow I was creating! Great tip!

Collapse
 
cesarcoatl profile image
César Román

Is it worth keeping caches "alive" so they don't get deleted after seven days of inactivity?

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay