【问题标题】:Github Actions / workflow_run / outputsGithub Actions/workflow_run/outputs
【发布时间】:2021-08-30 12:36:39
【问题描述】:

我有一个工作流build,其输出存储在 docker 注册表中, 然后,根据结果,我想运行e2e test

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

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

我应该能够获取IMAGE_URL 输出并对该特定工件运行测试。

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

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

【问题讨论】:

    标签: github continuous-integration github-actions github-actions-artifacts


    【解决方案1】:

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

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

    触发工作流

    [...]
    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
    

    触发的工作流
    (artifacts from other workflows can't be downloaded with the action 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
    

    【讨论】:

    • 你不应该需要一个工件位置,还是什么?你怎么知道要下载哪个工件?可以举个例子吗?
    【解决方案2】:

    您可以通过这种方式从workflow_run 事件中以声明方式访问工件:

          - name: Download BuildMetadata
            if: github.event_name == 'workflow_run'
            uses: dawidd6/action-download-artifact@v2
            with:
              workflow: 'Build'
              workflow_conclusion: success
              github_token: ${{ secrets.GITHUB_TOKEN }}
              run_id: ${{ github.event.workflow_run.id }}
              run_number: ${{ github.event.workflow_run.run_number }}
              name: build.meta.json
    

    【讨论】:

      猜你喜欢
      • 2022-12-16
      • 2022-12-12
      • 2022-10-05
      • 2020-01-14
      • 2020-04-15
      • 2020-09-06
      • 1970-01-01
      • 2021-06-19
      • 2020-11-21
      相关资源
      最近更新 更多