You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何捕获Jenkins构建作业Execute Shell步骤的退出码用于流水线后续操作

解决Jenkins流水线特定退出码重试问题

没问题,这个需求完全可以实现!咱们要做的就是先捕获my_build作业的退出码,然后根据特定码触发重试逻辑,我给你一步步拆解:

核心思路

  1. 让Jenkins流水线调用my_build时不直接终止流程,而是返回作业的退出码
  2. 判断退出码是否符合重试条件,仅当匹配时触发重试,其他错误直接终止流水线
  3. 控制最大重试次数,避免无限循环

修改后的流水线脚本

把你原来的Build阶段替换成下面的逻辑,我加了详细注释:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                script {
                    // 1. 定义需要触发重试的特定退出码(比如这里假设是2,你改成自己的目标码)
                    def targetRetryCode = 2
                    // 2. 设置最大重试次数
                    def maxRetryTimes = 3
                    def currentRetry = 0
                    def buildExitCode = -1

                    // 循环执行构建直到成功、达到重试上限或遇到非重试错误
                    while (currentRetry <= maxRetryTimes) {
                        // 关键:用returnStatus: true让build步骤返回退出码,而非直接失败流水线
                        buildExitCode = build(job: 'my_build', returnStatus: true)
                        
                        if (buildExitCode == 0) {
                            // 构建成功,退出循环
                            echo "✅ Build succeeded with exit code ${buildExitCode}"
                            break
                        } else if (buildExitCode == targetRetryCode) {
                            // 匹配到需要重试的退出码,开始重试流程
                            currentRetry++
                            if (currentRetry <= maxRetryTimes) {
                                echo "⚠️ Build failed with retryable exit code ${buildExitCode}, retrying (${currentRetry}/${maxRetryTimes})..."
                                // 可选:重试前等待30秒,避免频繁重试(根据需要调整)
                                sleep(time: 30, unit: 'SECONDS')
                            } else {
                                echo "❌ Max retries (${maxRetryTimes}) reached, build failed permanently"
                                // 主动抛出错误,终止流水线
                                error "Build failed after ${maxRetryTimes} retries with exit code ${buildExitCode}"
                            }
                        } else {
                            // 遇到非重试的错误码,直接终止流水线
                            echo "❌ Build failed with non-retryable exit code ${buildExitCode}"
                            error "Build failed with non-retryable exit code ${buildExitCode}"
                        }
                    }
                }
            }
        }
        // 其他阶段...
    }
}

关键细节说明

  • returnStatus: true参数:这是实现捕获退出码的核心,默认情况下build job如果失败会直接抛出异常终止流水线,加上这个参数后会返回作业的实际退出码,让我们可以后续判断。
  • 重试逻辑控制:用while循环结合重试次数计数器,确保不会无限重试;同时区分“可重试错误”和“不可重试错误”,避免不必要的重试。
  • error函数:当需要终止流水线时,用这个函数主动抛出错误,让Jenkins正确标记流水线状态为失败。

关于my_build作业的shell脚本

你当前的shell脚本其实可以简化:

#!/bin/bash
./my_build_script.sh

因为./my_build_script.sh执行后的退出码会自动成为shell步骤的退出码,exit $?是多余的,但保留也不会影响结果,只要确保my_build_script.sh确实返回了你预期的特定退出码就行。

内容的提问来源于stack exchange,提问作者The Spartan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:54:57