Semantics of using "job.needs" and "job.if" together

The documentation at https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#jobsjob_idneeds states:

If a job fails, all jobs that need it are skipped unless the jobs use a conditional statement
that causes the job to continue.

Now suppose I have a job configured as follows:

job2:
  needs: job1
  if: true

Will job2 always run regardless of the outcome of job1? Will it at least wait until job1 finishes?

2 Likes

Hi @hiranya911 ,

Job2 will be executed until job1 finished. As doc mentioned, if job1 failed, job2 will be skipped.

The exception is, if you set ‘if: always()’ in job2, job2 will be executed even job1 failed. Please refer to the official doc for more details.

job2:
    runs-on: [ubuntu-latest]
    if: always()
    needs: job1

always.png

3 Likes

Thanks @weide-zhou 

It’s great to know that always() works in the context of chained jobs too. I also tried the following, as a further experiment, and thought I’d share my results:

jobs:
  job1:
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo Job 1
        exit 1

  job2:
    runs-on: ubuntu-latest
    needs: job1
    if: github.event.client_payload.job2
    steps:
    - run: echo Job 2

Here job2 never runs (even if the client_payload.job2 is set to true), since the job1 is guaranteed to fail. This is exactly the semantics I needed for my workflow.

you have to use always() it is a special flag