Github Actions / workflow_run / outputs

2.1k views Asked by At

I have a workflow build whose output is store in a docker registry, depending on its outcome, then, I would like to run a e2e test.

I know I can use workflow_run but it's not clear how to pass outputs to the dependant workflow.

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

I should be able to grab the IMAGE_URL output and run tests on that specific artefact.

  1. how can I set a workflow output
  2. how can I read a workflow output

Current workaround is using workflow_dispatch, but it has the drawback of not being listed as PR check.

2

There are 2 answers

1
riQQ On

You can write your variables and values, which you want to pass, to a file and upload it as an artifact in the triggering workflow.

In the triggered workflow, download the artifact of the triggering workflow run. Then parse the file to get your variables.

Triggering workflow

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

Triggered workflow
(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
0
Hitmands On

You can declaratively access artifacts from the workflow_run event this way:

      - 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