Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}}
like image 401
kittikun Avatar asked Dec 01 '25 16:12

kittikun


2 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 (links may lead to not found page through time):

  • workflow run with empty input value
  • workflow run with not empty input value
like image 126
GuiFalourd Avatar answered Dec 04 '25 21:12

GuiFalourd


You can also add a validation element to the variable:

on:
  workflow_dispatch:
    inputs:
      level:
        description: 'level to process'
        type: number
        required: false
        validation:
          pattern: '[0-9A-Za-z_]+'
          message: 'The level value must be populated'

This will avoid the verbosity of multiple if clauses.

like image 33
cptully Avatar answered Dec 04 '25 19:12

cptully