how to run dependent jobs if dependency is skipped?

Viewed 35

If I have 2 jobs, where one must run after the other, but the former can be skipped due to some condition, how do I ensure that the second job runs if its condition is true? (I am using https://github.com/marketplace/actions/paths-changes-filter to determine whether a job should run based on whether a change has happened in its subdirectory).

jobs:
  job1:
    if: some_condition_1

  job2:
    needs: job1
    if: some_condition_2

So, if some_condition_1 is false, then job1 will not fire. How do I ensure that if some_condition_2 is true, that job2 runs if job1 is skipped? In the above setup, job2 does not fire if job1 is skipped. Additionally, job2 needs to run after job1 if job1 does actually run.

1 Answers

You can use the jobs.<job_id>.if conditional - doc's - along with status check functions success(), always() or failure() for a specific job - doc's - to create a separate logic for job control simply using && instead of using need for prerequisite jobs.

Edit with little example:

name: demo 

on:
  push:
    branches: [ "main" ]
  workflow_dispatch:

jobs:
  
  job1:
    runs-on: ubuntu-latest
    if: ${{ 'some_condition_1'=='not_true' }}
    steps:
      - name: Step for job1
        run: echo Hello from job1! 
  job2:
    runs-on: ubuntu-latest
    needs: job1
    if: ${{ always()  && 'some_condition_2'=='some_condition_2' }}
    steps:
      - name: Step for job2
        run: echo Hello from job2! 
Related