Sub topic 10
Running steps conditionally
This chapter covers the if key on steps inside a composite action. It assumes you can write a basic action.yml with inputs and runs.using: composite, and that you know the workflow expression syntax ${{ ... }}. You should also know that every run step in a composite action needs a shell.
Why conditions matter in composite actions
A composite action is a fixed list of steps. Callers often want to change that list without writing a second action: skip the tests, install an optional tool, or upload a file only when a previous step produced one. The if key on a step covers these cases. When the condition evaluates to false, the runner skips the step and moves on to the next one.
A conditional step looks like this:
- if: ${{ inputs.run-tests == 'true' }}
shell: bash
run: npm testThe if key works on both run steps and uses steps. It also works together with id, env, and with.
Inputs are always strings
Action inputs are always strings, even when the caller writes a YAML boolean. If a workflow passes run-tests: true, the action receives the text true. The same applies to numbers. Because of this, compare inputs against strings:
if: ${{ inputs.run-tests == 'true' }}Writing if: ${{ inputs.run-tests }} is a common mistake. The value 'false' is a non-empty string, and in a boolean context GitHub treats a non-empty string as true in some situations. Compare explicitly and the behavior is predictable.
Missing inputs with no default are empty strings. To run a step only when the caller supplied a value, test for a non-empty string:
if: ${{ inputs.extra-args != '' }}Normalizing a boolean-like input
Callers may write true, True, or TRUE. String comparison with == in expressions is case-insensitive, so inputs.run-tests == 'true' matches all three. It does not match yes, 1, or on. Document the accepted values in the input's description, and fail early on values you do not accept (see the validation example below).
The expression syntax in `if`
The ${{ }} wrapper is optional in an if key at the workflow level, but it is a good habit to include it in composite actions. Be careful with one rule: if the expression starts with !, YAML would read it as a tag. Either wrap the expression in ${{ }} or quote the whole string.
- if: ${{ !cancelled() }}
shell: bash
run: echo "Still running"Useful pieces of the expression language for conditions:
| Item | Example | Meaning | Column | Column |
|---|---|---|---|---|
| Equality | `inputs.mode == 'fast'` | Case-insensitive string comparison | ||
| Logical operators | `a && b`, `a \ | \ | b`, `!a` | Combine conditions |
| `contains()` | `contains(inputs.flags, '--verbose')` | Substring or array membership test | ||
| `startsWith()` | `startsWith(github.ref, 'refs/tags/')` | Prefix test | ||
| `success()` | `success()` | True if no earlier step failed (the default) | ||
| `failure()` | `failure()` | True if an earlier step failed | ||
| `always()` | `always()` | True regardless of earlier results | ||
| `cancelled()` | `cancelled()` | True if the workflow was cancelled |
Conditions based on step results
Steps in a composite action can look at the outputs and outcomes of earlier steps in the same action through the steps context. Give the earlier step an id, then read from it.
name: Build and report
description: Build the project and print a hint when the build fails
inputs:
target:
description: Build target
required: false
default: release
runs:
using: composite
steps:
- id: build
shell: bash
env:
TARGET: ${{ inputs.target }}
run: make "$TARGET"
- if: ${{ failure() && steps.build.outcome == 'failure' }}
shell: bash
run: echo "::error::Build failed. Run 'make clean' and try again."
- if: ${{ always() }}
shell: bash
run: echo "Build step finished with outcome ${{ steps.build.outcome }}"Two points are worth knowing here:
- Without a status function, an
ifexpression gets an implicitsuccess()check. A step that hasif: ${{ inputs.mode == 'fast' }}will not run after a failure. To run a cleanup or reporting step after a failure, includealways()orfailure()explicitly. steps.<id>.outcomeis the result beforecontinue-on-erroris applied.steps.<id>.conclusionis the result after. Useoutcomewhen you want to know whether the command really failed.
Conditions based on step outputs
A step can write an output with $GITHUB_OUTPUT, and a later step can branch on it. This lets a composite action decide at run time, for example whether a cache hit means a build can be skipped.
name: Conditional install
description: Install tools only when a lock file exists
runs:
using: composite
steps:
- id: detect
shell: bash
run: |
if [ -f package-lock.json ]; then
echo "has-lock=true" >> "$GITHUB_OUTPUT"
else
echo "has-lock=false" >> "$GITHUB_OUTPUT"
fi
- if: ${{ steps.detect.outputs.has-lock == 'true' }}
shell: bash
run: npm ci
- if: ${{ steps.detect.outputs.has-lock != 'true' }}
shell: bash
run: npm installStep outputs are strings too, so compare with 'true'.
Conditions on the runner and event
The runner and github contexts are available in composite if expressions, so one action can adapt to the platform:
- if: ${{ runner.os == 'Windows' }}
shell: pwsh
run: Write-Host "Running on Windows"
- if: ${{ runner.os != 'Windows' }}
shell: bash
run: echo "Running on $RUNNER_OS"Note that the shell must still be named on each step, and the condition does not change that requirement. A step that is skipped still has to be valid, so a missing shell is an error even when the step would never run.
Event-based conditions work the same way:
- if: ${{ github.event_name == 'pull_request' }}
shell: bash
run: echo "Pull request run"Using `uses` steps conditionally
A step that calls another action can also be conditional. The with values are evaluated only when the step runs.
- if: ${{ inputs.setup-python == 'true' }}
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python-version }}This is the usual way to offer optional toolchains from a single action.
Validating inputs with a failing condition
A conditional step can also stop the action early when an input is unacceptable. This gives callers a clear error instead of an odd failure later.
name: Deploy helper
description: Deploy to a named environment
inputs:
environment:
description: One of staging or production
required: true
runs:
using: composite
steps:
- if: ${{ inputs.environment != 'staging' && inputs.environment != 'production' }}
shell: bash
env:
ENVIRONMENT: ${{ inputs.environment }}
run: |
echo "::error::environment must be staging or production, got: $ENVIRONMENT"
exit 1
- shell: bash
run: echo "Deploying"The error message reads the input through an environment variable, not by placing ${{ inputs.environment }} inside the script text. That keeps untrusted text out of the shell source, which a later chapter covers in full.
Common errors
- Comparing to a boolean.
inputs.flag == truecompares a string to a boolean, and the two are converted in ways that are easy to misjudge. Use== 'true'. - Forgetting the status function. A reporting step with only an input check will not run after a failure.
- Starting an expression with
!without${{ }}. YAML parses the value as a tag and the workflow fails to load. - Reading
envinif. Theenvcontext is not available in a composite step'sif. Useinputs,steps,github,runner, orjobvalues instead. - Expecting a skipped step to give an output. Outputs of a skipped step are empty strings. Test for emptiness before using them.
Flow of a conditional step
Practice
Exercise
A composite action has an input lint and the step if: ${{ inputs.lint }}. A caller passes lint: false. Why is this condition unreliable, and what should it be?
Show answer
Inputs are strings, so the action receives the text 'false', not a boolean. A non-empty string can evaluate as true in a boolean context, so the step may run. Compare explicitly: if: ${{ inputs.lint == 'true' }}.
Exercise
You want a step that uploads logs to run only when an earlier step with id test failed. The step currently has if: ${{ steps.test.outcome == 'failure' }} and never runs. What is missing?
Show answer
The implicit success() check skips the step once any earlier step has failed. Add a status function: if: ${{ failure() && steps.test.outcome == 'failure' }} or if: ${{ always() && steps.test.outcome == 'failure' }}.