don't fail jenkins build if execute shell fails

前端 未结 14 1149
无人共我
无人共我 2020-12-04 08:08

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 be

相关标签:
14条回答
  • 2020-12-04 08:31

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

    0 讨论(0)
  • 2020-12-04 08:32

    The following works for mercurial by only committing if there are changes. So the build only fails if the commit fails.

    hg id | grep "+" || exit 0
    hg commit -m "scheduled commit"
    
    0 讨论(0)
  • 2020-12-04 08:34

    If you put this commands into shell block:

    false
    true
    

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

    set +e
    false
    true
    

    will not fail. However, this will fail even with the (set +e) in place:

    set +e
    false
    

    because the last shell command must exit with 0.

    0 讨论(0)
  • 2020-12-04 08:34

    Another one answer with some tips, can be helpful for somebody:

    remember to separate your commands with the following rule:

    command1 && command2 - means, that command2 will be executed, only if command1 success

    command1 ; command2 - means, that command 2 will be executed despite on result of command1

    for example:

    String run_tests = sh(script: "set +e && cd ~/development/tests/ && gmake test ;set -e;echo 0 ", returnStdout: true).trim()
    println run_tests 
    

    will be executed successfully with set -e and echo 0 commands if gmake test failed (your tests failed), while the following code snipped:

    String run_tests = sh(script: "set +e && cd ~/development/tests/ && gmake test && set -e && echo 0 ", returnStdout: true).trim()
    println run_tests 
    

    a bit wrong and commands set -e and echo 0 in&& gmake test && set -e && echo 0 will be skipped, with the println run_tests statement, because failed gmake test will abort the jenkins build. As workaround you can switch to returnStatus:true, but then you will miss the output from your command.

    0 讨论(0)
  • 2020-12-04 08:36

    I was able to get this working using the answer found here:

    How to git commit nothing without an error?

    git diff --quiet --exit-code --cached || git commit -m 'bla'
    
    0 讨论(0)
  • 2020-12-04 08:39

    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

    0 讨论(0)
提交回复
热议问题