if-statementgithub-actionsjobsskipjobexecutiondecider

How to execute a job that needs a job that was skipped in github actions


below workflow has 4 jobs.

I want job conditional-job to execute no matter whether the jobs it depends on are skipped i.e. skip-job and build.

Please let me know if and how is it possible.

name: My GitHub Actions Workflow

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: This job runs only on pull requests
        run: echo "JOB name build"    

  deploy:
    if: ${{ false }}  
    runs-on: ubuntu-latest
    steps:
      - name: This job runs only on pull requests
        run: echo "JOB name deploy"    
      # Your deployment steps go here...

  skip-job:
    if: ${{ false }} # This job will never run
    runs-on: ubuntu-latest
    steps:
      - name: This job is skipped
        run: echo "This job won't run because the if condition is set to false"

  conditional-job:
    needs:
      - skip-job  # This sets the 'skip-job' job as a dependency
      - deploy   # This sets the 'deploy' job as a dependency
    if: ${{ true }}
    runs-on: ubuntu-latest
    steps:
      - name: This job runs only on pull requests
        run: echo "JOB name conditional-job"

Kindly let me know.


Solution

  • Please use expression needs.skip-job.result == 'skipped' || needs. deploy.result == 'skipped'

    If you are not sure about result value you can print it before adding condition

      conditional-job:
        needs:
          - skip-job  # This sets the 'skip-job' job as a dependency
          - deploy   # This sets the 'deploy' job as a dependency
        if: ${{ true }}
        runs-on: ubuntu-latest
        steps:
          - name: This job runs only on pull requests
            run: echo "JOB name conditional-job"
          - name: check result
            run: |
             echo 'needs.skip-job.result'
             echo 'needs.deploy.result'