Sub topic 2
Inputs: Declaring, Defaulting, and Reading Them
Assume you know GitHub Actions workflow syntax, YAML, Bash, and Git tags. A composite action is a reusable group of workflow steps. Its action.yml file defines the inputs callers may provide.
Declare and Read Inputs
Inputs arrive as strings, even when they look like numbers or booleans. Give each input a description and a default when the action can work without caller configuration.
| Field | Purpose |
|---|---|
| `description` | Documents the input |
| `required` | Rejects a call that omits the input |
| `default` | Supplies a value when none is provided |
This action creates a greeting, exposes it as an output, runs a bundled script, and calls another action:
name: Greet
description: Build and check a greeting
inputs:
who-to-greet:
description: Name to greet
required: false
default: World
outputs:
greeting:
description: The generated greeting
value: ${{ steps.build.outputs.greeting }}
runs:
using: composite
steps:
- uses: actions/setup-node@v4
with:
node-version: "20"
- id: build
shell: bash
env:
WHO: ${{ inputs.who-to-greet }}
run: echo "greeting=Hello, ${WHO}" >> "$GITHUB_OUTPUT"
- shell: bash
env:
ACTION_DIR: ${{ github.action_path }}
run: bash "$ACTION_DIR/scripts/check.sh"A step writes an output by appending name=value to the GITHUB_OUTPUT file. The action's outputs.greeting.value then maps that step output to the public action output.
Use the Action
A workflow must check out the repository before using a local action. The step id gives the workflow a name for reading action outputs.
- uses: actions/checkout@v4
- id: hello
uses: ./.github/actions/greet
with:
who-to-greet: Octocat
- run: echo "${{ steps.hello.outputs.greeting }}"For another repository, use its repository path and a ref such as a tag:
- uses: acme/greet-action@v1
id: hello
with:
who-to-greet: OctocatScripts, Safety, and Reuse
Store helper files below the action directory, such as scripts/check.sh. Use github.action_path to locate them. This works regardless of the repository that calls the action.
Pass untrusted inputs through env, then quote variables in shell commands. Do not insert an input directly into shell source. Treat secrets as sensitive inputs and avoid printing them.
Exercise
Why does the workflow need an id on the step that uses the composite action?
Show answer
The id provides the namespace, such as steps.hello.outputs.greeting, for reading outputs returned by that action.