Sub topic 3
Running Steps Conditionally in Composite Actions
Assume you know workflow jobs, steps, uses, run, YAML, Bash, Git tags, and repositories. A composite action runs inside one workflow job, so its conditions control individual steps.
Running steps conditionally
Inputs are strings, even when they contain values such as true. Compare them as strings:
name: Test optionally
description: Run checks when requested
inputs:
run-tests:
description: Set to true to run tests
required: false
default: "true"
runs:
using: composite
steps:
- if: ${{ inputs.run-tests == 'true' }}
shell: bash
run: npm test
- if: ${{ inputs.run-tests != 'true' }}
shell: bash
run: echo "Tests were skipped"Avoiding script injection from inputs
Never insert an input directly into a shell command. Put it in the step environment and quote the variable:
- shell: bash
env:
MESSAGE: ${{ inputs.message }}
run: printf '%s\n' "$MESSAGE"This prevents shell characters in message from becoming commands.
Working with secrets
A composite action does not automatically receive the caller's secrets. Pass only the required secret as an input or environment variable. Do not print it, include it in command output, or write it to artifacts.
Testing the action in its own repository
Create a workflow that checks out the repository and uses the local action path:
name: Test composite action
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/test
with:
run-tests: "false"Test both branches and representative input values.
Debugging and log output
Use clear step names and safe status messages. For more workflow logging, enable the repository secret ACTIONS_STEP_DEBUG with value true. Never enable debugging while exposing secret values.
Versioning and releasing for other repositories
After testing, create a release tag such as v1.0.0, then move a major tag such as v1 when making compatible releases. Consumers can use owner/repository/path@v1. Pinning to a full commit SHA gives stronger supply-chain protection but requires manual updates.
Limitations and choosing the right tool
| Need | Suitable choice |
|---|---|
| Reusable steps in one job | Composite action |
| Multiple jobs or matrices | Reusable workflow |
| Packaged language logic | JavaScript action |
| Custom runtime or system packages | Docker action |
Composite actions cannot define jobs, permissions, or services. Choose a reusable workflow when the reusable unit needs job-level control.
Exercise
Why should an input be passed through an environment variable before a shell command uses it?
Show answer
Quoting the environment variable keeps input characters from being interpreted as shell syntax.