我们可以在github actions中缓存yarn全局变量吗

问题描述 投票:0回答:1

我有一些全局包,例如无服务器框架,ESLint等。我已经为yarn实现了GitHub Actions缓存。下面是我的代码。

- 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

但是我的全局包没有被缓存。有没有办法缓存 Yarn 全局变量?

javascript node.js yarnpkg github-actions
1个回答
2
投票

我正在粘贴解决方案的构建文件,

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

我给这些步骤命名了,以便他们可以理解。

于2020-12-06更新了答案

© www.soinside.com 2019 - 2024. All rights reserved.