Workflow_dispatch and schedule with inputs

For the workflow_dispatch I defined some inputs, and would like the schedule to run with the same defaults, but the {{ github.events.inputs.(field) }} is empty.

Is there any way to make these two events use the same data?

name: Some Workflow

on:
  schedule:
    - cron:  '0 4 3 * *'
  workflow_dispatch:
    inputs:
      an_input:
        description: 'Input?'
        required: true
        default: 'Foo'
jobs:
  build:
  # Do something with ${{ github.event.inputs.an_input }}

(updated to better express the ask)

1 Like

Should they both always use the same values?

https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs

So you’ve already got something like:

name: Some Workflow

on:
  workflow_dispatch:
    inputs:
      an_input:
        description: 'Input?'
        required: true
        default: 'Foo'
jobs:
  build:
  # Do something with ${{ github.event.inputs.an_input }}
name: Some Workflow

on:
  schedule:
    - cron:  '0 4 3 * *'
    inputs:
      an_input:
        description: 'Input?'
        required: true
        default: 'Foo'
  workflow_dispatch:
    inputs:
      an_input:
        description: 'Input?'
        required: true
        default: 'Foo'
jobs:
  build:
  # Do something with ${{ github.event.inputs.an_input }}

I haven’t tested but I suspect that you can likely also do something like:

name: Some Workflow

inputs:
  an_input:
    description: 'Input?'
    required: true
    default: 'Foo'

on:
  schedule:
    - cron:  '0 4 3 * *'
  workflow_dispatch:

jobs:
  build:
  # Do something with ${{ github.event.inputs.an_input }}

I haven’t tried either of the later two since the documentation doesn’t mention that as an option. I’ve barely even seen a reference to the original workflow_dispatch.

I guess I could try them as an option

Okay, so I just tried both of those recommendations and neither of them are valid syntax for the actions file. So unless there is some other way to do this, I guess the best option is to do two different workflows that do exactly the same thing but have different triggers.

I’ll do some testing tomorrow. Hopefully I just bungled something silly.

Hi @ajohnson-ventera Thanks for your feedback.

Seems to be impossible with my knowledge. Different event names correspond to different Webhook event payloads.
If the work flow is triggered by workflow_dispatch
this Webhook payload will be appended in the Github Context. So you can use the ${{ github.event.inputs.an_input }} in your work flow.


here is the log to show the the Github Context demonstrating this.

If the work flow is triggered by schedule
There is no Webhook payload(only “schedule”: “*/15 * * * *”) can be attached to Github Context. So this is why you cannot use the {{ github.events.inputs.(field) }}

If the values are static, then you could just use env and do something like:

name: Test2

on:
  schedule:
    - cron:  '*/5 * * * *'
  workflow_dispatch:

jobs:
  Test2:
    env:
      AN_INPUT: Foo
    name: Test2
    runs-on: ubuntu-latest
    steps:
    - name: Test2
      run: |
        echo $AN_INPUT
        exit 0

If you want workflow_dispatch to be able to override the values you could do something like:

name: Test2

on:
  schedule:
    - cron:  '*/5 * * * *'
  workflow_dispatch:
    inputs:
      an_input:
        description: 'Input?'
jobs:
  Test2:
    env:
      AN_INPUT: Foo
    name: Test2
    runs-on: ubuntu-latest
    steps:
    - name: Step 1
      if: github.event.inputs.an_input != ''
      run: echo "::set-env name=AN_INPUT::${{ github.event.inputs.an_input }}"
    - name: Step 2
      run: |
        echo $AN_INPUT
        exit 0

So if the input isn’t empty then it overrides the env variable.

See How can you use expressions as the workflow_dispatch input default?

It seems to work. I only have to type the default value 1 time and it can be overridden.
It’s just a bit weird looking in the UI for manually triggering.

I had a similar scenario where I wanted to reuse the same workflow for push to master as well as a manual workflow_dispatch. The solution I came up with:

on:
  workflow_dispatch:
    inputs:
      environment:
        description: 'Which environment to deploy to [dev|qa|prod].'
        default: dev
        required: true
      git_reference:
        description: 'Which Git reference to check out. Examples: "refs/tags/v0.2.1", "refs/heads/website/feature_branch" "refs/heads/master".'
        default: refs/tags/vX.Y.Z
        required: true
  push:
    paths:
      - 'my-project-subdirectory/**'
    branches:
      - master

jobs:
  deploy:
    name: Deploy my project

    runs-on:
      - ubuntu-20.04

    steps:
      - name: Set variables
        env:
          DEFAULT_ENVIRONMENT: dev
          DEFAULT_GIT_REFERENCE: refs/heads/master
        run: |
          echo "GIT_REFERENCE=${{ github.event.inputs.git_reference || env.DEFAULT_GIT_REFERENCE }}" >> $GITHUB_ENV
          echo "ENVIRONMENT=${{ github.event.inputs.environment || env.DEFAULT_ENVIRONMENT }}" >> $GITHUB_ENV

      - name: Checkout repository at ${{ env.GIT_REFERENCE }}
        uses: actions/checkout@v2
        with:
          ref: ${{ env.GIT_REFERENCE }}
          fetch-depth: 0

      - name: Build and deploy to ${{ env.ENVIRONMENT }}
        ...