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

Jenkins Pipeline按变量跳过Stage:仅RUN_BACKUP为YES时获取用户输入

Fix: Input Prompt Triggers Even When RUN_BACKUP is "NO"

I get it, that's frustrating—you set up a when condition to only run the backup stage if RUN_BACKUP is "YES", but the input prompt still pops up when it's set to "NO". Let's sort this out.

Why This Happens

In some Jenkins pipeline setups, the input directive placed directly under the stage (outside the steps block) can get evaluated before the when condition is checked. That means Jenkins tries to prompt for input even when the stage shouldn't execute at all.

The Fix: Move Input Inside Conditional Steps

To make sure the input prompt only appears when RUN_BACKUP is "YES", move the input request inside the steps block's script, guarded by the existing (or an explicit) conditional check. Here's the adjusted code:

stage('Executing DB backup stage') {
    when {
        expression { params.RUN_BACKUP == 'YES' }
    }
    steps {
        script {
            // Prompt for DB parameters only when the stage is allowed to run
            def dbParams = input(
                message: "Enter DB parameters?",
                ok: "OK",
                parameters: [
                    string(defaultValue: '', name: 'DB_HOST', description: 'database host'),
                    string(defaultValue: '30051', name: 'DB_PORT', description: 'database port'),
                    password(defaultValue: '', name: 'DB_PASSWORD', description: 'database password')
                ]
            )

            def now = LocalDateTime.now()
            def datePart = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
            print "Current Date is : ${datePart}"
            
            // Use the input parameters in your backup command if needed
            def statusCode = sh(script: "run my own backup command --host ${dbParams.DB_HOST} --port ${dbParams.DB_PORT}", returnStatus:true)
            println "Exit Status = ${statusCode}"
            
            if(statusCode != 0){
                error "Error Backing Up Database"
            } else {
                print "Backup completed successfully"
            }
        }
    }
}

Extra Checks to Avoid Edge Cases

If you want to be extra safe, you can add a redundant check inside the script (though the when condition should handle it) or normalize the parameter value to avoid case issues:

// Add this inside the script block if you want double assurance
if (params.RUN_BACKUP?.toUpperCase() != 'YES') {
    return // Exit early if backup isn't enabled
}

Key Takeaway

By nesting the input inside the steps block, you ensure Jenkins only triggers the prompt after verifying the when condition. This aligns perfectly with your goal of running the input and backup logic only when RUN_BACKUP is set to "YES".

内容的提问来源于stack exchange,提问作者Dileeka Fernando

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 07:50:27