Awesome
Deprecated
This functionality is now part of actions/setup-node
:
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
cache: 'yarn'
gha-yarn-cache
1-liner yarn install cache for GitHub Actions
Status and support
- ✔ stable
- ✔ supported
- ✖ no ongoing development
GitHub Action caches improve build times and reduce network dependencies. However, writing the correct cache logic is tricky. You need to understand how the cache action (keys and restore keys) work. Did you know you're not supposed to cache the node_modules
folder? The setup is different per OS and takes a lot of space in your workflows. Not anymore!
gha-yarn-cache
is a simple 1-liner that covers all use-cases, correctly:
- Caches the Yarn cache directory instead of
node-modules
as recommended - Works on Ubuntu, MacOS and Windows
- Restore keys take the OS into account as recommended
- Builds on the native cache functionality of GitHub Actions, same as v2 of the generic cache action
Usage
Add this step before yarn install
:
uses: c-hive/gha-yarn-cache@v2
For example:
.github/workflows/ci.yml
name: CI
on: [push]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: c-hive/gha-yarn-cache@v2
# with:
# directory: server # Optional, speficy the folder of yarn.lock if not in the root directory
- name: Install JS dependencies
run: yarn install
- name: Test
run: yarn test
Solution comparison
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache@v1
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
gha-yarn-cache
- uses: c-hive/gha-yarn-cache@v2
Conventions
This project follows C-Hive guides for code style, way of working and other development concerns.
License
The project is available as open source under the terms of the MIT License.