Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep default action input if parameter passed is empty in github action

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?

like image 340
Alexper Avatar asked Dec 09 '25 20:12

Alexper


1 Answers

I would like the my-action action to keep its default input value My default input instead 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.

like image 130
thiagowfx Avatar answered Dec 13 '25 02:12

thiagowfx