Search code examples
githubworkflowgithub-actions

Github workflow, job not executed even when job.needs successful


I have a weird problem with Github workflow. Last job job_5 is not executed even when job from needs (job_4) is executed and successful. Can anyone tell my why is that?

Workflow file:

name: Test run v2

on:
  workflow_dispatch:

jobs:
  job_1:
    runs-on: ubuntu-18.04
    steps:
      - run: echo JOB1

  job_2:
    needs: job_1
    if: false
    runs-on: ubuntu-18.04
    steps:
      - run: echo JOB2; exit 1;

  job_3:
    needs: job_1
    runs-on: ubuntu-18.04
    steps:
      - run: echo JOB3

  job_4:
    needs: [job_2, job_3]
    if: always()
    runs-on: ubuntu-18.04
    steps:
      - run: echo JOB4

  job_5:
    needs: job_4
    runs-on: ubuntu-18.04
    steps:
        - run: echo JOB5

Workflow visualization looks like this: workflow

I can fix that using for example:

  job_5:
    needs: job_4
    if: always() && needs.job_4.result == 'success'
    runs-on: ubuntu-18.04
    steps:
        - run: echo JOB5

But still why is it not working as expected when job_4 is a success?


Solution

  • This is the the fact that your job 4 is dependent on job 2 and job 3 and job 2 failed. So when you put job 4 as a need for job 5 it checks not only status of job 4 but also its dependencies (in this case job 2 and job 3) and since job 2 failed it failed evaluation needs for job 5. What is strange it is that ignores condition always() on job 4. It could be a bug or designed by purpose. I don't know that. However if you remove job 2 all went fine

    # https://stackoverflow.com/questions/65735099/github-workflow-job-not-executed-even-when-job-needs-successful
    name: Multiple jobs
    
    on:
      workflow_dispatch:
    
    jobs:
      job_1:
        runs-on: ubuntu-18.04
        steps:
          - run: echo JOB1
    
      job_2:
        needs: job_1
        if: false
        runs-on: ubuntu-18.04
        steps:
          - run: echo JOB2; exit 1;
    
      job_3:
        needs: job_1
        runs-on: ubuntu-18.04
        steps:
          - run: echo JOB3
    
      job_4:
        needs: [job_3]
        if: always()
        runs-on: ubuntu-18.04
        steps:
          - run: echo JOB4
    
      job_5:
        needs: job_4
        runs-on: ubuntu-18.04
        steps:
            - run: echo JOB5
    

    enter image description here

    So if this structure is sth you need you should use your workaround.

    You can create a topic on support community as this really can be a bug.

    There is open issue on GitHub for this - Job-level "if" condition not evaluated correctly if job in "needs" property is skipped.