Search code examples
javascriptnode.jsyarnpkggithub-actions

Can we cache yarn globals in github actions


I have some global packages such as serverless framework, ESLint and etc. I've implemented GitHub Actions cache for yarn. Below is my code.

- 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-

- name: Adding serverless globally
  run: yarn global add serverless

- name: Yarn Install
  if: steps.yarn-cache.outputs.cache-hit != 'true'              
  run: |
    echo "cache hit failed"
    yarn install
  env:
    CI: false

But my global packages are not cached. Is there any way to cache Yarn globals?


Solution

  • I'm pasting the build file for the solution,

    name: global-test
    on:
        push:
            branches:
                - dev
        pull_request:
            branches:
                - dev
    jobs:
        aws-deployment:
            runs-on: ubuntu-latest
            steps:
                - name: CHECKOUT ACTION
                  uses: actions/checkout@v2
    
                - name: NODE SETUP ACTION
                  uses: actions/setup-node@v1
                  with:
                      node-version: '12.x'
    
                - name: Get yarn cache directory path
                  id: yarn-cache-dir-path
                  run: |
                      echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
    
                - name: Set yarn global bin path
                  run: |
                      yarn config set prefix $(yarn cache dir)
    
                - name: Add yarn bin path to system path
                  run: |
                      echo $(yarn global bin) >> $GITHUB_PATH
    
                - name: Set yarn global installation path
                  run: |
                      yarn config set global-folder $(yarn cache dir)
    
                - name: CACHE ACTION
                  uses: actions/cache@v2
                  env:
                      cache-version: v1
                  id: yarn-cache
                  with:
                      path: |
                          ${{ steps.yarn-cache-dir-path.outputs.dir }}
                          **/node_modules
                      key: ${{ runner.os }}-yarn-${{ env.cache-version }}-${{ hashFiles('**/yarn.lock') }}
                      restore-keys: |
                          ${{ runner.os }}-yarn-${{ env.cache-version }}-
                          ${{ runner.os }}-yarn-
                          ${{ runner.os }}-
    
                - name: Installing dependencies
                  if: steps.yarn-cache.outputs.cache-hit != 'true'
                  run: |
                      echo "YARN CACHE CHANGED"
                      yarn install
    
                - name: Adding serverless globally
                  if: steps.yarn-cache.outputs.cache-hit != 'true'
                  run: |
                      echo "NO CACHE HIT"
                      yarn global add serverless
    

    I named the steps, so they can be understood.

    UPDATED the answer on 2020-12-06