Sub topic 10
Passing inputs to a container and returning outputs
This chapter covers how data moves into and out of a Docker container action. It assumes you can read an action.yml file, know basic Dockerfile syntax, and can write a POSIX shell script. The earlier chapter on building a Docker container action covered the Dockerfile and the runs block. This chapter focuses on the data path.
How a container action receives data
A JavaScript action runs on the runner's Node runtime and reads inputs through @actions/core. A container action has no toolkit unless you install one in the image. The runner gives the container data through two channels:
- Command-line arguments, taken from the
argslist inaction.yml. - Environment variables, which the runner sets automatically for every input.
Outputs go back through a file. The runner sets the GITHUB_OUTPUT environment variable to a file path. The script appends name=value lines to that file. When the step ends, the runner reads the file and exposes each pair as steps.<id>.outputs.<name>.
Passing inputs with args
The args list in action.yml is the most explicit way to pass an input. You can use the inputs context inside it.
name: 'Hello World (Docker)'
description: 'Greet someone from a container'
inputs:
who-to-greet:
description: 'Who to greet'
required: true
default: 'World'
outputs:
time:
description: 'The time we greeted you'
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.who-to-greet }}The runner evaluates the expression and passes the result as the first argument to the container entrypoint. If you add more items to args, they arrive as $2, $3, and so on, in order.
The Dockerfile makes the script the entrypoint. Arguments from args are appended after it.
FROM alpine:3.20
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]Use the exec form (["/entrypoint.sh"]) as shown. In shell form, Docker wraps the command in /bin/sh -c, and the arguments are not passed to your script.
Passing inputs with environment variables
The runner also exposes every input as an environment variable. The name is INPUT_ followed by the input name in uppercase, with spaces replaced by underscores. Hyphens are kept. For an input called who-to-greet, the variable is INPUT_WHO-TO-GREET.
That name is not a valid shell identifier, so $INPUT_WHO-TO-GREET does not work in sh. You can read it with printenv or env:
#!/bin/sh -l
name=$(printenv 'INPUT_WHO-TO-GREET')
echo "Hello ${name}"This is why most container actions use args. Arguments are positional and need no special lookup. Environment variables are the better choice when an input contains data you do not want on a command line, or when an action has many inputs and numbered arguments become hard to follow.
| Approach | Where it is defined | How the script reads it | Best for |
|---|---|---|---|
| `args` | `runs.args` in `action.yml` | `$1`, `$2`, and so on | Few inputs, simple scripts |
| `INPUT_` variables | Set automatically by the runner | `printenv 'INPUT_NAME'` | Many inputs, optional inputs |
Returning outputs with GITHUB_OUTPUT
The entrypoint reads the greeting from its first argument and writes the output:
#!/bin/sh -l
echo "Hello $1"
time=$(date)
echo "time=$time" >> "$GITHUB_OUTPUT"Three details matter here:
- Append with
>>. Using>overwrites any pairs already written to the file, including ones written by the runner. - Quote
"$GITHUB_OUTPUT". The path is normally safe, but quoting is a good habit in shell scripts. - Declare every output in
action.ymlunderoutputs. The runner accepts undeclared names written to the file, but the declaration documents the action and is what users read.
The -l flag in the shebang makes the shell a login shell. The Alpine sh handles this fine. If you use a different base image, check that your shell accepts the flag.
Multi-line output values
A name=value line ends at the first newline. To return a value with several lines, use a delimiter:
#!/bin/sh
{
echo 'report<<EOF'
echo "Line one"
echo "Line two"
echo 'EOF'
} >> "$GITHUB_OUTPUT"The line report<<EOF starts the value, and the line EOF ends it. If the value comes from an untrusted source, choose a random delimiter. A value that contains the delimiter on its own line would end the output early, and the lines after it would be parsed as new outputs.
#!/bin/sh
delimiter="ghadelimiter_$(head -c 16 /dev/urandom | od -An -tx1 | tr -d ' \n')"
{
echo "report<<${delimiter}"
cat /tmp/report.txt
echo "${delimiter}"
} >> "$GITHUB_OUTPUT"Using the outputs in a workflow
A later step reads the output through the step id. Without an id, the step has no name to reference.
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 actions/checkout step is required for uses: ./. The local path refers to the repository workspace, and the workspace is empty until checkout runs.
Edge cases and common errors
Inputs and the container filesystem
A container action runs in its own filesystem, but the runner mounts the workspace and a few other directories into it. The working directory is set to the workspace (GITHUB_WORKSPACE). Files your script writes there persist for later steps. Files written elsewhere in the container are lost when the container exits.
The GITHUB_OUTPUT path points into a mounted location, so writing to it works. Do not copy the path into another file and expect it to remain valid after the step.
Empty and missing inputs
If an input is not required and the workflow does not supply it, the runner uses the default value. With no default, the argument is an empty string. An empty string still occupies a position in args, so $2 is empty rather than missing, and later positions do not shift. Check for emptiness explicitly:
#!/bin/sh
greeting="$1"
if [ -z "$greeting" ]; then
echo "::error::The who-to-greet input is empty"
exit 1
fi
echo "Hello ${greeting}"The ::error:: prefix is a workflow command. It creates an annotation on the run, and the non-zero exit status marks the step as failed.
Shell injection through inputs
An input is text chosen by the workflow author, and sometimes by an outside party, for example when the value is an issue title. If your script builds a command by pasting input into a string and running it with eval or sh -c, the input can run arbitrary commands. Pass the value as a quoted variable ("$1") and never as part of a command string.
Common mistakes
- Output is empty in the next step. The step has no
id, theidis misspelled, or the script wrote the line with>or to the wrong file. Check that you wrote to"$GITHUB_OUTPUT". - Script says
Permission denied. The entrypoint is not executable. Keep theRUN chmod +xline, or set the executable bit in Git withgit update-index --chmod=+x entrypoint.sh. exec format errorornot foundon startup. The script has Windows line endings, or the shebang points at a shell that the base image lacks. Alpine has/bin/shbut not/bin/bash.- The step runs but arguments are missing. The Dockerfile used shell-form
ENTRYPOINT, or theargslist is under the wrong key. - Container actions fail on Windows and macOS runners. Docker container actions run only on Linux runners. Use
runs-on: ubuntu-latestfor any job that calls one.
Summary of the data path
An input starts as a with: value in the workflow. The runner turns it into an INPUT_ environment variable and, if args lists it, a positional argument. The entrypoint reads it, does its work, and appends name=value lines to $GITHUB_OUTPUT. The runner reads that file after the container exits and makes the pairs available to later steps through steps.<id>.outputs.
Exercise
A container action declares the input target-env and passes ${{ inputs.target-env }} as the only item in args. Its entrypoint script must write an output named result with the value ok. Write the two shell lines that read the input into a variable called env_name and write the output. Then say what happens if the script uses > instead of >>.
Show answer
env_name="$1" and echo "result=ok" >> "$GITHUB_OUTPUT". With >, the script replaces the whole contents of the file, so any name=value pairs written to it earlier in the step are lost, and only the last write survives.
Exercise
A step uses a container action, and the next step runs echo "${{ steps.build.outputs.version }}" but prints an empty line. The script does write version=1.2.3 to $GITHUB_OUTPUT. List two causes to check in the workflow file.
Show answer
The action step may have no id: build, or its id may be spelled differently from the one in the expression. The step may also have failed or been skipped, in which case it sets no outputs. Another possibility is that the output name in the expression differs from the name written by the script.