Sub topic 8
Failing an action clearly
This chapter assumes you can write a basic action.yml, have read inputs with @actions/core, and know how a workflow step passes or fails. It covers how an action reports failure, how to make the failure readable, and how the runner treats the result.
How the runner decides a step failed
The runner judges every step by its exit code. Exit code 0 means success. Any other code marks the step as failed, and by default the job stops running its remaining steps.
How you get a non-zero code depends on the action type:
| Action type | Mechanism | Result |
|---|---|---|
| JavaScript | `core.setFailed(message)` | Logs an error, sets exit code to 1 |
| JavaScript | Uncaught exception | Process exits non-zero, stack trace in log |
| Docker container | `exit 1` (or any non-zero code) in the entrypoint | Container exits non-zero, step fails |
| Docker container | Workflow command `::error::` | Annotation only, step still passes |
The last row is the common trap. An error message by itself does not fail a step. Only the exit code does.
Failing a JavaScript action
core.setFailed does two things. It writes an ::error:: message to the log, and it sets process.exitCode to 1. It does not stop your code, so statements after the call still run.
const core = require('@actions/core');
function run() {
const level = core.getInput('level');
if (!['low', 'high'].includes(level)) {
core.setFailed(`Input "level" must be "low" or "high", got "${level}".`);
return;
}
core.info(`Running at level ${level}`);
}
run();The explicit return matters. Without it, the code would log "Running at level" right after reporting the failure, which confuses readers of the log.
Catching errors
The standard pattern wraps the whole body in try/catch and passes the message to setFailed:
const core = require('@actions/core');
async function run() {
try {
const url = core.getInput('url', { required: true });
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Request to ${url} returned HTTP ${response.status}`);
}
core.setOutput('status', String(response.status));
} catch (error) {
core.setFailed(error instanceof Error ? error.message : String(error));
}
}
run();Two details are worth noting:
- In JavaScript, anything can be thrown, including strings and objects. The
instanceof Errorcheck keepssetFailedfrom receivingundefinedas a message. getInputwithrequired: truethrows its own error when the input is empty. That error is caught by the same block, so the user seesInput required and not supplied: url.
Unhandled promise rejections
If you call an async function and forget to await it or catch its rejection, Node 20 terminates the process with a non-zero code. The step fails, but the message is a raw stack trace rather than a clear error. Always catch at the top level of the action, as above.
Failing a container action
A container action has no toolkit unless you install one yourself. The shell entrypoint signals failure the way any program does, with its exit code. To make the message visible and attached to the run summary, print a workflow command first.
#!/bin/sh -l
if [ -z "$1" ]; then
echo "::error title=Missing input::who-to-greet must not be empty"
exit 1
fi
echo "Hello $1"The ::error:: line creates an annotation. The exit 1 fails the step. You need both for the best result.
A shell script keeps running after an error unless you tell it otherwise. Add set -e near the top to exit on the first failing command:
#!/bin/sh -l
set -e
curl --fail --silent --show-error "$1" > /dev/null
echo "Reached the URL"Without set -e, a failed curl would be ignored and the script would print the success message and exit 0. The --fail flag makes curl return a non-zero code on HTTP errors, which set -e then catches.
Error annotations
Annotations are messages the runner attaches to a run, and for files and lines, to the pull request diff. There are three levels:
errorshows a red marker and appears in the run summary.warningshows a yellow marker and does not change the exit code.noticeshows an informational marker.
In JavaScript, use core.error, core.warning, and core.notice. They accept an optional properties object:
const core = require('@actions/core');
core.error('Unused variable "count"', {
title: 'Lint failure',
file: 'src/app.js',
startLine: 12,
endLine: 12,
});In a container, the equivalent is a workflow command with parameters:
echo "::error file=src/app.js,line=12,title=Lint failure::Unused variable \"count\""Calling core.error does not fail the step. This is useful when you want to report many problems and then fail once at the end. A linter action, for example, can annotate every violation and call setFailed a single time with a count.
const core = require('@actions/core');
function report(problems) {
for (const p of problems) {
core.error(p.message, { file: p.file, startLine: p.line });
}
if (problems.length > 0) {
core.setFailed(`Found ${problems.length} problem(s).`);
}
}
report([
{ message: 'Missing semicolon', file: 'src/a.js', line: 3 },
{ message: 'Unused import', file: 'src/b.js', line: 1 },
]);Grouping and debug output
Long logs hide the useful part. Two features keep the failure visible.
core.startGroup(name)andcore.endGroup()collapse noisy output into a foldable section. Always close a group in afinallyblock so a thrown error cannot leave it open.core.debug(message)prints only when the repository secret or variableACTIONS_STEP_DEBUGis set totrue. Use it for details that help you diagnose a failure but would clutter normal runs.
const core = require('@actions/core');
async function run() {
core.startGroup('Fetching data');
try {
core.debug('Starting request');
throw new Error('Connection refused');
} finally {
core.endGroup();
}
}
run().catch((error) => core.setFailed(error.message));What the workflow author can do with a failure
Your action's failure is only the start. The workflow that uses it decides what happens next.
- By default, a failed step skips the later steps in the job.
continue-on-error: trueon the step lets the job continue. The step'soutcomeisfailure, but itsconclusionissuccess.if: failure()runs a step only after an earlier step failed.if: always()runs it regardless.
steps:
- id: check
uses: ./
continue-on-error: true
- name: Report
if: steps.check.outcome == 'failure'
run: echo "The check action failed, but the job goes on."Because workflow authors can do this, your action should stay honest. Fail when the work did not succeed, and do not hide real errors behind exit code 0.
Failure flow
The diagram shows the path from an error in your code to the final job result.
Writing messages that help
A good failure message answers three questions: what failed, why, and what the user can do.
- Name the input, file, or URL involved.
- State the observed value or status.
- Say what is expected or suggest a fix.
Compare these two messages:
- Poor:
Error: invalid - Better:
Input "timeout" must be a positive integer in seconds, got "-5".
Never print secrets in a message. Values registered as secrets are masked in logs, but a derived value, such as a substring or a decoded token, is not masked unless you call core.setSecret on it first.
Common mistakes
- Calling
core.errorand expecting the step to fail. It will not. - Using
console.error. It writes to the log but sets no failure and creates no annotation. - Calling
process.exit(1)inside an action. It works, but it cuts off pending output and skips cleanup code. PrefersetFailedand let the process end on its own. - Forgetting
exit 1in a container entrypoint after printing::error::. - Omitting
set -ein shell scripts, so a mid-script failure goes unnoticed. - Throwing inside a
postscript and hiding the main failure. Keep cleanup code defensive.
Exercises
Exercise
A JavaScript action calls core.error('Disk full') and then finishes normally. What is the step's result, and how do you fix the action so the step fails?
Show answer
The step passes with exit code 0, because core.error only creates an annotation. Call core.setFailed('Disk full') (or throw an error that is caught and passed to setFailed) so the exit code becomes 1.
Exercise
A Docker action's entrypoint runs curl https://example.com/missing and then echo "Done". The URL returns HTTP 404, yet the step succeeds. Give two changes that make the step fail.
Show answer
Add set -e so the script exits when a command fails, and add --fail to curl so it returns a non-zero code on HTTP errors. Alternatively, check the result explicitly with curl ... || exit 1.
Exercise
A workflow sets continue-on-error: true on your action's step and the action fails. What are the step's outcome and conclusion?
Show answer
The outcome is failure and the conclusion is success. The outcome is the raw result before continue-on-error is applied, and the conclusion is the result after.