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}}
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):
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With