don't fail jenkins build if execute shell fails
As part of my build process, I am running a git commit as an execute shell step. However, if there are no changes in the workspace, Jenkins is failing the build. This is because git is returning an error code when there are no changes to commit. I'd like to either abort the build, or just mark it as unstable if this is the case. Any ideas?
Solution 1:
To stop further execution when command fails:
command || exit 0
To continue execution when command fails:
command || true
Solution 2:
Jenkins is executing shell build steps using /bin/sh -xe
by default. -x
means to print every command executed. -e
means to exit with failure if any of the commands in the script failed.
So I think what happened in your case is your git command exit with 1, and because of the default -e
param, the shell picks up the non-0 exit code, ignores the rest of the script and marks the step as a failure. We can confirm this if you can post your build step script here.
If that's the case, you can try to put #!/bin/sh
so that the script will be executed without option; or do a set +e
or anything similar on top of the build step to override this behavior.
Edited: Another thing to note is that, if the last command in your shell script returns non-0 code, the whole build step will still be marked as fail even with this setup. In this case, you can simply put a true
command at the end to avoid that.
Another related question
Solution 3:
If there is nothing to push git returns exit status 1. Execute shell build step is marked as failed respectively. You can use OR statement || (double pipe).
git commit -m 'some messasge' || echo 'Commit failed. There is probably nothing to commit.'
That means, execute second argument if first failed (returned exit status > 0). Second command always returns 0. When there is nothing to push (exit status 1 -> execute second command) echo will return 0 and build step continues.
To mark build as unstable you can use post-build step Jenkins Text Finder. It can go through console output, match pattern (your echo) and mark build as unstable.
Solution 4:
There is another smooth way to tell Jenkins not to fail. You can isolate your commit in a build step and set the shell to not fail:
set +e
git commit -m "Bla."
set -e