Jenkins Pipeline Silent Failure¶
概念概覽
失敗鏈¶
核心知識¶
失敗鏈¶
kubeContext: null
→ error: context "null" does not exist
→ || true 吃掉 exit code
→ pipeline 繼續執行(ECR push #839/#840 成功)
→ pod restart 被跳過
→ pipeline 最後才 FAILURE
危險模式¶
Jenkins pipeline 中使用 some_command || true 會吞掉所有非零 exit code,讓後續 stage 繼續執行並產生誤導性的「部分成功」狀態。當 null/empty 參數傳入 kubectl 的 --context 時,錯誤訊息被靜默,特別難以追蹤。
修復方式¶
在 Configure stage 開頭加入 null guard:
這讓 pipeline 在最早期就 FAILURE,而不是執行到一半才失敗。診斷方法¶
懷疑靜默失敗時,搜尋 pipeline log 中的 || true 用法,逐一確認被吞掉的 exit code 是否包含關鍵指令(kubectl、aws cli 等)。
經驗教訓¶
-
|| true 在 Jenkins pipeline 中是靜默失敗的高風險模式,應謹慎使用
-
null/empty 的 k8s context 參數會讓 kubectl 報錯但錯誤被吞掉
-
pipeline 最後才 FAILURE 不代表最後的 step 出錯,需從頭追查 exit code
常見陷阱¶
-
kubeContext:null 傳入 kubectl --context 會靜默失敗,後續 stage 繼續執行
-
ECR push 成功不代表 pod restart 成功,需分別確認每個 stage 結果
最佳實踐¶
-
關鍵 kubectl/aws 指令不要加 || true,寧可 pipeline 快速失敗
-
Jenkins pipeline 的必填參數在最開頭做 null guard 檢查
-
懷疑靜默失敗時搜尋 log 中所有 || true 的出現位置
相關概念¶
- EKS Cluster Version Upgrade
- Go Test Coverage -coverpkg Flag
- jenkins-msk-iam-auth-debug----dangling-------dangling---
- Jenkins Shared Library Branch Risk
- Jenkins SSH to Secret Text Credential Migration
相關視角¶
以下頁面與本概念共享主題,但從不同角度切入。保留獨立視角同時提供交叉參考:
- Jenkins Pipeline kubectl exec Target — 共享:
ci-cd,jenkins,pipeline/ 獨特:automation,kubectl - Helm Annotation Special Character Escaping — 共享:
groovy,jenkins,pipeline/ 獨特:annotation,escaping - ECR Image Tag Versioning in Jenkins Pipeline — 共享:
jenkins/ 獨特:deployment,ecr
來源 Sessions¶
| 日期 | Session | 貢獻摘要 |
|---|---|---|
| 2026-03-22 | 64b0516b-af8e-4206-8a1b-037aee68b1bf | 以 build #121 實際 log 佐證 kubeContext:null + || true 吃掉 exit code 的完整靜默失敗鏈 |