If condition on block or job

Hello!

I’m fairly new to the whole actions :slight_smile: and I’ve got some questions.
I want to stop a job from happening if the previous job didnt happen.

  • How do I expose a variable from the previous job?
  • How do I check for the value of this variable?
  • Does a single if: blocks everything after it or just the next line?

Thanks

Hi @joanofdart,

Glad to see you in Github Community Forum!

To answer your queries:

Jobs run in parallel by default. To run jobs sequentially, you can define dependencies on other jobs using the jobs.<job_id>.needs keyword. For example:

jobs:
  job1:
  job2:
    needs: job1

In this example, job1 must complete successfully before job2 begins, if job1 fails, the job2 will not begin.

Jobs are executed on individual VM, you can transfer the variable value to the next job which needs the previous one.
‘set-ouput’ in previous job and get via jobs.<jobs_id>.outputs. in the 2nd job, please check the sample here for more details.

The if expression can be defined in step or job level, it only affects the step or job where it defines, not affect the next step or jobs.
Please check the link for more details.
Job level:
https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idif
step level:
https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsif

Hope it clear and helpful!

1 Like

Hey @weide-zhou

Thank you so much for replying.
It was indeed clear and helpful.

Best wishes!

Thanks @joanofdart for your reply, glad to know it’s helpful.
Could you please help to mark then the ticket can be archived, and it would help others who have same queries.

Cheers!