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

395 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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'

0
On

If you need to run your job after another job only when it's successful or skipped, you need to add them as needs. It makes the job wait for the other job. Then need to use if to make it conditional. Also, ensure you have always() alongside with other conditions.

name: build

on:
  push:
  pull_request:
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - run: echo build

  skipped:
    name: Skipped
    if: ${{ false }}
    runs-on: ubuntu-latest
    steps:
      - run: echo skipped

  conditional:
    needs: 
      - skipped
      - build
    name: Conditional
    if: |
      always() && 
      (needs.skipped.result == 'skipped' || needs.skipped.result == 'success') &&
      (needs.build.result == 'skipped' || needs.build.result == 'success')
    runs-on: ubuntu-latest
    steps:
      - run: echo ${{ needs.skipped.result }}