Sub topic 10
Passing inputs to a container and returning outputs
This chapter assumes you can read a Dockerfile and action.yml, and that you know basic workflow syntax and POSIX shell. You will see how a Docker container action receives inputs and how it returns outputs to the workflow.
How data moves across the container boundary
A container action runs in its own container, so it cannot call @actions/core. Data crosses the boundary in two ways:
- Inputs arrive as command-line arguments (
args) or as environment variables. - Outputs leave through a file. The runner sets the
GITHUB_OUTPUTenvironment variable to the path of that file. The action appendsname=valuelines to it.
Passing inputs as arguments
In action.yml, the args list under runs takes expressions. Each entry becomes one argument to the container entrypoint.
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 Dockerfile copies the script in and makes it the entrypoint:
FROM alpine:3.20
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]Because the entrypoint uses the exec form, the runner's args are appended after it. The script sees the first one as $1.
Returning outputs
The entrypoint reads $1 and appends a line to the file named by GITHUB_OUTPUT:
#!/bin/sh -l
echo "Hello $1"
time=$(date)
echo "time=$time" >> "$GITHUB_OUTPUT"Use >>, not >. Overwriting the file would discard outputs that were already written. Every output you write must also be declared under outputs in action.yml, so users and tools can see what the action provides.
Reading the output in a workflow
Give the step an id. Other steps read its outputs through steps.<id>.outputs.<name>.
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 }}"uses: ./ needs the checkout step first. Without it, the runner finds no action.yml in the workspace.
Environment variables as an alternative
The runner also exposes every input as an environment variable named INPUT_ plus the uppercased input name. Spaces become underscores, but hyphens stay. The input who-to-greet becomes INPUT_WHO-TO-GREET. Most shells cannot reference a name with a hyphen directly, so arguments are usually easier in shell scripts.
Common errors
| Symptom | Likely cause |
|---|---|
| `$1` is empty | The input is missing from `args`, or the entrypoint uses shell form and drops the arguments |
| Output is empty in the workflow | The script wrote to `>` instead of `>>`, or the output name differs from the one in `steps.id.outputs` |
| `Permission denied` on start | The script lacks the executable bit. Keep `RUN chmod +x` in the Dockerfile |
| Multi-line output breaks | A value with newlines needs the heredoc form of `GITHUB_OUTPUT` (`name<<EOF`) |
Exercise
Your container action runs, but steps.hello.outputs.time is empty in the next step. The script ends with echo "time=$time" > "$GITHUB_OUTPUT". List two things to check.
Show answer
First, check that the script appends with >> rather than overwriting with >. Overwriting can drop earlier outputs. Second, check that the step has id: hello and that the output name time matches both the script and the outputs section of action.yml.