Do not compromise jenkins collection if shell fails

As part of my build process, I run git commit as an execution shell step. However, if there are no changes in the workspace, Jenkins will not build. This is because git returns an error code when there are no changes to commit. I would like to either interrupt the assembly, or simply mark it as unstable, if so. Any ideas?

+58
jenkins
Jan 18 '13 at 4:22
source share
10 answers

I managed to get this working using the answer found here:

How to fix git without errors?

git diff --quiet --exit-code --cached || git commit -m 'bla' 
+6
Jan 18 '13 at 18:06
source share

To stop further execution when a command fails:

command || exit 0

To continue execution when the command failed:

command || true

+94
Sep 09 '14 at 13:13
source share

Jenkins follows the shell assembly steps using /bin/sh -xe by default. -x means printing every command executed. -e means exit with an error if any of the commands in the script failed.

So, I think in your case it was that you ended the git command with 1, and because of the -e option, the default shell gets an exit code other than 0, ignores the rest of the script and marks this step as a failure. We can confirm this if you can post your build step script here.

In this case, you can try setting #!/bin/sh so that the script runs without an option; or do set +e or something similar at the 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 a non-0 code , the entire build step will still be marked as a failure even with this installation. In this case, you can simply put the echo command at the end to avoid this.

Another related question

+45
Feb 11 '15 at 7:00
source share

If you do not click anything, then git returns the completion status of 1. The execution of the shell assembly phase is marked as unsuccessful, respectively. You can use the OR || (double pipe).

 git commit -m 'some messasge' || echo 'Commit failed. There is probably nothing to commit.' 

This means that execute the second argument if the first fails (completion status returned> 0). The second command always returns 0. If you press nothing (exit state 1 β†’ execute the second command), the echo will return 0, and the build step will continue.

To mark an assembly as unstable, you can use the Jenkins Text Finder post-assembly. It can go through console output, a matching pattern (your echo), and create labels as unstable.

+33
Oct 17 '13 at 10:37
source share

Jenkins determines the success / failure of a step by the return value of the step. For the shell case, this should be the return of the last value. For Windows CMD shells and (POSIX) Bash, you should be able to set the return value manually, using exit 0 as the last command.

+7
Jan 18 '13 at 14:55
source share

In the (more general) question in the header - to prevent Jenkins from crashing, you can prevent him from seeing exit code 1. Example for ping:

 bash -c "ping 1.2.3.9999 -c 1; exit 0" 

And now you can, for example, get ping output:

 output=`bash -c "ping 1.2.3.9999 -c 1; exit 0"` 

Of course, instead of ping ... you can use any command - including git commit .

+6
Nov 20 '13 at 11:05
source share

You can use the plugin to search for text . This will allow you to check the output console for the expression of your choice, and then mark the assembly as Unstable .

+5
Jan 18 '13 at 9:11
source share

There is another smooth way to tell Jenkins not to fail. You can isolate the commit at the build stage and not run the shell:

 set +e git commit -m "Bla." set -e 
+5
Jun 04
source share

The following steps for mercurial are only performed if there are changes. Thus, the assembly fails if the commit fails.

 hg id | grep "+" || exit 0 hg commit -m "scheduled commit" 
+2
Feb 05 '13 at 18:18
source share

If you put these commands in a shell:

 false true 

your assembly will be marked as crashing (at least 1 non-zero exit code), so you can add (set + e) ​​to ignore it:

 set +e false true 

won't let you down. However, this will not work even with (set + e) ​​in place:

 set +e false 

because the last shell command should exit from 0.

+1
Nov 30 '16 at 19:35
source share



All Articles