Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use env variable as default value for input in github actions?

I have a github action that has an input which should have a default value based on an env.variable. Because github actions doesn't support env variables in the default field, I was wondering if I could reassign the inputs.variable in the steps portion of my action.yml file.

Here's what I've tried so far:

Doesn't work:

...
inputs:
  ...
  mono-build-tag:
    description: Release tag to download from the mono-build-repo
    # Github Actions complains that env is being used here
    default: "${{ env.GODOT_MONO_BUILD_TAG }}" 
runs:
  using: "composite"
  steps:
    - ...
  ...

Doesn't work:

...
inputs:
  ...
  mono-build-tag:
    description: Release tag to download from the mono-build-repo
    default: ""  
runs:
  using: "composite"
  steps:
    - name: Setup default inputs
      run: |
        if ${{ inputs.mono-build-tag == '' }}; then
          # How do I set inputs.mono-build-tag here???
        fi
  ...
like image 489
Atlinx Avatar asked Feb 16 '26 20:02

Atlinx


2 Answers

You could define the env variable as follow (remember to put string literal in single quotes):


env:
  GODOT_MONO_BUILD_TAG: ${{ github.event.inputs.mono-build-repo || 'latest' }}

where latest should be the default value for the env var

like image 109
Matteo Avatar answered Feb 19 '26 15:02

Matteo


The suggestion to use the env context is a good one although its use is limited - For example, I found that the env context can not be used for workflow or job names, only for step names:

on:
  push:
  workflow_dispatch:
    inputs:
      my-var:
        default: abc

env:
  TAG: ${{ inputs.my-var || 'abc' }}

run-name: My Workflow with variable [${{ env.MY_VAR }}]     << does not work

jobs:
  some-job:
    name: My JOB with variable [${{ env.MY_VAR }}]          << does not work
    steps:
      - name: My Step with variable [${{ env.MY_VAR }}]     << works!
    

We can use inputs for the workflow/job names but on push events, those values are empty :-(

An alternative is to use store defaults in Repository/Organization variables that can then be accessed from the vars context - i.e. you could do something like ${{ inputs.my-var || vars.DEFAULT_MY_VAR }}

If there are any other suggestions for using (default) variables regardless of the event type - please post them.

It would be awesome if a push event could inherit the default inputs from the workflow_dispatch event ...

More about contexts and how to use them here

like image 40
mverkerk Avatar answered Feb 19 '26 15:02

mverkerk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!