Atlinx
Atlinx

Reputation: 594

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
  ...

Upvotes: 12

Views: 24245

Answers (3)

mverkerk
mverkerk

Reputation: 101

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

Upvotes: 7

Matteo
Matteo

Reputation: 39370

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

Upvotes: 21

Yash Patel
Yash Patel

Reputation: 51

If you were to use @Matteo's solution of creating an env variable with a default value I believe that default value would have to use single quotes as per the github actions expressions docs: "You don't need to enclose strings in ${{ and }}. However, if you do, you must use single quotes (') around the string. To use a literal single quote, escape the literal single quote using an additional single quote (''). Wrapping with double quotes (") will throw an error."

Upvotes: 3

Related Questions