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

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}}


Solution 1:[1]

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:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1