3

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:
    - name: Setup default inputs
      run: |
        if ${{ inputs.mono-build-repo == '' }}
  ...

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-repo == '' }}; then
          # How do I set inputs.mono-build-repo here???
        fi
  ...
Atlinx
  • 318
  • 3
  • 10
  • Would it be an option to use a reusable workflow here, or does it have to be an action? (using a reusable workflow could allow you to use the env variable as input in the main workflow, as I believe it's not possible to achieve what you want with action inputs) – GuiFalourd Oct 05 '22 at 12:39

3 Answers3

3

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

Matteo
  • 37,680
  • 11
  • 100
  • 115
3

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

Yash Patel
  • 31
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 19 '22 at 09:08
0

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

mverkerk
  • 31
  • 1
  • 2