gpt4 book ai didi

git - 如何在Jenkins Execute Shell中进行错误陷阱?

转载 作者:行者123 更新时间:2023-12-03 07:41:33 28 4
gpt4 key购买 nike

我在执行 shell Jenkins配置中运行了一堆git操作。我确实看到了正在生成的错误,但是 Jenkins 的工作仍然显示为成功。我如何捕获以下错误,从而使Jenkins作业的状态失败:

错误:无法将一些引用推送到'ssh://git @ git

编辑jenkins shell git push之后,如下所示:

git push“$ target”-全部| grep -z“错误:” &&退出1

即使发生错误,Jenkins作业仍被标记为成功

15:24:06  ! [remote rejected] test/test/testBranch ->test/test/testBranch (pre-receive hook declined)
15:24:06 error: failed to push some refs to 'ssh://git@git.testing-repo/test/test-test.git'
15:24:06 hint: Updates were rejected because the remote contains work that you do
15:24:06 hint: not have locally. This is usually caused by another repository pushing
15:24:06 hint: to the same ref. You may want to first integrate the remote changes
15:24:06 hint: (e.g., 'git pull ...') before pushing again.
15:24:06 hint: See the 'Note about fast-forwards' in 'git push --help' for details.
15:24:06 Everything up-to-date
15:24:07 Finished: SUCCESS

编辑#2所有更改都存在于jenkins的Execute shell(#!/bin/bash)配置部分中。

脚本:
RESPONSE=$(git push "$target" --all | grep "error:" || true)


if [ -n "$RESPONSE" ]; then
exit 1
fi

输出:
14:42:30 Cloning into bare repository 'testing-repo.git'...
14:44:25 From bitbucket.org:TEST/testing-repo
14:44:25 * branch HEAD -> FETCH_HEAD
14:44:29 remote:
14:44:29 ! [remote rejected] test/test/testBranch ->test/test/testBranch (pre-receive hook declined)
14:44:29 error: failed to push some refs to 'ssh://git@git.testing-repo/test/test-test.git'
14:44:29 hint: Updates were rejected because the remote contains work that you do
14:44:29 hint: not have locally. This is usually caused by another repository pushing
14:44:29 hint: to the same ref. You may want to first integrate the remote changes
14:44:29 hint: (e.g., 'git pull ...') before pushing again.
14:44:29 hint: See the 'Note about fast-forwards' in 'git push --help' for details.
14:44:29 Everything up-to-date
14:44:29 Finished: SUCCESS

编辑#3:实际上,当我在shell中调试$ RESPONSE时,它不包含任何数据,因此它可以解释为什么它不会更改jenkins作业的状态。因此,即使git命令实际上执行了预期的操作,它也没有将命令的输出馈送到$ RESPONSE中

编辑#4 RESPONSE = $(git push“$ target” --all 2>&1 | grep“error:” || true)达到了目的。

最佳答案

这个问题很奇怪,因为此git push失败,因此它应返回不同于0的退出代码,然后退出构建。 Jenkin的Execute Shell生成步骤的最后一个命令的退出代码是确定Build步骤成功/失败的原因。 0-成功,其他任何内容-失败。如果您可以提供更多数据,也许我可以调试一下。 git push之后返回什么退出代码? (您可以通过键入以下命令来检查最后一个命令的退出代码:echo $?)您还可以添加一些示例吗?

但请放心,我也为您提供了一个简单的解决方法(也许不漂亮,但它可以工作)。您始终可以通过自己设置错误代码来手动对Jenkins说失败的构建。在这种情况下,您可以对git push进行grep进行错误处理。如果存在,请退出并输入代码1。

git push ... | grep -z "error: failed to push some refs to" && exit 1 || true

或更通用的:
 git push ... | grep -z "error:" && exit 1 || true

编辑

您究竟在哪里运行此 git push命令?在执行 shell 构建步骤?您使用 Jenkins Source Code Management吗? (这将使在Jenkins内部的存储库更容易使用)

虚拟作业,以显示 exit 1将强制失败:

Build Execute shell
Console Output

您可以随时尝试执行相同的bash脚本,例如:
RESPONSE=$(git push ... | grep -z "error:" || true)

if [ -n "$RESPONSE" ]; then
exit 1
fi

另外,我已经将 || true添加到了解决方法命令中,因为我忘记了如果没有选择任何行, grep将成为 exit 1:

Exit status is 0 if any line is selected, 1 otherwise



这意味着这将迫使所有构建失败。

关于git - 如何在Jenkins Execute Shell中进行错误陷阱?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53236096/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com