Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schedule Trigger Github Action workflow with Input parameters

I want to run my Github workflow two ways:

  1. Manually by user
  2. Cron job

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 * * * *"
like image 617
user19089852 Avatar asked Sep 06 '25 03:09

user19089852


2 Answers

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:

  • the springProfile parameter is set to 'staging'.
  • the logLevel parameter is set to 'DEBUG'.
  • the isFalseWhenScheduled parameter is set to false
  • the isTrueWhenScheduled parameter is set to true

If the job is triggered on workflow_dispatch: then the above parameters are set to the inputs: values.

Boolean value defaults

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
like image 72
Joman68 Avatar answered Sep 08 '25 00:09

Joman68


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.

like image 37
oferei Avatar answered Sep 07 '25 23:09

oferei