Sub topic 3
Declaring inputs and outputs
This chapter assumes you can read a workflow file (jobs, steps, uses, with) and write basic JavaScript and shell. It also assumes you know what an action.yml file is. Here you will learn how an action receives data from the workflow and hands results back.
The contract between workflow and action
Inputs and outputs are the whole public interface of an action. The inputs block in action.yml lists what callers may pass in. The outputs block lists what later steps may read. The runner connects the two sides, and the action code never needs to know which workflow called it.
Declaring inputs
Each input has a name and a few optional fields.
| Field | Purpose |
|---|---|
| `description` | Required by convention. Appears in documentation and in the Marketplace. |
| `required` | Marks the input as mandatory for callers. |
| `default` | Value used when the caller does not set the input. |
| `deprecationMessage` | A warning logged when a caller still uses the input. |
Here is an action.yml that uses all of them:
name: 'Greeter'
description: 'Build a greeting from the inputs'
inputs:
who-to-greet:
description: 'Who to greet'
required: true
shout:
description: 'Upper-case the greeting (true or false)'
required: false
default: 'false'
tags:
description: 'One tag per line'
required: false
default: ''
greeting-prefix:
description: 'Old name for who-to-greet'
required: false
deprecationMessage: 'Use who-to-greet instead.'
outputs:
greeting:
description: 'The final greeting text'
tag-count:
description: 'How many tags were supplied'
runs:
using: 'node20'
main: 'index.js'Inputs are always strings
The runner has no typed inputs. A YAML true or 5 in a with: block reaches the action as the text "true" or "5". Write defaults as quoted strings ('false') so the file shows what the action will actually see.
`required` is a promise, not a check
Do not rely on required: true to stop a run. The runner does not reliably fail a step when a required input is missing. Validate in your own code. In JavaScript, pass { required: true } to core.getInput, which throws when the value is empty. In shell, test the variable yourself.
Defaults and empty values
When the caller omits an input, the runner supplies the default. When the caller passes an empty string explicitly, the action receives an empty string. If an input has no default and is omitted, the value is empty too. Code that must tell "not supplied" from "supplied but empty" cannot do it through the standard helpers, so design inputs so that an empty string means "use the built-in behavior".
Reading inputs in a JavaScript action
The runner exposes each input as an environment variable named INPUT_ plus the input name in upper case, with spaces replaced by underscores. Hyphens stay. So who-to-greet becomes INPUT_WHO-TO-GREET. Most shells cannot reference a name containing a hyphen directly, which is one reason the toolkit helpers exist.
const core = require('@actions/core');
try {
const name = core.getInput('who-to-greet', { required: true });
const shout = core.getBooleanInput('shout');
const tags = core.getMultilineInput('tags');
const text = `Hello ${name}!`;
core.setOutput('greeting', shout ? text.toUpperCase() : text);
core.setOutput('tag-count', tags.length);
} catch (error) {
core.setFailed(error.message);
}The helpers differ in what they return:
core.getInput(name, options)returns a string. It trims leading and trailing whitespace unless you pass{ trimWhitespace: false }.core.getBooleanInput(name)accepts onlytrue,True,TRUE,false,False, andFALSE. Anything else throws aTypeError, which thecatchblock above turns into a failed step.core.getMultilineInput(name)splits on newlines and drops empty lines. An empty input yields an empty array.
Parse numbers yourself and check the result, because Number('abc') is NaN and will not throw:
const retries = Number.parseInt(core.getInput('retries'), 10);
if (Number.isNaN(retries) || retries < 0) {
throw new Error('retries must be a non-negative integer');
}Declaring outputs
For JavaScript and container actions, an output entry carries only a description. It does not carry a value. The declaration documents the name, and the action produces the value at run time. Composite actions are different: they add a value field that points at an inner step's output. That belongs to a later chapter.
An undeclared output can still be set and read in practice, but declaring every output keeps the interface honest and lets tooling and readers see it.
Setting outputs
Outputs travel through a file. The runner sets the environment variable GITHUB_OUTPUT to the path of a temporary file for each step. The action appends name=value lines to it. When the step ends, the runner reads the file and records the values.
core.setOutput(name, value) does that for you and converts non-string values to strings. From a shell entrypoint, append the line yourself:
#!/bin/sh -l
time=$(date)
echo "time=$time" >> "$GITHUB_OUTPUT"Multiline values
A plain name=value line ends at the newline, so values with newlines need a delimiter syntax. Write name<<DELIMITER, then the content, then the delimiter alone on a line:
#!/bin/sh
{
echo "report<<END_OF_REPORT_7f3a91"
cat report.txt
echo "END_OF_REPORT_7f3a91"
} >> "$GITHUB_OUTPUT"Choose a delimiter that cannot appear in the content. If the content comes from untrusted data, such as a pull request title, a fixed delimiter lets an attacker end the value early and add their own name=value lines. Generate a random delimiter for such content. core.setOutput handles delimiting for you.
Limits
Outputs are Unicode strings. Each output can be at most 1 MB, and all outputs in one workflow run together can total at most 50 MB. For larger results, write a file and return its path, or upload an artifact.
Reading outputs from a workflow
Give the step an id, then read steps.<id>.outputs.<name>:
name: Test greeter
on: [push]
jobs:
greet:
runs-on: ubuntu-latest
outputs:
message: ${{ steps.hello.outputs.greeting }}
steps:
- uses: actions/checkout@v4
- name: Run the local action
id: hello
uses: ./
with:
who-to-greet: 'Mona'
shout: 'true'
tags: |
alpha
beta
- name: Show outputs
run: |
echo "Greeting: ${{ steps.hello.outputs.greeting }}"
echo "Tags: ${{ steps.hello.outputs.tag-count }}"
consume:
needs: greet
runs-on: ubuntu-latest
steps:
- run: echo "${{ needs.greet.outputs.message }}"A step output is visible only inside its own job. To pass it to another job, map it under the job's outputs, as message does above, and read it through needs.<job>.outputs.
If a step does not have an id, you cannot reference its outputs. If the action never sets the output, the expression evaluates to an empty string and no error appears. A misspelled output name fails silently in the same way, so check names when a value looks blank.
Inputs in a container action
A Docker action receives inputs in two ways. You can pass them as arguments with args, as in the earlier Docker example: args: ['${{ inputs.who-to-greet }}']. The runner also sets the same INPUT_ environment variables inside the container. Environment variables are the safer route for user-controlled text, because an expression in args is substituted into the command line before the container starts. In a shell entrypoint, read a hyphenated input with printenv:
#!/bin/sh -l
name=$(printenv 'INPUT_WHO-TO-GREET')
echo "Hello ${name}"
echo "greeting=Hello ${name}" >> "$GITHUB_OUTPUT"Common errors
- Output is blank. The step has no
id, the output name is misspelled, or the action wrote toGITHUB_OUTPUTwith a typo in thename=prefix. - Boolean input throws. The caller passed
yesor1.getBooleanInputaccepts only the six spellings listed above. - Old
::set-outputcommands. Theecho "::set-output name=x::y"syntax is deprecated. Use theGITHUB_OUTPUTfile. - Required input missing, step still runs. Validate in code, as described above.
- Whitespace surprises.
getInputtrims, so a value of" x "arrives as"x". Disable trimming when spaces matter.
Exercise
A workflow passes retries: 3 to your action. Inside the JavaScript code, what type does core.getInput('retries') return, and what should you do before using it as a loop limit?
Show answer
It returns the string "3". Convert it with Number.parseInt(value, 10) and check for NaN and for negative values, throwing an error if the value is invalid so the step fails with a clear message.
Exercise
A shell script sets echo "title=$PR_TITLE" >> "$GITHUB_OUTPUT", where PR_TITLE comes from a pull request. Why is this unsafe, and how do you fix it?
Show answer
If the title contains a newline, the text after it is read as a new name=value line, so an attacker can set outputs that the action never intended. Use the multiline form title<<DELIM, with a randomly generated delimiter that cannot appear in the title, or set the output with core.setOutput in JavaScript.