首页 > 解决方案 > Github Actions/workflow_run/outputs

问题描述

我有一个工作流build,其输出存储在 docker 注册表中,具体取决于其结果,然后,我想运行e2e test.

我知道我可以使用workflow_run,但不清楚如何将输出传递给依赖的工作流。

on:
  workflow_run:
    workflows: ["Build"]
    types: [completed]

我应该能够获取IMAGE_URL输出并对那个特定的人工制品运行测试。

  1. 如何设置工作流输出
  2. 如何读取工作流输出

当前的解决方法是使用workflow_dispatch,但它的缺点是没有被列为 PR 检查。

标签: githubcontinuous-integrationgithub-actionsgithub-actions-artifacts

解决方案


您可以将要传递的变量和值写入文件,并将其作为工件上传到触发工作流中。

在触发的工作流中,下载触发工作流运行的工件。然后解析文件以获取您的变量。

触发工作流

[...]
name: Build
jobs:
  aJob:    
    name: A job
    runs-on: ubuntu-latest
    steps:
    - run: echo "aVariable,aValue" > vars.csv

    - uses: actions/upload-artifact@v2
      with:
        name: variables
        path: vars.csv

触发的工作流
来自其他工作流的工件不能与操作一起下载download-artifact

on:
  workflow_run:
    workflows: ["Build"]
    types: [completed]
jobs:
  aJob:    
    name: A job
    runs-on: ubuntu-latest
    steps:
    - uses: actions/github-script@v4
      id: get-artifact-id
      with:
        result-encoding: string
        script: |
          const result = await octokit.request('GET /repos/{owner}/{repo}/actions/runs/{run_id}/artifacts', {
            owner: '${{github.repository_owner}}',
            repo: '${{github.event.repository.name}}',
            run_id: ${{github.event.workflow_run.id}}
          })
         # assumes the variables artifact is the only one in this workflow
         return result.data.artifacts[0].artifact_id
    - name: Get result
      run: |
        echo "${{steps.get-artifact-id.outputs.result}}"
        curl -L -H "Authorization: token ${{github.token}}" \
          -H "Accept: application/vnd.github.v3+json" \
          -O variables.zip \
          https://api.github.com/repos/${{github.repository}}/actions/artifacts/${{steps.get-artifact-id.outputs.result}}/zip
        unzip variables.zip
        # parse variables from variables.csv and set them

推荐阅读