I want to run my Github workflow two ways:
Now, everything was running fine until I added input parameters. After that, the cron job is running but not picking default value.
Here is my yaml:
name: WebDriverIO Automation
on:
workflow_dispatch:
inputs:
typeOfTesting:
type: choice
description: Select Type of Test
default: 'stage-test-local-All'
required: true
options:
- stage-test-local-All
- stage-test
- stage-test-local-Sanity
- prod-test
branches:
- workingBranch
- JSNew
schedule:
- cron: "*/5 * * * *"
inputs
are available to workflows triggered by the workflow_dispatch event only (and to any workflows that are called by dispatched workflows).
When a workflow with inputs is triggered on a schedule (or on push/pull) all the input values are null. Any defaults that are set in the inputs section have no effect.
Therefore you need to set the defaults at the point where they are needed in the workflow. A convenient method for doing this uses Github expressions.
For string and choice inputs you can use the ||
expression operator to set the default value.
For boolean inputs that default to false
you can use ||
expression to set the default value.
For boolean inputs that default to true
you can use the contains()
function to set the default value.
For example:
on:
schedule:
- cron: '15 0,18 * * 0-5'
workflow_dispatch:
inputs:
springProfile:
required: true
type: choice
options:
- staging
- production
logLevel:
required: true
type: string
isFalseWhenScheduled:
required: true
type: boolean
isTrueWhenScheduled:
required: true
type: boolean
jobs:
test:
uses: ./.github/workflows/run-job-with-params.yml
secrets: inherit
with:
springProfile: ${{ inputs.springProfile || 'staging' }}
logLevel: ${{ inputs.logLevel || 'DEBUG' }}
isFalseWhenScheduled: ${{ inputs.isFalseWhenScheduled || false }}
isTrueWhenScheduled: ${{ !contains(inputs.isTrueWhenScheduled, 'false') }}
In the above example, if the workflow is triggered on schedule:
springProfile
parameter is set to 'staging'.logLevel
parameter is set to 'DEBUG'.isFalseWhenScheduled
parameter is set to falseisTrueWhenScheduled
parameter is set to trueIf the job is triggered on workflow_dispatch:
then the above parameters are set to the inputs:
values.
The contains
function casts its first parameter to a string, and null
is cast to the empty string ''
. As mentioned, when triggered on schedule, inputs parameters are all set to null.
So:
Trigger method | isTrueWhenScheduled value | input value resolves to |
---|---|---|
workflow_dispatch | true | !contains('true', 'false') = true |
workflow_dispatch | false | !contains('false', 'false') = false (*) |
schedule | null | !contains('', 'false') = true |
(*) This combination is what necessitates the use of contains
. If contains is not used and you do ${{ inputs.isTrueWhenScheduled || true }}
this will force this value to true
even if it is set false
in the workflow dispatch event.
And:
Trigger method | isFalseWhenScheduled value | input value resolves to |
---|---|---|
workflow_dispatch | true | true || false = true |
workflow_dispatch | false | false || false = false |
schedule | null | null || false = false |
You can set an env variable with a default value:
env:
typeOfTesting: ${{ github.event_name == 'schedule' && 'stage-test-local-All' || github.event.inputs.typeOfTesting }}
then use env.typeOfTesting
instead of inputs.typeOfTesting
.
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