git - 如何在 Jenkins 执行 shell 中捕获错误?

标签 git shell jenkins error-handling

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

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

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

git push "$target"--all | 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://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a6c1cfd2e6c1cfd288d2c3d5d2cfc8c18bd4c3d6c9" rel="noreferrer noopener nofollow">[email protected]</a>/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 的执行 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://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c7b75685c7b75683268796f6875727b316e796c73" rel="noreferrer noopener nofollow">[email protected]</a>/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 的执行 Shell 构建步骤的最后一个命令的退出代码决定了构建步骤的成功/失败。 0 - 成功,其他 - 失败。如果您可以提供更多数据也许我可以对此进行调试。 git 推送后返回什么退出代码? (您可以通过键入:echo $?来检查最后一个命令的退出代码)您还可以添加一些示例吗?

但别担心,我还为您提供了一个简单的解决方法(也许不太漂亮,但它有效)。您始终可以通过自己设置错误代码来手动告诉 Jenkins 构建失败。在这种情况下,您可以 grep git Push 查找错误。如果有,则使用代码 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 到解决方法命令,因为我忘记了如果没有选择任何行,grepexit 1:

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

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

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

相关文章:

android - 使用 Jenkins 创建安卓模拟器

json - 如何将 git log -p 获取为 json

git - 解压缩 git 树对象

ios - Cocoapods 与 github fork 项目

linux - 如何使用 linux 命令从另一个文件创建一个只包含小写字母的新文件?

linux - Shell感叹号命令

jenkins - 执行 shell 命令框后,Jenkins 中的工作目录是什么?

git - TortoiseGit - 向其他用户推送错误

linux - 将文件中的每个单词替换为另一个字典文件中的值

java - Jenkins 和 JIRA Cloud 之间的集成