Sub topic 4
Configuring actions with inputs and reading outputs
This chapter assumes you can read a workflow file (events, jobs, steps) and know how to reference an action with uses: owner/repo@ref. Here you will learn how to configure an action with inputs and how to read what it produces.
Inputs: the `with` keyword
An input is a named setting that an action accepts. You set inputs under the with key of a step. Each key under with must match an input name that the action defines in its action.yml file or in its README.
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ciIn this example, node-version selects the Node.js release, and cache turns on dependency caching for npm. Inputs that you leave out use the default that the action defines. Some inputs are required, and the step fails if you omit them.
Input values are always passed to the action as strings. The number 20 becomes the text "20". This matters for values such as 1.10, which YAML reads as the number 1.1. Quote version numbers like that:
with:
python-version: "3.10"Without quotes, 3.10 is parsed as 3.1, and the action installs the wrong version.
You can also use expressions in input values. An expression is written as ${{ ... }} and is evaluated before the step runs.
with:
key: npm-${{ runner.os }}-${{ hashFiles('**/package-lock.json') }}Outputs: passing results to later steps
An output is a named value that an action produces when it finishes. A later step in the same job can read it, but only if the producing step has an id. The id gives you a name to use in the steps context.
The syntax is steps.<id>.outputs.<name>.
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
id: npm-cache
with:
path: ~/.npm
key: npm-${{ runner.os }}-${{ hashFiles('**/package-lock.json') }}
- if: steps.npm-cache.outputs.cache-hit != 'true'
run: echo "Cache miss, dependencies will be downloaded"The cache step has id: npm-cache. The last step reads steps.npm-cache.outputs.cache-hit in an if condition. Outputs are strings too, so the comparison is against the text 'true', not a boolean.
You can use outputs in run commands as well. Expressions are substituted into the script text before the shell runs it.
- run: echo "Cache hit was ${{ steps.npm-cache.outputs.cache-hit }}"How data flows
The diagram shows how an input reaches an action and how an output reaches a later step.
Finding the right names
Input and output names are defined by each action, so you cannot guess them. Look in these places:
- The
inputs:andoutputs:sections of the action'saction.ymlfile. - The README, which usually lists them with examples.
- The Marketplace page, which shows the README.
Names are case-insensitive for inputs, but copy them exactly to avoid confusion.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Warning: "Unexpected input(s)" | A key under `with` does not match any input of the action. Often a typo or an input from another version. | Check the names in `action.yml` for the version you use. |
| Step fails with a "required input" message | A required input is missing. | Add it under `with`. |
| Output is empty | The step has no `id`, the `id` is misspelled, or the action does not set that output. | Check the `id` and the output name. |
| Wrong tool version installed | An unquoted version such as `3.10` was read as a number. | Quote the value. |
| Condition never matches | You compared an output to a boolean instead of a string. | Compare with `'true'`. |
Exercise
A step uses actions/setup-python@v5 with python-version: 3.10, and the job ends up with Python 3.1. Why does this happen, and how do you fix it? Also, what must you add to a step so that a later step can read its outputs?
Show answer
YAML parses the unquoted 3.10 as the number 3.1, so the action receives "3.1". Quote it as "3.10". To read outputs later, give the producing step an id and use steps.<id>.outputs.<name>.