How to check if an optional workflow_dispatch input is set or not

Viewed 4016

I have an input defined as

  workflow_dispatch:
    inputs:
      level:
        description: 'level to process'
        type: number
        required: false

I would like to run named actions in the step depending on if the value is set or not but I cannot figure how

This might be working:

  if: ${{ github.event.inputs.level}}

But I cannot find the opposite version, this is invalid:

  if: !${{ github.event.inputs.level}}
1 Answers

I've tested it in this workflow and the correct way to check if an input (or any variable) is empty or not in an IF conditional is by using the following syntax:

if: "${{ github.event.inputs.<INPUT_NAME> != '' }}"

Note: using != "" will break the workflow as the interpreter doesn't accept this symbol in expressions.

Therefore, your workflow would look like this:

on:
  workflow_dispatch:
    inputs:
      level:
        description: 'level to process'
        type: number
        required: false

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Print variable if not empty
        if: "${{ github.event.inputs.level != '' }}"
        run: echo Level value is ${{ github.event.inputs.level }}

      - name: Print message if empty
        if: "${{ github.event.inputs.level == '' }}"
        run: echo Level value is empty

I've made two tests here if you want to check:

Related