Sub topic 8
Failing an action clearly
This chapter assumes you can write a basic workflow, read action.yml, and run a small Node script or shell script. You have already seen how an action reads inputs and sets outputs. Now you will make it report errors in a way that helps the person reading the logs.
What "failed" means for a step
The runner decides whether a step passed or failed from the process exit code.
- Exit code
0means success. - Any non-zero exit code means failure. The job stops, unless the step sets
continue-on-error: true.
A JavaScript action and a container action reach the same result by different routes. This chapter covers both.
Failing a JavaScript action
The toolkit provides core.setFailed. It does two things: it prints the message as an error annotation, and it sets process.exitCode to 1.
const core = require('@actions/core');
try {
const who = core.getInput('who-to-greet', { required: true });
if (who.trim() === '') {
throw new Error('who-to-greet must not be blank');
}
console.log(`Hello ${who}!`);
} catch (error) {
core.setFailed(`Greeting failed: ${error.message}`);
}Three points about this code:
required: truemakesgetInputthrow if the input is empty, so thecatchblock handles missing values too.setFaileddoes not stop your script. Code after it still runs, soreturnor exit the function yourself if later code depends on success.- Without a
try/catch, an uncaught exception also fails the step. The message is less tidy, because it arrives as a raw stack trace.
Error, warning, and notice
Not every problem should stop the job. The toolkit has lighter levels.
| Function | Effect on the step | Use it for |
|---|---|---|
| `core.setFailed(msg)` | Step fails, error annotation | Work that cannot continue |
| `core.error(msg)` | Annotation only, step still passes | A problem you want visible but not blocking |
| `core.warning(msg)` | Warning annotation | Deprecated usage, odd input |
| `core.notice(msg)` | Notice annotation | Useful information |
Under the hood these functions print workflow commands such as ::error::message to standard output. The runner parses those lines. That is why they also work from a shell script.
Failing a container action
A container action has no toolkit unless you install one. The shell entrypoint controls the result with its exit code. You can also print a workflow command so the message appears as an annotation.
#!/bin/sh -l
if [ -z "$1" ]; then
echo "::error::who-to-greet must not be blank"
exit 1
fi
echo "Hello $1"
echo "time=$(date)" >> "$GITHUB_OUTPUT"Here ::error:: creates the annotation and exit 1 fails the step. If you print the annotation but forget exit 1, the step still passes. The annotation shows in the summary, which can mislead readers into thinking the run was clean.
For longer scripts, add set -e near the top. The shell then exits on the first command that returns a non-zero code, so one failed command does not get hidden by later ones.
Writing good failure messages
A clear message answers three questions: what failed, why, and what the user can do.
- Name the input or file involved, for example
Input 'path' points to a missing file: dist/app.zip. - Give the cause in plain words, without a bare stack trace.
- Suggest a fix when you know one.
Never include secrets in a message. The runner masks registered secrets, but a value derived from one, such as a base64 copy, may not be masked.
Exercise
A container action's entrypoint prints ::error::Config file not found when a file is missing, but the step shows as passed. What is missing, and why does the step pass?
Show answer
The script never exits with a non-zero code. The runner decides pass or fail from the exit code, and the ::error:: command only adds an annotation. Adding exit 1 after the echo fails the step.
Exercise
In a JavaScript action, you call core.setFailed('bad input') inside a catch block, and the code after the try/catch still writes an output. Why does that happen, and how do you prevent it?
Show answer
setFailed only sets the exit code and writes the annotation. It does not terminate the process. Return from the function (or guard the later code) right after calling setFailed.