How Cache Works on GitHub Actions

How Cache Works on GitHub Actions
21 de marzo de 2025
geraldine
1 min de lectura
0
Categoría:
Developers

GitHub Actions caching dependencies

Author: Nicolás de la TorreDo you know about the importance of GitHub Actions Caching Dependencies? When running workflows on GitHub Actions, installing dependencies can be a time-consuming step. These Actions are also billed based on execution time, so optimizing them helps save money, which is crucial.
PlanStorageMinutes (per month)
GitHub Free500 MB2,000
GitHub Pro1 GB3,000
GitHub Free for organizations500 MB2,000
GitHub Team2 GB3,000
GitHub Enterprise Cloud50 GB50,000
By leveraging caching, we can significantly speed up our CI/CD pipeline. In this article, we will explore how to cache node_modules to avoid redundant installations, allowing multiple jobs to run efficiently in parallel.

Caching node_modules in GitHub Actions 

Let's take the following GitHub Actions workflow as an example:name:Testson:[push,pull_request] jobs:install:name:Installpackagesruns-on:ubuntu-latesttimeout-minutes:20outputs:test-files:${{steps.list-tests.outputs.test-files}}steps:-name:Checkoutrepositoryuses:actions/checkout@v3-name:SetupNode.jsuses:actions/setup-node@v3with:node-version:'18'-name:Cachenode_modulesid:cache-node-modulesuses:actions/cache@v3with:path:node_moduleskey:node-modules-${{hashFiles('yarn.lock')}}-name:Installdependenciesrun:yarninstall-name:Listtestfilesid:list-testsrun:|nodescripts/list-tests.jsecho"test-files=$(cattest-files.json)">>"$GITHUB_OUTPUT" tests:name:Runtestfile:${{matrix.file}}runs-on:ubuntu-latesttimeout-minutes:20needs:[install]strategy:matrix:file:${{fromJson(needs.install.outputs.test-files)}}steps:-name:Checkoutrepositoryuses:actions/checkout@v3-name:SetupNode.jsuses:actions/setup-node@v3with:node-version:'18'-name:Restorenode_modulesid:cache-node-modulesuses:actions/cache@v3with:path:node_moduleskey:node-modules-${{hashFiles('yarn.lock')}}-run:|mv.env.test.env-name:Runtestforfilerun:npxhardhattest${{matrix.file}}

How Caching Works

First Run (No Cache Available)

Initially, there is no cached data. You can verify this by navigating to GitHub Repository → Actions → Caches.Image: 0-no-cache.png(Image: 0-no-cache.png)Since no cache is found, the workflow installs dependencies from scratch, which takes additional time.Image: 2-install-without-cache.png(Image: 1-no-cache-found.png)In this case, the yarn install step took 2 minutes and 52 seconds.(Image: 3-cache.png)(Image: 2-install-without-cache.png)After the workflow completes, the node_modules cache is stored, making it available for future runs.(Image: 4-install-with-cache.png)(Image: 3-cache.png)

Subsequent Runs (Using Cache)

When the action runs again, the workflow restores the cached dependencies, reducing installation time significantly.Image: 4-install-with-cache.png(Image: 4-install-with-cache.png)

Cache Invalidation

If new dependencies are installed and the yarn.lock file changes, the hashFiles('yarn.lock') value also changes. This results in a cache miss, forcing the action to install dependencies again and create a new cache entry.Image: cache-miss-example.png(Image: cache-miss-example.png)You can verify the existence of multiple cache entries under GitHub Repository → Actions → Caches.Image: cache-entries.png(Image: cache-entries.png)

Conclusion

By implementing caching in GitHub Actions, we optimize workflow execution time and improve CI/CD efficiency. However, cache invalidation should be handled properly to prevent inconsistencies. Keeping track of dependency file changes ensures that the correct packages are installed while still benefiting from caching.By following this approach, development teams can run tests and deployments faster, reducing overall execution time and improving productivity.
Última actualización: 21 de marzo de 2025