Continue script on failure

I have a freestyle Jenkins job that has a simple bash script for a build.

In the bash script, there is a for loop which sometimes returns a non-zero return code.

Thing is, Jenkins quits immediately when it happens. However, if during the run it gets a non-zero return code, the job needs to continue, and in the end mark the job as failed. (In other words, don’t stop on failure but show the job failed when it’s finished). (that’s why I can’t just appent || true)

Is it possible to do? Thanks ahead!

Perhaps you could make the script create a marker file if the loop fails, then have another script check whether the marker file exists at the end of the build. And remove the marker file at the start of the build.

Despite the name, freestyle isn’t very free. It depends mostly on plugins. This is pretty trivial to do in pipelines, and recommend in the long term look into switching to pipelines.

I second @kon’s suggestion, a script like

EXITCODE=0
for i in a b c d; do 
  echo $i; 
  EXITCODE=$((EXITCODE + $?)); # add exit code to existing exit code
done;
exit $EXITCODE

would probably work. as jenkins doesn’t care what the code is, as long as its not 0

I recommend the seperate script, but you could do the above in jenkins by adding set +e which disables the exit on non zero exit code