Tacos
Tacos

Reputation: 73

Reusing github actions jobs results from a workflow when called on workflow_run

Is it possible to properly get the results from jobs executed in a workflow A when workflow B is executed due to have a trigger workflow_run for workflow A?

Workflow A

name: A

on:
  push:

jobs:
  job1:
    run: exit 1
  job2:
    run: exit 0

Workflow B

name: B

on:
  workflow_run:
    workflows:
      - A
    types:
      - completed

jobs:
  job1: echo "${{ github.event.workflow_run.jobs.job1.result }}" # expects true

Thanks in advance.

Upvotes: 3

Views: 1719

Answers (2)

user7610
user7610

Reputation: 28811

GitHub Workflows get passed in the GITHUB_TOKEN, so you can run authenticated queries against the GitHub API from within the workflow.

The endpoint to give you results of jobs is (for example) https://api.github.com/repos/skupperproject/skupper-router/actions/runs/4754818643/jobs, format of which is documented at https://docs.github.com/en/rest/actions/workflow-jobs?apiVersion=2022-11-28.

So, we want to query that url (which we find in the github.event.workflow_run.jobs_url in the job being triggered) and examine it for results of the jobs we care about.

Here is a job that runs after the Build workflow completed, and checks if the job named Python Checker (ubuntu-20.04) succeeded`. Only then will it build and push a container image.

name: Publish skupper-router main
on:
  workflow_run:
    workflows: ["Build"]
    branches: [main]
    types:
      - completed

jobs:

  build-image:
    name: Publish skupper-router image
    runs-on: ubuntu-latest
    steps:
      - name: Check that python linting job from previous workflow did succeed
        run: |
          curl -L \
            -H "Accept: application/vnd.github+json" \
            -H "Authorization: Bearer ${GITHUB_TOKEN}" \
            -H "X-GitHub-Api-Version: 2022-11-28" \
            ${{ github.event.workflow_run.jobs_url }} > jobs.json
          conclusion=$(jq < jobs.json --raw-output '.jobs[] |  select( .name == "Python Checker (ubuntu-20.04)") | .conclusion')

          echo ${conclusion}
          [[ ${conclusion} == "success" ]]

        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - uses: actions/checkout@v3

      - run: |
          # build and publish the main image
          # Use {GITHUB_SHA::8} for shorter SHA
          export VERSION="${GITHUB_SHA}"
          ./.github/scripts/image.sh
        env:
          DOCKER_USER: '${{ secrets.DOCKER_USER }}'
          DOCKER_PASSWORD: '${{ secrets.DOCKER_PASSWORD }}'

Upvotes: 1

GuiFalourd
GuiFalourd

Reputation: 22970

There is no native way to achieve what you want using the Github Context alone.

It is possible to use if: ${{ github.event.workflow_run.conclusion == 'success' }} to only run the workflow B jobs if the workflow A was successful, but you can get the workflow A run jobs status that way.


However, there is a workaround (quite verbose) using artifacts to share datas between workflows:

Workflow A

name: A

on:
  push:

jobs:
  job1:
    runs-on: ubuntu-latest
    steps:
      - name: Exit 1
        run: exit 1
      - name: Create file status_job1.txt and write the job status into it
        if: always()
        run: |
          echo ${{ job.status }} > status_job1.txt    
      - name: Upload file status_job1.txt as an artifact
        if: always()
        uses: actions/upload-artifact@v1
        with:
          name: pass_status_job1
          path: ./status_job1.txt
  job2:
    runs-on: ubuntu-latest
    steps:
     - name: Create file status_job2.txt and write the job status into it
        if: always()
        run: |
          echo ${{ job.status }} > status_job2.txt    
      - name: Upload file status_job2.txt as an artifact
        if: always()
        uses: actions/upload-artifact@v1
        with:
          name: pass_status_job2
          path: ./status_job2.txt

Workflow B

name: B

on:
  workflow_run:
    workflows:
      - A
    types:
      - completed

jobs:
  job1:
    runs-on: ubuntu-latest
    steps:
      - name: Download workflow artifacts
        uses: dawidd6/[email protected]
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          workflow: workflow_A_name.yml
          run_id: ${{ github.event.workflow_run.id }}

      - name: Read the job_status1 file
        id: job_status1
        uses: juliangruber/[email protected]
        with:
          path: ./pass_status_job1/status_job1.txt

      - run: echo Job_status1 is ${{ steps.job_status1.outputs.content }}

      - name: Read the job_status2 file
        id: job_status2
        uses: juliangruber/[email protected]
        with:
          path: ./pass_status_job2/status_job2.txt

      - run: echo Job_status2 is ${{ steps.job_status2.outputs.content }}

Note: I didn't test the workflows above, but I've done something similar here, sharing a PR number between workflows, if you want to have a look for tests:

Upvotes: 3

Related Questions