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



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

示例:

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

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

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

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

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

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

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

使用upload-artifactbuild工作流上传构建文件,使用download-artifactReleased工作流程中下载这些文件。

构建工作流

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

奖金提示:请注意;我的org/some repo/.github/工作流/build.yml@main"字符串区分大小写。我浪费了一些时间弄清楚这就是下面错误的原因。

错误:.github#L1
解析称为工作流的错误;我的org/some repo/.github/工作流/build.yml@main":找不到工作流。看见https://docs.github.com/en/actions/learn-github-actions/reusing-workflows#access-以获取更多信息。

相关内容