Cache your actions

If your actions have a long installation step (or very big node_modules) you can cache the installation step using the GitHub action actions/cache.

This is very useful for cases where you use a matrix to run multiple tests.

They have a lot of implementation examples, but they all follow the same logic.

Find out where your package manager downloads the artifacts

There are step by step instructions for most popular package manager. For example, you can obtain npm’s cache by having the following step:

- name: Get npm cache directory
  id: npm-cache-dir
  shell: bash
  run: echo "dir=$(npm config get cache)" >> ${GITHUB_OUTPUT}

In the case of this action, the cache gets store into the steps.npm-cache-dir.outputs.dir variable.

Restoring the cache

After you know where your cache is located, you can restore it by using the actions/cache action:

- uses: actions/cache@v4
  with:
    path: ${{ steps.npm-cache-dir.outputs.dir }} # Cache variable
    key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
    restore-keys: |
      ${{ runner.os }}-node-

We only want to restore the cache when the packages changed, so we set the key variable to use the hashed package-lock.json.

We also don’t want to cache the dependencies for a different OS, so we add to the key variable the name of the OS.

And that’s all! Now you can cache all your actions.

Remember to checkout the examples for your specific package manager.