Sub topic 10
Running steps conditionally
This chapter assumes you can read workflow YAML, know what uses and run do, and have written a basic action.yml with inputs. It covers the if key inside a composite action and the mistakes that most often break it.
How `if` works in a composite action
Each step in a composite action can have an if key. When the expression evaluates to false, GitHub skips the step. Other steps still run.
The expression can read the inputs, github, env, steps, and runner contexts. It can also use status functions such as success(), failure(), always() and cancelled().
Inside if, you may leave out ${{ }}. Writing if: ${{ inputs.run-tests == 'true' }} and if: inputs.run-tests == 'true' behave the same. This chapter keeps the braces to match the earlier examples.
Inputs are strings
Every input arrives as a string, even when the caller writes a YAML boolean. A caller who writes run-tests: false passes the string 'false'. A non-empty string is truthy, so this check is wrong:
- if: ${{ inputs.run-tests }}
shell: bash
run: npm testThe step runs when the input is 'false'. Compare against the string instead:
- if: ${{ inputs.run-tests == 'true' }}
shell: bash
run: npm testChoose one convention for your action, such as 'true' and 'false', and document it in the input description.
A complete example
This action installs Node.js and dependencies, then runs tests only when asked. It also uploads a log file if something fails.
name: Node setup and test
description: Install Node.js, install dependencies and optionally run tests
inputs:
node-version:
description: Node.js version
required: false
default: "20"
run-tests:
description: Set to 'true' to run the test suite
required: false
default: "true"
runs:
using: composite
steps:
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
cache: npm
- shell: bash
run: npm ci
- if: ${{ inputs.run-tests == 'true' }}
shell: bash
run: npm test
- if: ${{ failure() }}
shell: bash
run: echo "A step in this action failed."Only the third step depends on an input. The fourth step uses failure(), which is true when an earlier step in the job has failed.
Status functions
Without a status function, a step has an implicit success() check. If an earlier step failed, later steps are skipped. Adding failure() or always() changes that.
| Expression | Step runs when |
|---|---|
| no `if` | all previous steps succeeded |
| `success()` | all previous steps succeeded |
| `failure()` | a previous step failed |
| `always()` | regardless of earlier results, unless the run was cancelled before this point is reached by a cancellation request taking effect |
| `cancelled()` | the workflow was cancelled |
Note that failure() looks at the whole job, not only at steps inside your action. A caller's earlier step that failed also makes it true.
Branching on step outputs
A step can decide whether later steps run. Write a value to $GITHUB_OUTPUT, then test it:
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 ciCommon errors
- Truthy strings. Using
if: ${{ inputs.flag }}without a comparison, as shown above. - Missing
shell. A skippedrunstep still needsshell. The action fails validation without it. - Mixing text and expressions.
if: Run when ${{ inputs.x }}is always truthy, because the result is a non-empty string. Keep the whole condition as one expression. - Wrong context. Use
inputs.name, notgithub.event.inputs.name, to read a composite action's inputs.
Exercise
A caller passes run-tests: false. The action contains the step - if: ${{ inputs.run-tests }} followed by run: npm test. Does the tests step run, and how should you fix the condition?
Show answer
Yes, it runs. The input is the string 'false', which is non-empty and therefore truthy. Change the condition to if: ${{ inputs.run-tests == 'true' }}.