GitHub Actions - 重用其他可重用工作流程的输出

Kri*_*ris 21 github github-actions

我不确定是否可能,但我正在尝试使用一个可重用工作流程的输出,在另一个可重用工作流程中,这是同一调用者工作流程的一部分。作为参考,请参阅以下配置:

呼叫者工作流程:

jobs:
  call-workflow-calver:
    uses: ./.github/workflows/called-workflow1.yaml
    secrets: inherit

  call-workflow-echo:
    needs: call-workflow-calver
    uses: ./.github/workflows/called-workflow2.yaml
    secrets: inherit
Run Code Online (Sandbox Code Playgroud)

$VERSION用于创建 CalVer 标签的作业(作为操作的一部分输出)

称为工作流程 1:

...
jobs:
  calver:
    name: Create CalVer tag
...
    steps:
      - name: Calver tag
        uses: StephaneBour/actions-calver@1.4.4
        if: ${{ github.ref == 'refs/heads/main' }}
        id: calVer
        with:
          date_format: "%Y-%m-%d"
          release: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
...
Run Code Online (Sandbox Code Playgroud)

尝试使用另一个调用者工作流程的 CalVer $VERSION 输出

称为工作流程2:

...
- name: Echo
  run: |
    echo ${{needs.calver.outputs.VERSION}}
...
Run Code Online (Sandbox Code Playgroud)

基本概念是我尝试在另一个可重用工作流程中使用输出来设置工作流程 1 中的 CalVer 版本,在工作流程 2 中调用它,以便我可以将其设置为图像版本。我最终将在第三个可重用工作流程中使用来部署所述图像。如果这可能的话,那就太好了!

希望这一切都有道理,但如果有任何需要澄清的地方,请告诉我!

提前谢谢了!

Gui*_*urd 36

根据官方文档,您现在可以将输出声明为可重用的工作流程。

这些工作就像作业输出一样,needs.<reusable>.outputs.<output>一旦声明输出,就可以通过格式获得。

例子

1.可复用的工作流程配置:

name: Reusable workflow

on:
  workflow_call:
    # Map the workflow outputs to job outputs
    outputs:
      firstword:
        description: "The first output string"
        value: ${{ jobs.example_job.outputs.output1 }}
      secondword:
        description: "The second output string"
        value: ${{ jobs.example_job.outputs.output2 }}

jobs:
  example_job:
    name: Generate output
    runs-on: ubuntu-latest
    # Map the job outputs to step outputs
    outputs:
      output1: ${{ steps.step1.outputs.firstword }}
      output2: ${{ steps.step2.outputs.secondword }}
    steps:
      - id: step1
        run: echo "firstword=hello" >> $GITHUB_OUTPUT
      - id: step2
        run: echo "secondword=world" >> $GITHUB_OUTPUT
Run Code Online (Sandbox Code Playgroud)

2. 使用可重用的工作流程:

name: Call a reusable workflow and use its outputs

on:
  workflow_dispatch:

jobs:
  job1:
    uses: octo-org/example-repo/.github/workflows/called-workflow.yml@v1

  job2:
    runs-on: ubuntu-latest
    needs: job1
    steps:
      - run: echo ${{ needs.job1.outputs.firstword }} ${{ needs.job1.outputs.secondword }}
Run Code Online (Sandbox Code Playgroud)

请注意,如果使用矩阵策略执行设置输出的可重用工作流程,则输出将是由实际设置值的矩阵的最后一次成功完成的可重用工作流程设置的输出。这意味着,如果最后一个成功完成的可重用工作流程为其输出设置空字符串,而倒数第二个成功完成的可重用工作流程为其输出设置实际值,则输出将包含倒数第二个完成的可重用工作流程的值。

如果您想检查工作流程运行的日志,我在此使用此工作流程作为示例。

  • 啊,太棒了!不知道我是怎么错过这个的!我的 google-fu 最近似乎不太对劲:我会尝试一下:) (2认同)