簡體   English   中英

如何在 Jenkins 執行 shell 中出錯?

[英]How to error trap in Jenkins Execute shell?

我正在執行 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://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的退出代碼,然后你的構建應該失敗。 Jenkins 的 Execute Shell 構建步驟的最后一個命令的退出代碼決定了構建步驟的成功/失敗。 0 - 成功,其他 - 失敗。 如果您可以提供更多數據,也許我可以對此進行調試。 git push 后返回什么退出代碼? (您可以通過鍵入: echo $?來檢查最后一個命令的退出代碼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 源代碼管理嗎? (在 Jenkins 構建中使用存儲庫會更容易)

顯示exit 1將強制失敗的虛擬作業:

構建執行shell 控制台輸出

您總是可以嘗試執行更多相同的 bash 腳本,例如:

RESPONSE=$(git push ... | grep -z "error:" || true)

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

另外,我添加了|| true 對解決方法命令是|| true ,因為我忘記了如果沒有選擇任何行, grepexit 1

如果選擇任何行,退出狀態為 0,否則為 1

這意味着這將迫使您的所有構建失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM