Conditionally run a Job based on the output of another Job

I know Jobs have an if parameter, but it doesn’t seem to be possible to use an output of a previous Job in the expression for the if.

As an example:

...
jobs:
  run-check:
    outputs:
      approved: steps.pr-eval.outputs.approved
    steps:
      - name: PR Approval Status
         id: pr-eval
         run: |
           <do some checks>
           echo "::set-output name=approved::true"
  deploy:
    needs: [run-check]
    // Invalid "Unrecognised named-value: 'jobs'
    if: jobs.run-check.outputs.approved == 'true' 
    // None of these work
    if: needs.run-check.outputs.approved
    if: needs.run-check.outputs.approved == 'true' 
    if: needs.run-check.outputs.approved == true

Anyone got any ideas or is this just not possible?

Hi @itsjxck,

Glad to see you in Github Community Forum!

This is due to you omit ${{}} for the step output value in job run-check, the job output value is not true in this case.

Fixed as below:

jobs:
  run-check:
    runs-on: ubuntu-latest
    outputs:
      approved: ${{ steps.pre-val.outputs.approved }}  # add ${{ }} for the step output
    steps:
      - name: PR Approval Status
        id: pre-val
        run: |
           echo "::set-output name=approved::true"
  deploy:
    needs: [run-check]
    runs-on: ubuntu-latest
    if: needs.run-check.outputs.approved == 'true'

Thanks

2 Likes

You’re a star, thank you!