Sub topic 3
Declaring inputs and outputs
This chapter assumes you know basic workflow syntax (jobs, steps, uses, with) and have seen an action.yml file. It covers how an action declares the values it accepts and returns, and how those values travel between a workflow and the action.
Inputs: what the action accepts
An input is a named value that a workflow passes to your action through with:. You declare each input under the inputs key in action.yml.
name: 'Hello World'
description: 'Greet someone and record the time'
inputs:
who-to-greet:
description: 'Who to greet'
required: true
default: 'World'
outputs:
time:
description: 'The time we greeted you'
runs:
using: 'node20'
main: 'index.js'Each input can have these fields:
| Field | Purpose |
|---|---|
| `description` | Explains the input to people reading the metadata. |
| `required` | Marks whether the workflow must supply a value. |
| `default` | The value used when the workflow omits the input. |
| `deprecationMessage` | A warning shown when someone still uses an old input. |
Two points often cause confusion:
- Every input arrives as a string. Booleans and numbers need to be parsed by your code. The string
'false'is truthy in JavaScript. required: trueis documentation more than enforcement. The runner does not reliably stop a workflow when a required input is missing, so your code should check for it.
Outputs: what the action returns
An output is a value that later steps can read. You declare it under outputs with a description. The declaration only documents the output. The value is set while the action runs.
The workflow reads it through the step's id:
name: Test hello action
on: [push]
jobs:
hello:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run the local action
id: hello
uses: ./
with:
who-to-greet: 'Mona'
- name: Show the output
run: echo "The time was ${{ steps.hello.outputs.time }}"The step needs an id. Without it, the expression steps.hello.outputs.time has nothing to refer to.
How the values move
For a JavaScript action, the runner exposes each input as an environment variable named INPUT_ plus the uppercased input name. @actions/core reads these for you:
const core = require('@actions/core');
const nameToGreet = core.getInput('who-to-greet', { required: true });
core.setOutput('time', new Date().toTimeString());Passing { required: true } makes getInput throw if the value is empty, which gives you the check that action.yml does not.
Common errors
- Hyphens versus underscores. Input names such as
who-to-greetare read with the same hyphenated name ingetInput. Do not rewrite them with underscores. - Forgetting the step
id. The output exists, but the workflow cannot reach it. - Undeclared outputs. An output set at run time but missing from
action.ymlmay still work, but it is undocumented and tools will not know about it. Always declare it. - Treating inputs as typed. Compare booleans with
core.getBooleanInputor against the string'true'.
Exercise
A workflow passes with: { dry-run: false } to your action. Your code runs if (core.getInput('dry-run')) { ... } and the block still executes. Why, and how do you fix it?
Show answer
Inputs are always strings, and the string 'false' is non-empty, so it is truthy. Use core.getBooleanInput('dry-run'), which parses 'true' and 'false' into real booleans. Alternatively, compare the string with === 'true'.