Linux 如果执行 shell 失败,不要让 jenkins 构建失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14392349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:43:23  来源:igfitidea点击:

don't fail jenkins build if execute shell fails

linuxunixjenkins

提问by Ben

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?

作为构建过程的一部分,我将 git commit 作为执行 shell 步骤运行。但是,如果工作区中没有任何更改,Jenkins 将导致构建失败。这是因为当没有提交更改时,git 会返回错误代码。如果是这种情况,我想中止构建,或者只是将其标记为不稳定。有任何想法吗?

采纳答案by Quolonel Questions

To stop further execution when commandfails:

命令失败时停止进一步执行:

command || exit 0

command || exit 0

To continue execution when commandfails:

命令失败时继续执行:

command || true

command || true

回答by jphuynh

You can use the Text-finder Plugin. It will allow you to check the output console for an expression of your choice then mark the build as Unstable.

您可以使用文本查找器插件。它将允许您检查输出控制台以获取您选择的表达式,然后将构建标记为Unstable.

回答by jwernerny

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 0as the last command.

Jenkins 通过步骤的返回值确定步骤的成功/失败。对于shell的情况,它应该是最后一个值的返回。对于 Windows CMD 和 (POSIX) Bash shell,您应该能够使用exit 0as last 命令手动设置返回值。

回答by Ben

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

我能够使用这里找到的答案来完成这项工作:

How to git commit nothing without an error?

如何 git commit 没有错误?

git diff --quiet --exit-code --cached || git commit -m 'bla'

回答by Shaun Lebron

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

以下仅在有更改时提交才适用于mercurial。所以只有在提交失败时构建才会失败。

hg id | grep "+" || exit 0
hg commit -m "scheduled commit"

回答by ecervena

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返回退出状态1。执行shell构建步骤分别标记为失败。您可以使用 OR 语句 || (双管)。

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.

这意味着,如果第一次失败(返回退出状态 > 0),则执行第二个参数。第二个命令总是返回 0。当没有任何东西要推送时(退出状态 1 -> 执行第二个命令)echo 将返回 0 并继续构建步骤。

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.

要将构建标记为不稳定,您可以使用构建后步骤 Jenkins Text Finder。它可以通过控制台输出、匹配模式(您的回声)并将构建标记为不稳定。

回答by Nux

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

关于标题中的(更一般的)问题 - 为防止 Jenkins 失败,您可以防止它看到退出代码 1。ping 示例:

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

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

现在你可以得到 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.

当然,而不是ping ...您可以使用任何命令 - 包括git commit.

回答by joecks

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:

还有另一种平滑的方法可以告诉 Jenkins 不要失败。您可以在构建步骤中隔离您的提交并将 shell 设置为不失败:

set +e
git commit -m "Bla."
set -e

回答by Xiawei Zhang

Jenkins is executing shell build steps using /bin/sh -xeby default. -xmeans to print every command executed. -emeans to exit with failure if any of the commands in the script failed.

/bin/sh -xe默认情况下,Jenkins 正在执行 shell 构建步骤。-x表示打印执行的每个命令。-e如果脚本中的任何命令失败,则表示失败退出。

So I think what happened in your case is your git command exit with 1, and because of the default -eparam, 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.

因此,我认为在您的情况下发生的事情是您的 git 命令以 1 退出,并且由于默认-e参数,shell 会选择非 0 退出代码,忽略脚本的其余部分并将该步骤标记为失败。如果您可以在此处发布构建步骤脚本,我们可以确认这一点。

If that's the case, you can try to put #!/bin/shso that the script will be executed without option; or do a set +eor anything similar on top of the build step to override this behavior.

如果是这种情况,您可以尝试 put#!/bin/sh以便脚本将在没有选项的情况下执行;或set +e在构建步骤之上执行或执行任何类似操作以覆盖此行为。



Edited: Another thing to note is that, if the last commandin 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 echocommand at the end to avoid that.

编辑:另一件需要注意的事情是,如果您的 shell 脚本中的最后一个命令返回 non-0 code,则即使使用此设置,整个构建步骤仍将被标记为失败。在这种情况下,您可以简单地echo在末尾放置一个命令来避免这种情况。

Another related question

另一个相关问题

回答by chenchuk

If you put this commands into shell block:

如果将此命令放入 shell 块中:

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:

您的构建将被标记为失败(至少 1 个非零退出代码),因此您可以添加 (set +e) 以忽略它:

set +e
false
true

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

不会失败。但是,即使 (set +e) 到位,这也会失败:

set +e
false

because the last shell command must exit with 0.

因为最后一个 shell 命令必须以 0 退出。