I have a workflow with workflow_dispatch input default to an empty string.
The workflow uses a local github action having it's custom inputs with their respective default values:
# .github/workflow/ci.yml
on:
push:
- main
- wip/**
workflow_dispatch:
inputs:
myInput:
description: An input string
type: string
jobs:
myJob:
steps:
- name: My action step
uses: ./.github/actions/my-action
with:
myInput: ${{ github.event.inputs.myInput }}
# .github/actions/my-action/action.yml
name: 'My action'
description: 'An action'
inputs:
myInput:
description: An input
required: false
default: 'My default input'
runs:
steps:
- name: Run
shell: bash
run: echo Input: ${{ inputs.myInput }}
If the workflow is triggered by a push or if I manually start the workflow with an empty input, I would like the my-action action to keep its default input value My default input instead of the empty string.
Any idea if this is possible from the workflow, without tweaking the my-action action?
I would like the
my-actionaction to keep its default input valueMy default inputinstead of the empty string.
Unfortunately default: only works for workflow_dispatch. As you have noted, workflows triggered by push do not honour it.
One way to address this is by adding another step to action.yml, that sets an environment variable that defaults to the provided value when it is empty. Example:
# .github/actions/my-action/action.yml
name: 'My action'
description: 'An action'
inputs:
myInput:
description: An input
required: false
runs:
steps:
- name: Set environment variables
run: |
echo "MY_INPUT=${{ inputs.myInput || 'My default input' }}" >> $GITHUB_ENV
- name: Run
shell: bash
run: echo Input: ${{ env.MY_INPUT }}
# Or even:
- name: Run
shell: bash
run: echo Input: $MY_INPUT
Another way is to use GITHUB_OUTPUT (step outputs or job outputs) to achieve a similar result.
Although, note the disclaimer, you asked:
without tweaking the my-action action
...and this obviously tweaks the action, but in a way that the default is set for all workflow trigger kinds.
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