Dropdown for GitHub Workflows input parameters
Asked Answered
A

1

47

I want to create a dropdown list for my GitHub Action Input parameter. This should help in selecting a value from the dropdown just like how the option is there to select the branches.

Asti answered 23/9, 2021 at 8:13 Comment(2)
This is no such feature in GitHub actions yet.Intramundane
@MohammadDohadwala there is now.Thanks
W
102

When using workflow_dispatch, it's now possible to have choice, boolean and environment inputs instead of only just strings. choice is a dropdown, boolean is a checkbox and environment is like choice but will auto-populate with all environments configured in your repos settings.

Here's an example workflow using the new types:

name: CI

on:
  workflow_dispatch:
    inputs:
      environment:
        type: environment
        description: Select the environment
      boolean:
        type: boolean
        description: True or False
      choice:
        type: choice
        description: Make a choice
        options:
        - foo
        - bar
        - baz
jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: greet
        run: | 
          echo "environment is ${{ github.event.inputs.environment }} / ${{ inputs.environment }}"
          echo "boolean is ${{ github.event.inputs.boolean }}" / ${{ inputs.boolean }} 
          echo "choice is ${{ github.event.inputs.choice }}" / ${{ inputs.choice }}

Note that inputs can now be accessed directly using the inputs context, instead of using github.event.inputs.

example-of-the-workflow

Weatherly answered 10/11, 2021 at 21:10 Comment(11)
Perfect!! Thanks a lot :) Can I load/pass the parameters using the JSON file ?Asti
I'm actually unsure when variable substitution will happen in the workflows lifetime. So I'm not convinced that would work. But you should definitely give it a try!Weatherly
docs.github.com/en/actions/learn-github-actions/… do you know why github recommends using inputs.inputname and not github.event.inputs.inputname? Are they equivalent?Nisa
@walnut_salami Yes they are equivalent. This is because they unified the way you access inputs so it's the same across workflows triggered manually using workflow_dispatch and workflows called by another workflow using workflow_call. See this blog post for reference: github.blog/changelog/…Weatherly
What about populating the choice programmatically or another way to input in one of the steps?Measureless
@LuisMauricio as far as I know that is not possible.Weatherly
Is it somehow possible to have multiple values for a single option - an object maybe ? is this possible ?Aalesund
As far as I know that's not possible at the moment.Weatherly
inputs.inputname and not github.event.inputs.inputname are not always the same. For example, suppose a dispatched workflow calls an action. The workflow and the action has different inputs. When you reference inputs.inputname and github.event.inputs.inputname inside the aciton. inputs.inputname will the be the inputs of the action, and github.event.inputs.inputname will be the inputs of the worflow.Unify
@JonasLomholdt From where does the dropdown list for environment get populated? Mine shows an empty dropdown list.Dorris
@OldGeezer it's populated from the deployment environments that you define (docs.github.com/en/actions/deployment/…). Environments are only available in public repos and paid plans.Weatherly

© 2022 - 2024 — McMap. All rights reserved.