I have a workflow with several jobs that run sequentially, using needs. I know I can force a job to run even if previous jobs fail by using if:
jobs: one: - do stuff - two: needs: one - do more stuff - three: needs: [one, two] if: always() - do even more stuff -
The problem with this approach, is that job three will always run, no matter what happens with one and/or two.
In my case, three needs to wait for both one and two to finish before it runs, but it should NOT run if one fails. As long as one succeeds, then three should run once both one and two finish. How can I accomplish this? I want something like:
jobs: one: - do stuff - two: needs: one - do more stuff - three: needs: [one, two] if: (jobs.one.status == 'success') - do even more stuff -
Is there a way to do this with Github Actions?
There are not predefined environment variables or contexts to get the statuses of previous jobs.
As a workaround, you can try to add 2 extra steps at the end of job_one:
● The first one is used to create a text file and write the content ‘true’ in it.
● The second one is used to upload this text file as an artifact.
In job_three, also need to add 2 extra steps at the top:
● The first one is used to download the artifact;
● The second one is used to read the content of the text file from the downloaded artifact, and set the content as the value of a output parameter.
Then according to the value of the output parameter to decide whether execute the other steps of job_three.
NOTE: Because the value of output parameter only can be shared between the steps in a same job, not across jobs, so select using artifact to pass the value across jobs.
A simple demo:
jobs: job_one: name: Job One runs-on: ubuntu-latest steps: - name: Run steps of Job One run: echo "Some steps of Job One" - name: Create file 'RUN_JOB_THREE.TXT' and write 'true' into it run: echo 'true' > RUN_JOB_THREE.TXT - name: Upload file 'RUN_JOB_THREE.TXT' as an artifact uses: actions/upload-artifact@v1 with: name: pass_file path: RUN_JOB_THREE.TXT job_two: needs: [job_one] name: Job Two runs-on: ubuntu-latest steps: - name: Run steps of Job Two run: echo "Some steps of Job Two" job_three: needs: [job_one, job_two] if: always() name: Job Three runs-on: ubuntu-latest steps: - name: Download file 'RUN_JOB_THREE.TXT' from artifact uses: actions/download-artifact@v1 with: name: pass_file - name: Read file 'RUN_JOB_THREE.TXT' and set output parameter id: set_output run: echo "::set-output name=RUN_JOB_THREE::$(<pass_file/RUN_JOB_THREE.TXT)" - name: Run steps of Job Three if: steps.set_output.outputs.RUN_JOB_THREE == 'true' run: echo "Some steps of Job Three"