在另一个作业中重用 GitHub Action 工作流程步骤

Dun*_*Luk 13 continuous-integration github github-actions

我已经使用触发器创建了可重用工作流程workflow_call,但我需要根据其结果运行其他步骤。

例子:

jobs:
  released:
    steps:
      - name: Build
        uses: my-org/some-repo/.github/workflows/build.yml@main

      - name: Upload source maps
        run: something
Run Code Online (Sandbox Code Playgroud)

可重用的构建步骤构建我的 JS 应用程序并生成源映射。现在,我需要将这些源映射作为单独的步骤上传到某个位置,该步骤应该仅在此已发布的作业中运行。

执行上述操作会导致以下错误:

错误:.github#L1
可重用工作流程应在顶级“jobs.*.uses”键引用,而不是在步骤内引用

它只允许在作业中运行我的可重用工作流程,而不是在步骤中运行。但这样做我就无法再访问源映射了。

我的问题:如何重用构建工作流程中的步骤并在已发布作业中访问其输出?

Dun*_*Luk 9

您可以使用工件在作业之间共享这些输出文件。

使用从构建upload-artifact工作流程上传构建文件并在已发布工作流程中下载它们。download-artifact

构建工作流程

name: Build

on:
  workflow_call:
    secrets:
      SOME_SECRET:
        required: true

jobs:
  build:
    steps:
      # Your build steps here

      - name: Create build-output artifact
        uses: actions/upload-artifact@master
        with:
          name: build-output
          path: build/
Run Code Online (Sandbox Code Playgroud)

发布工作流程

name: Released

on:
  push:
    branches:
      - main

jobs:
  build:
    uses: my-org/some-repo/.github/workflows/build.yml@main
    secrets:
      SOME_SECRET: ${{ secrets.SOME_SECRET }}

  released:
    needs: build

    steps:
      - name: Download build-output artifact
        uses: actions/download-artifact@master
        with:
          name: build-output
          path: build/

      # The "build" directory is now available inside this job

      - name: Upload source maps
        run: something
Run Code Online (Sandbox Code Playgroud)

额外提示:请注意,“my-org/some-repo/.github/workflows/build.yml@main”字符串区分大小写。我浪费了一些时间来弄清楚这就是下面错误的原因。

错误:.github#L1
错误解析调用工作流“my-org/some-repo/.github/workflows/build.yml@main”:找不到工作流。有关更多信息,请参阅https://docs.github.com/en/actions/learn-github-actions/reusing-workflows#access-to-reusable-workflows 。