GitHub Actions: make job dependent on at least one other job from an array, but not all of them

Viewed 1457

In my GitHub workflow, I have a finish job that uploads the coverage report to Coveralls once all other jobs are finished.

jobs:
  foo: ...
  bar: ...
  finish:
    needs:
      - foo
      - bar

If either foo or bar fails though, or it is skipped (why a job may be skipped is not relevant to this question), the finish job won't be run.

Is there a way I could make finish run if at least one of the jobs provided in the needs field is run successfully?

1 Answers

It's currently not possible to get job.status or job.conclusion natively on the workflow to check them on other jobs through conditional.

A workaround could be to use outputs or artifacts to always save each job status.

Here is an example using artifacts with 3 jobs, where the third job would check the previous 2 jobs status before executing some operation:

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

Where the other steps of job03 would depend on the outputs results you got from the other jobs, to perform an action or not.

You can find more references about this example on the links below:

I also wrote an example here:

Related