【问题标题】:Reusing GitHub Action workflow steps inside another job在另一个作业中重用 GitHub Action 工作流程步骤
【发布时间】:2021-12-28 09:35:09
【问题描述】:

我使用workflow_call 触发器创建了Reusable Workflow,但我需要根据其结果运行其他步骤。

例子:

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

      - name: Upload source maps
        run: something

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

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

错误:.github#L1
可重用的工作流应该在顶级 `jobs.*.uses' 键中引用,而不是在步骤中

它只允许在作业中运行我的可重用工作流,而不是在步骤中。但是这样我就不能再访问源地图了。

我的问题:如何重用 Build 工作流程中的步骤并在 Released 作业中访问其输出?

【问题讨论】:

  • 如果我理解正确,将你的东西分成多个工作并让工作通过输出进行通信是否会有所帮助? stackoverflow.com/a/61236803/1080523
  • @rethab 如果我理解正确,输出仅适用于字符串值。在这种情况下,我需要访问生成的源映射 files.
  • 构建作业能否将源映射上传为构建工件(使用 artifact-upload),然后发布作业会再次下载它们并将它们上传到它想上传的任何其他位置?
  • 您可以在.github/actions/<action-folder-name> 目录中的此存储库上使用composite 类型创建一个本地操作,并在您的工作流作业中使用uses: ./.github/actions/<action-folder-name> 的步骤访问它。此操作可能有一个输出变量,您可以在其他步骤中访问该变量。
  • 我做了一个例子:the workflow,带有输出的local (composite) action,workflow run。让我知道它是否能解决您的问题。

标签: github continuous-integration github-actions


【解决方案1】:

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

使用upload-artifact 从Build 工作流程上传构建文件,并使用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/

发布的工作流程

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

额外提示:请注意,“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 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-26
    • 2022-08-23
    • 2021-07-23
    • 2020-03-29
    • 1970-01-01
    • 2020-09-11
    • 2023-02-07
    • 2015-09-15
    相关资源
    最近更新 更多