💸Save up to $132K/month in CI costs!👉 Try Free
Skip to main content
← Back to workflows

How Do I Cache Steps in GitHub Actions?

actions/cache -
GitHub Action
v4.0.2
4,490
Contributors
Contributor - dhadkaContributor - joshmgrossContributor - kotewar
Categories

Optimize Your CI/CD Pipeline

Get instant insights into your CI/CD performance and costs. Reduce build times by up to 45% and save on infrastructure costs.

45% Faster Builds
60% Cost Reduction
Usage
name: GitHub Actions Cache
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Cache Primes
id: cache-primes
uses: actions/cache@v4

cache logo

Cache

Cache dependencies and build outputs in GitHub Actions


What is GitHub Actions cache?

The GitHub Actions cache is a feature that allows you to store and reuse files and dependencies between workflow runs. This can significantly speed up your workflows by avoiding redundant downloads or builds.

How to Update Cache for Changes in Dependencies?

To ensure that your cache reflects any changes in dependencies, you can utilize a hash of the lockfile as a key. This approach ensures that the same cache is restored for a lockfile until there's a change in the listed dependencies.

- uses: actions/cache@v3
with:
path: |
path/to/dependencies
some/other/dependencies
key: cache-${{ hashFiles('**/lockfiles') }}

By using the hash of the lockfile as the cache key, you can ensure that the cache is updated whenever there are changes in dependencies, thus maintaining consistency.

How to Use Restore Keys to Download the Closest Matching Cache?

If a cache matching the primary key is not found, you can use restore keys to download the closest matching cache that was recently created. This approach minimizes the need to fetch additional dependencies, saving build time.

- uses: actions/cache@v3
with:
path: |
path/to/dependencies
some/other/dependencies
key: cache-npm-${{ hashFiles('**/lockfiles') }}
restore-keys: |
cache-npm-

The use of restore keys ensures that even if an exact match for the cache is not found, the workflow can retrieve the closest matching cache, reducing the need for redundant downloads.

How to Separate Caches by Operating System?

For workflows running on multiple operating systems, you can store caches separately for each OS. This can be particularly useful when combined with hashfiles to generate multiple caches per OS.

- uses: actions/cache@v3
with:
path: |
path/to/dependencies
some/other/dependencies
key: ${{ runner.os }}-cache

By separating caches by operating system, you can optimize caching strategies for each environment, enhancing workflow performance.

How to Create a Short-Lived Cache?

To scope caches to a specific workflow run or attempt, you can use the run ID or run attempt. This approach is effective for creating short-lived caches.

key: cache-${{ github.run_id }}-${{ github.run_attempt }}

Short-lived caches based on the workflow run ID or attempt provide temporary storage for workflow-specific data, optimizing resource utilization.