How get the status of previous jobs

Hey :slight_smile:

I have some questions:

With this workflow:

on:
push:
branches:

  • master

name: Test
jobs:
job1:
{bla bla bla}
job2:
{bla bla bla}
job3:
needs: [job1, job2]
{bla bla bla}

Is there a possible way to send the status of previous jobs to JOB3 (failed, success…)?

I am trying to make a notification system (EG: Workflow failed: JOB2 error.). I know that jobs are generally independent. And I know that I can do something similar at some step of the work.

Maybe checking in some way the status of work flow … (?)

Any ideas?

Thank you

1 Like

By default, once a step in a running job fails, all the subsequent steps in this job will be skipped and this job will be marked as failed. If you want the subsequent steps still execute, you can add the if conditional (if: always()) in each subsequent step.

According to your request, you can reference the workaround I mentioned in this ticket.

Add 2 extra steps at the end of the job1 and job2, and set the 2 steps always execute (if: always()). The first one is used to create a text file and write the job status into it, and the second one is used to upload this text file as an artifact. In job3, you also need to add the steps to download the artifacts and read the statuses of jo1 and jo2.

A simple demo:

jobs:
  JOB_01:
    name: Job 01
    . . .
    steps:
      - name: Some steps of job 01
      . . .
      - name: Create file status_job01.txt and write the job status into it
        if: always()
        run: |
          echo ${{ job.status }} > status_job01.txt

      - name: Upload file status_job01.txt as an artifact
        if: always()
        uses: actions/upload-artifact@v1
        with:
          name: pass_status_job01
          path: status_job01.txt

  JOB_02:
    name: Job 02
    . . .
    steps:
      - name: Some steps of job 02
      . . .
      - name: Create file status_job02.txt and write the job status into it
        if: always()
        run: |
          echo ${{ job.status }} > status_job02.txt

      - name: Upload file status_job02.txt as an artifact
        if: always()
        uses: actions/upload-artifact@v1
        with:
          name: pass_status_job02
          path: status_job02.txt

  JOB_03:
    needs: [JOB_01, JOB_02]
    if: always()
    name: Job 03
    . . .
    steps:
      - name: Download artifact pass_status_job01
        uses: actions/download-artifact@v1
        with:
          name: pass_status_job01

      - name: Download artifact pass_status_job02
        uses: actions/download-artifact@v1
        with:
          name: pass_status_job02

      - name: Set the statuses of Job 01 and Job 02 as output parameters
        id: set_outputs
        run: |
          echo "::set-output name=status_job01::$(<pass_status_job01/status_job01.txt)"
          echo "::set-output name=status_job02::$(<pass_status_job02/status_job02.txt)"

      - name: Show the values of the outputs
        run: |
          # using the syntax steps.<step_id>.outputs.<output_name> to access the output parameters
          echo "status_job01 = ${{ steps.set_outputs.outputs.status_job01 }}"
          echo "status_job02 = ${{ steps.set_outputs.outputs.status_job02 }}"

      - name: Some other steps of job 03
      . . .

  

1 Like