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

Jenkins Pipeline超时后未按默认设置继续构建问题求助

Jenkins Pipeline 用户确认超时默认继续的修复方案

问题根源

你的代码存在三个关键问题导致超时默认继续不生效:

  1. input返回值处理错误:input步骤返回的是包含参数名的Map集合,直接用userInput == 'Abort'无法正确判断用户选择,必须通过userInput.PROCEED获取实际选项值。
  2. 异常捕获范围不准确:超时触发的是org.jenkinsci.plugins.workflow.steps.FlowInterruptedException,仅捕获普通Exception可能导致部分超时场景未被正确处理。
  3. 终止流程逻辑缺失:仅设置currentBuild.result = 'ABORTED'后return,只会退出当前script块,后续阶段仍会执行,无法真正终止构建。

修复后的完整代码

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                cleanWs()
                checkout scm
            }
        }

        stage('User Confirmation') {
            steps {
                script {
                    def proceedWithBuild = true // 默认继续构建
                    try {
                        timeout(time: 1, unit: 'MINUTES') {
                            def userInput = input(
                                id: 'userInput',
                                message: '是否继续执行构建?',
                                parameters: [
                                    choice(
                                        choices: ['Yes', 'Abort'],
                                        description: '选择"Yes"继续,选择"Abort"终止构建',
                                        name: 'PROCEED'
                                    )
                                ]
                            )
                            // 正确获取用户选择的选项
                            if (userInput.PROCEED == 'Abort') {
                                proceedWithBuild = false
                            }
                        }
                    } catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) {
                        // 捕获超时或用户主动中断的异常
                        if (e.cause instanceof hudson.model.AbortException) {
                            // 用户主动点击终止
                            proceedWithBuild = false
                            echo "用户主动终止构建"
                        } else {
                            // 超时触发,保持默认继续
                            echo "超时未操作,默认继续构建"
                        }
                    } catch (Exception e) {
                        // 处理其他异常,默认继续
                        echo "发生未知异常,默认继续构建: ${e.getMessage()}"
                    }

                    if (!proceedWithBuild) {
                        currentBuild.result = 'ABORTED'
                        // 主动终止整个Pipeline流程
                        error("构建已被用户终止")
                    }
                }
            }
        }

        stage('Build Product') {
            steps {
                sh 'chmod a+rx deployment.sh'
                sh './deployment.sh'
            }
        }
    }

    post {
        always {
            cleanWs()
            echo 'Jenkins Pipeline 执行完成!'
        }
        success {
            echo '部署成功'
        }
        failure {
            emailext(
                subject: "Pipeline 失败: ${env.JOB_NAME}",
                body: "Pipeline 构建 ${env.BUILD_NUMBER} 失败,请排查问题。",
                to: "abc@xyz.com"
            )
        }
    }
}

关键修改说明

  1. 正确处理input返回值:通过userInput.PROCEED获取用户选择的选项,确保判断逻辑生效。
  2. 精准捕获超时异常:优先捕获FlowInterruptedException,区分用户主动终止和超时两种场景,分别处理。
  3. 完善终止流程:设置构建状态为ABORTED后,调用error()主动终止整个Pipeline,避免后续阶段继续执行。
  4. 优化提示信息:将英文提示改为中文,更直观。

内容的提问来源于stack exchange,提问作者Minhaj Salahuddin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 05:53:16