don't fail jenkins build if execute shell fails

前端 未结 14 1151
无人共我
无人共我 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:39

    This answer is correct, but it doesn't specify the || exit 0 or || true goes inside the shell command. Here's a more complete example:

    sh "adb uninstall com.example.app || true"
    

    The above will work, but the following will fail:

    sh "adb uninstall com.example.app" || true
    

    Perhaps it's obvious to others, but I wasted a lot of time before I realized this.

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

    On the (more general) question in title - to prevent Jenkins from failing you can prevent it from seeing exit code 1. Example for ping:

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

    And now you can e.g. get output of ping:

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

    Of course instead of ping ... You can use any command(s) - including git commit.

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

    To stop further execution when command fails:

    command || exit 0

    To continue execution when command fails:

    command || true

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

    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
    
    0 讨论(0)
  • 2020-12-04 08:48

    For multiple shell commands, I ignores the failures by adding:

    set +e commands true

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

    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.

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