Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script in gitlab-ci does not output errors

I have a shell script that I use on gitlab CI that runs a supplied command. The issue is it doesn't display errors if the command failed, it just says ERROR: Job failed: exit code 1. I tried the script locally and it was able to output the failed command, is there a way I could somehow force it to display the error through my script before it exits the job?

The particular part of my script

output="$( (cd "$FOLDER"; eval "$@") 2>&1 )"
if [ "$output" ]; then
    echo -e "$output\n"
fi
like image 707
uLan Avatar asked Jul 12 '26 09:07

uLan


1 Answers

One way to trap an error that works in every shell is to combine two commands with logical OR ||.

Catch error from subshell:

output="$( (cd "$FOLDER"; eval "$@") 2>&1 )" || errorcode="$?"

will save the error code from the previous command if it fails.

Exit with own error code if important command fails

output="$( (cd "$FOLDER"; eval "$@") 2>&1 )" || exit 12

For more complex things one can define a function that will be called after the OR.

handle_error() {
# do stuff
}

output="$( (cd "$FOLDER"; eval "$@") 2>&1 )" || handle_error
like image 125
rkta Avatar answered Jul 14 '26 02:07

rkta