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

Jenkinsfile中Active Choice Reactive Reference Parameter使用报错求助

Fixing Active Choice Reactive Parameters in Jenkinsfile

Let's work through your issue step by step. The error The properties section has been renamed as of version 0.8. Use options stems from two main problems: outdated parameter class names from the Active Choice plugin, and a minor syntax adjustment for modern Jenkins Pipeline versions.

Key Issues in Your Original Code

  • Deprecated Parameter Classes: ChoiceParameter and CascadeChoiceParameter are no longer the correct classes for Active Choice parameters in newer plugin versions. You need to use ActiveChoiceParameter (for static dropdowns) and ActiveChoiceReactiveParameter (for cascading/reactive options) instead.
  • Pipeline Syntax Misalignment: If you're using a Declarative Pipeline, the properties block needs to be wrapped inside the options block (as the error hints). For Scripted Pipeline, properties works directly, but we'll update it to align with current best practices.
  • Groovy Script Tweaks: Your reactive script had an unclosed string literal and could be made more robust to handle edge cases.

Corrected Jenkinsfile Code

For Declarative Pipeline

pipeline {
    agent any
    options {
        properties([
            parameters([
                // Static Active Choice Parameter for Environment
                [$class: 'ActiveChoiceParameter',
                 choiceType: 'PT_SINGLE_SELECT',
                 description: 'Select the Env Name from the Dropdown List',
                 filterLength: 1,
                 filterable: true,
                 name: 'Env',
                 script: [
                     $class: 'GroovyScript',
                     fallbackScript: [
                         classpath: [],
                         sandbox: false,
                         script: 'return ["Could not get Env"]'
                     ],
                     script: [
                         classpath: [],
                         sandbox: false,
                         script: 'return ["Dev","QA","Stage","Prod"]'
                     ]
                 ]
                ],
                // Reactive Active Choice Parameter for Servers
                [$class: 'ActiveChoiceReactiveParameter',
                 choiceType: 'PT_CHECKBOX',
                 description: 'Select Servers',
                 filterLength: 1,
                 filterable: true,
                 name: 'Server',
                 referencedParameters: 'Env',
                 script: [
                     $class: 'GroovyScript',
                     fallbackScript: [
                         classpath: [],
                         sandbox: false,
                         script: 'return ["Could not get Environment from Env Param"]'
                     ],
                     script: [
                         classpath: [],
                         sandbox: false,
                         script: '''
                             def envName = binding.get('Env') ?: ''
                             switch(envName) {
                                 case "Dev":
                                     return ["devaaa001","devaaa002","devbbb001","devbbb002","devccc001","devccc002"]
                                 case "QA":
                                     return ["qaaaa001","qabbb002","qaccc003"]
                                 case "Stage":
                                     return ["staaa001","stbbb002","stccc003"]
                                 case "Prod":
                                     return ["praaa001","prbbb002","prccc003"]
                                 default:
                                     return ["No servers found for selected environment"]
                             }
                         '''
                     ]
                 ]
                ]
            ])
        ])
    }
    stages {
        stage('Verify Parameters') {
            steps {
                echo "Selected Environment: ${params.Env}"
                echo "Selected Servers: ${params.Server}"
            }
        }
    }
}

For Scripted Pipeline

properties([
    parameters([
        [$class: 'ActiveChoiceParameter',
         choiceType: 'PT_SINGLE_SELECT',
         description: 'Select the Env Name from the Dropdown List',
         filterLength: 1,
         filterable: true,
         name: 'Env',
         script: [
             $class: 'GroovyScript',
             fallbackScript: [
                 classpath: [],
                 sandbox: false,
                 script: 'return ["Could not get Env"]'
             ],
             script: [
                 classpath: [],
                 sandbox: false,
                 script: 'return ["Dev","QA","Stage","Prod"]'
             ]
         ]
        ],
        [$class: 'ActiveChoiceReactiveParameter',
         choiceType: 'PT_CHECKBOX',
         description: 'Select Servers',
         filterLength: 1,
         filterable: true,
         name: 'Server',
         referencedParameters: 'Env',
         script: [
             $class: 'GroovyScript',
             fallbackScript: [
                 classpath: [],
                 sandbox: false,
                 script: 'return ["Could not get Environment from Env Param"]'
             ],
             script: [
                 classpath: [],
                 sandbox: false,
                 script: '''
                     def envName = binding.get('Env') ?: ''
                     switch(envName) {
                         case "Dev":
                             return ["devaaa001","devaaa002","devbbb001","devbbb002","devccc001","devccc002"]
                         case "QA":
                             return ["qaaaa001","qabbb002","qaccc003"]
                         case "Stage":
                             return ["staaa001","stbbb002","stccc003"]
                         case "Prod":
                             return ["praaa001","prbbb002","prccc003"]
                         default:
                             return ["No servers found for selected environment"]
                     }
                 '''
             ]
         ]
        ]
    ])
])

node {
    stage('Verify Parameters') {
        echo "Selected Environment: ${params.Env}"
        echo "Selected Servers: ${params.Server}"
    }
}

What Changed?

  1. Updated Parameter Classes: Switched to the official, supported classes ActiveChoiceParameter and ActiveChoiceReactiveParameter to resolve plugin compatibility issues.
  2. Fixed Pipeline Syntax: For Declarative Pipeline, wrapped properties inside options to eliminate the version warning. For Scripted Pipeline, kept properties at the top but updated parameter classes.
  3. Robust Groovy Script:
    • Used binding.get('Env') to safely retrieve the parameter value (avoids scope-related errors)
    • Added a default case to handle unexpected environment selections
    • Fixed string formatting and closed all literals properly
  4. Removed Unnecessary Field: The auto-generated randomName field is not required in Pipeline definitions—Jenkins handles this automatically.

Additional Tips

  • Ensure the Active Choices Plug-in and its dependency Active Choices Reactive Reference Parameter are installed and updated to the latest compatible version in your Jenkins instance.
  • If sandbox-related errors occur, you can either:
    • Enable sandbox mode and approve required Groovy methods in Manage Jenkins > In-process Script Approval
    • Keep sandbox mode disabled (as you did) but ensure only trusted users can modify your pipelines.

内容的提问来源于stack exchange,提问作者naresh mahato

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 19:39:09