【发布时间】: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