'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.



Solution 1:[1]

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:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GuiFalourd