Github Actions: cache yarn install
    9th December 2020
    Edited : Upgraded actions/setup-node to version 2.
Caching dependencies installed by Yarn in Github Actions is fairly straightforward, but there are a few small gotchas to get right:
- Caching node_modulesdirectly isn’t efficient, using Yarn’s built-in cache system is both faster and takes up less space.
- Using --prefer-offlineensures your cache is used if it is available, but the install will still pass with no errors if anything needs to be downloaded from the network.
Combining the above with the examples from the documentation, below is a functional example of a Github Action with dependency caching.
name: Descriptive Name
# Update to match your requirements
on: push
jobs:
  descriptive-name:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [12.x]
    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
    # See: https://github.com/actions/cache/blob/main/examples.md#node---yarn
    - name: Get Yarn cache directory
      id: yarn-cache-dir-path
      run: echo "::set-output name=dir::$(yarn cache dir)"
    - name: Use Yarn cache
      uses: actions/cache@v2
      id: yarn-cache
      with:
        path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
        key: ${{ runner.os }}-yarn-${{ matrix.node-version }}-${{ hashFiles('**/yarn.lock') }}
    # `--prefer-offline` gives cache priority
    - name: Install dependencies
      run: yarn install --prefer-offline --frozen-lockfile
    # Ready to run steps that rely on node_modules
    # - name: Build application
    #   run: yarn build