Github Actions 可重用工作流程 yarn 缓存未找到。

3
我刚开始写 GH Actions。我正在完成一个任务,删除常用的工作流程并使用可重用的工作流程功能(reusable workflow feature)。现在我能够按顺序运行我的工作流程,这很棒。然而,第二个工作流程出现了意外的错误消息,似乎与 yarn 依赖项工作流程未保存到缓存有关,这让我感到困惑:

执行 yarn lint ... snip myPackage: /bin/sh: 1: concurrently: not found

请您看一下,确认这是否正常?目前,我的目标是创建一个 pull-request 工作流程,调用可重用功能中的 yarn 和 lint。
name: pull-request
on:
  pull_request:
    branches:
      - main
jobs:
  yarn:
    uses: ./.github/workflows/yarn.yml
  validate_lint:
    needs: yarn
    uses: ./.github/workflows/validate_lint.yml

使用:

name: Yarn

on:
  workflow_call:

jobs:
  yarn_and_deps:
    name: Run Lint
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Git repository
        uses: actions/checkout@v3

      - name: Enable node
        uses: actions/setup-node@v3
        with:
          node-version: 16

      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - uses: actions/cache@v3
        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: Install dependencies
        run: yarn install --frozen-lockfile && yarn bootstrap
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

并且

name: Validate Lint

on:
  workflow_call:

jobs:
  run_lint:
    name: Run Lint
    runs-on: ubuntu-latest

    - name: Enable node
        uses: actions/setup-node@v3
        with:
          node-version: 16
          cache: 'yarn' # <<--- THIS CACHE IS NOT FOUND ‍♂️

    # NOTE: if I add in all the "yarn cache/install" commands from above workflow, this passes.

    steps:
      - name: Validate Lint
        run: yarn lint

错误发生在“验证 Lint”作业中,因为似乎找不到缓存。我创建了“yarn”作业,以避免每个作业都重新创建轮子。
我的缓存期望与实际工作方式有什么问题?每个作业都需要执行“安装依赖项”步骤感觉过于繁琐。

为什么你在一个工作流中使用了'cache: yarn',而在另一个工作流中却没有使用? - rethab
这是官方 actions 提供的示例:https://github.com/actions/cache/blob/main/examples.md#node---yarn - Phil Lucks
1个回答

3
每个工作流都是一个独立的Docker容器。因此,如果我在工作流1的容器中运行yarn,则工作流2无法访问缓存或知道这个过程。
最接近的解决方案似乎是上传/下载"共享数据",但这也有其缺点,例如下载node_modules文件夹比仅安装依赖项要慢。

不幸的是,解决方案似乎是每个工作流都依赖前一个项目的输出,因此存在代码重复的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接