Search code examples
githubgithub-actions

Github Actions Job always running even with conditional


I have a github actions workflow dispatch that takes some input. i have three jobs within it. The second and Third jobs should depend on the first completing successfully, plus the second Job should only run if a checkbox input is true, and the third job should only run if another checkbox input is true.

This is what I currently have, but its not working as I expect, the second and third jobs do not run:

on:
  workflow_dispatch:
    inputs:
      input1:
        type: boolean
        description: input 1 desc
        default: true
      input2:
        type: boolean
        description: input 2 desc
        default: false
      input3:
        type: string
        description: input 3 desc

jobs:
  job1:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-company-short-ids.outputs.IDS_ARRAY }}
    steps:
      - name: Create array of ids
        id: set-ids
        run: |
          echo "IDS_ARRAY=$(echo '"${{ github.event.inputs.input3 }}"' | tr -d ' ' | jq -c 'split(",")')" >> $GITHUB_OUTPUT
          
  job2:
    if: ${{ github.event.inputs.input1 == true }}
    needs: job1
    strategy:
      fail-fast: false
      matrix:
        target: ${{ fromJSON(needs.job1.outputs.matrix) }}
    uses: ./.github/workflows/reusableWorkflow.yml
  
  job3:
    if: ${{ github.event.inputs.input2 == true }}
    needs: job1
    strategy:
      fail-fast: false
      matrix:
        target: ["once"]
    uses: ./.github/workflows/reusableWorkflow.yml

When I add a debug step to job1 I can see the values come thru true and false. Any ideas?


Solution

  • According to workflow_dispatch event:

    When the workflow runs, you can access the input values in the inputs context.

    The workflow will also receive the inputs in the github.event.inputs context. The information in the inputs context and github.event.inputs context is identical except that the inputs context preserves Boolean values as Booleans instead of converting them to strings. ...

    As you're using github.event.inputs context, the values are converted to strings. You need to compare against strings:

    if: ${{ github.event.inputs.input1 == 'true' }}
    

    or, use the inputs context:

    if: ${{ inputs.input1 == true }}
    

    Apart from that, you need to fix outputs in job1. The step id is not what you're using to set the output parameter. You may use https://rhysd.github.io/actionlint/ to lint your workflow for syntax issues.