【问题标题】:How to error trap in Jenkins Execute shell?如何在 Jenkins Execute shell 中进行错误陷阱?
【发布时间】:2019-04-13 15:14:57
【问题描述】:

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

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

编辑jenkins shell git push 后如下:

git push "$target" --all | grep -z "error:" && exit 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

EDIT #4 RESPONSE=$(git push "$target" --all 2>&1 | grep "error:" || true) 成功了。

【问题讨论】:

    标签: git shell jenkins error-handling


    【解决方案1】:

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

    但别担心,我也有一个简单的解决方法供您使用(也许不漂亮,但它有效)。您始终可以通过自己设置错误代码来手动告诉 Jenkins 构建失败。在这种情况下,您可以 grep 您的 git 推送错误。如果有,使用代码 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 将强制失败的虚拟作业:

    您总是可以尝试执行相同的更多 bash 脚本,例如:

    RESPONSE=$(git push ... | grep -z "error:" || true)
    
    if [ -n "$RESPONSE" ]; then
      exit 1
    fi
    

    另外,我已将|| true 添加到解决方法命令中,因为我忘记了如果未选择任何行,grepexit 1

    如果选择了任何一行,则退出状态为 0,否则为 1

    这意味着这将强制您的所有构建失败。

    【讨论】:

    • 谢谢 Raoslaw。我要做的就是在 2 个 BitBucket 实例之间同步 jenkins shell 中的 2 个 repos。由于(预接收挂钩被拒绝),一些推送被拒绝,并在上面生成最终错误。不幸的是,它并没有让詹金斯的工作失败。我只想在推送失败时通过标记 Jenkins 作业失败或不稳定来发出警告。会尝试你的建议。再次感谢。
    • 用更多信息修改了我的问题,错误仍然没有将 Jenkins 作业的状态标记为失败。
    • @Dzerlig 好的,让我检查一下,我将创建虚拟作业来调试它。顺便问一下,你使用 Execute shell build step 吗?
    • @Dzerlig 请查看编辑。您可以将您的执行外壳添加到问题中吗?也许有什么错误或错字。如果你想让构建不稳定,只需在 Execute Shell Advanced 选项中设置退出代码,将构建不稳定设置为 1。
    • 谢谢!我明白了,编辑#4 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    相关资源
    最近更新 更多