How can you use expressions as the workflow_dispatch input default?

This appears to work:

name: Production Build

env:
  DEFAULT_DEPLOYMENT_PREFIX: "mainy"
on:
  schedule:
    # * is a special character in YAML so you have to quote this string
    - cron: "0 */24 * * *"

  workflow_dispatch:
    inputs:
      deployment_prefix:
        description: "Deployment prefix"
        required: false
        default: ${DEFAULT_DEPLOYMENT_PREFIX}


jobs:
  build:
    runs-on: ubuntu-latest

      - name: DEBIGGG
        run: |
          echo ${{ github.event.inputs.deployment_prefix }}
          echo ${{ env.DEFAULT_DEPLOYMENT_PREFIX }}

When I run that, from a workflow dispatch, it seems to work.

This line:

echo ${{ github.event.inputs.deployment_prefix }}

echos out the default that I set in that env.DEFAULT_DEPLOYMENT_PREFIX. Cool.

The only “ugly” thing is that it’s a bit weird looking in the UI:

Screen Shot 2020-11-03 at 2.43.42 PM

Now, when the jobs run, it doesn’t matter if it was triggered manually (workflow_dispatch) or if it was run by the cron.

It’s not terrible, but it would be cool if the default input could display the string mainy.

Is this possible to fix?

I suppose you could default to the workflow-level env value if no input is set:

name: Production Build

env:
  DEFAULT_DEPLOYMENT_PREFIX: mainy

on:
  schedule:
    # * is a special character in YAML so you have to quote this string
    - cron: "0 */24 * * *"

  workflow_dispatch:
    inputs:
      deployment_prefix:
        description: Deployment prefix
        required: false
        default: mainy

jobs:
  build:
    runs-on: ubuntu-latest

      - name: DEBIGGG
        run: |
          echo "${{ github.event.inputs.deployment_prefix || env.DEFAULT_DEPLOYMENT_PREFIX }}"

But that means you have to express the default value twice. And my above-mentioned solution does mean I can use just ${{ github.event.inputs.deployment_prefix }} instead of the or construct.

But with your solution you need to call the echo twice, that is not always ideal. I think another way might be to move the “or” into the env definition? like this:

name: Production Build

on:
  schedule:
    # * is a special character in YAML so you have to quote this string
    - cron: "0 */24 * * *"

  workflow_dispatch:
    inputs:
      deployment_prefix:
        description: Deployment prefix
        required: false
        default: mainy

env:
  DEPLOYMENT_PREFIX: ${{ github.event.inputs.deployment_prefix || mainy }}

jobs:
  build:
    runs-on: ubuntu-latest

      - name: DEBIGGG
        run: |
          echo "${{ env.DEPLOYMENT_PREFIX }}"

You don’t have to set it to the same value. You could let it default to an empty string. It depends on the desired semantics. Do you want to define the default once and use it for both Workflow dispatch and scheduled runs?

I wasn’t aware that was an option. Thanks.
It still doesn’t solve the problem of having to type in the defaults twice. (In my real project, we currently have 5 questions (5 defaults) in the workflow_dispatch and I want to share all those defaults between workflow_dispatch and schedule.

If it can’t be done, I’ll have to make a decision:

  1. Type the defaults twice (in two different places)
  2. Accept my above-mentioned solution where the inputs look like my attached screenshot.

Do you want to define the default once and use it for both Workflow dispatch and scheduled runs?

Yes. That’s the ultimate goal.

I like how you can write a web app where you use the default value as the initial input and it can come from the server:

# server.py
DEFAULT_DEPLOYMENT_PREFIX = getDefaultDeploymentPrefix()

# template code 
<input type="text" name="DEPLOYMENT_PREFIX" value="{{ DEFAULT_DEPLOYMENT_PREFIX }}">

It might seem like I’m being pedantic and “lazy” but I have more than just 1 input and DRY of defaults is dangerous because it’s so easy that a future me or another developer accidentally misses this DRY and only changes one and doesn’t notice why it’s not working.