How to Validate Inputs in a Custom Action
Since the runner does not enforce input formats, validate them in the action and fail fast on bad values.
Check inputs against allowed values or a pattern at the start of the action. Fail with ::error:: plus exit 1 in a composite step, or core.setFailed in JavaScript.
Steps
- Read the input early, before any side effects.
- Compare against an allowlist or a regex.
- Fail with a clear annotation on a bad value.
Composite validation
action.yml
inputs:
level:
description: 'Log level'
default: 'info'
runs:
using: "composite"
steps:
- run: |
case "${{ inputs.level }}" in
debug|info|warn|error) ;;
*) echo "::error::level must be debug, info, warn, or error"; exit 1 ;;
esac
shell: bashJavaScript validation
index.js
const allowed = ['debug', 'info', 'warn', 'error'];
const level = core.getInput('level') || 'info';
if (!allowed.includes(level)) {
core.setFailed(`level must be one of ${allowed.join(', ')}`);
}Gotchas
- Validate before side effects so a bad input never leaves partial state.
- The action.yml
typefield for inputs is not enforced by the runner; validate yourself.
Related guides
How to Mark an Input Required in a Custom ActionMark a custom action input as required in action.yml and validate it at runtime, since the runner does not ha…
How to Set Default Input Values in a Custom ActionGive a custom action input a default with the default key in action.yml, so callers can omit it and still get…
How to Emit Annotations From a Custom ActionEmit error, warning, and notice annotations from a custom action with the ::error:: workflow commands or the…