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

如何通过Groovy脚本调整Jenkins任务参数的展示顺序?

Jenkins参数排序:调整param1、param2、param3的显示顺序

你当前的问题是Jenkins任务参数显示顺序为param2、param1、param3,想要改成param1、param2、param3。核心原因是Jenkins会优先渲染pipeline块内parameters定义的参数,再处理properties里的参数,所以param2会排在最前面。

解决方法很直接:把所有参数统一放到properties()的parameters列表中,按照你想要的顺序排列即可。修改后的完整脚本如下:

properties([
    parameters([
        [$class: 'ChoiceParameter',
            choiceType: 'PT_SINGLE_SELECT',
            description: 'Select param1',
            filterLength: 1,
            filterable: false,
            name: 'param1',
            randomName: 'choice-parameter-5631314439613978',
            script: [
                $class: 'GroovyScript',
                fallbackScript: [
                    classpath: [],
                    sandbox: true,
                    script:
                        'return[\'Could not get param1\']'
                ],
                script: [
                    classpath: [],
                    sandbox: true,
                    script:
                        'return["value1", "value2", "value3"]'
                ]
            ]
        ],
        // 将param2移到此处,放在param1之后
        string(
            name: "param2",
            defaultValue: "test1",
            description: "Test value"
        ),
        [$class: 'CascadeChoiceParameter',
            choiceType: 'PT_SINGLE_SELECT',
            description: 'Select param3',
            filterLength: 1,
            filterable: false,
            name: 'param3',
            randomName: 'choice-parameter-10000000000000000',
            referencedParameters: 'param1',
            script: [
                $class: 'GroovyScript',
                fallbackScript: [
                    classpath: [],
                    sandbox: true,
                    script:
                        'return[\'Could not get param3\']'
                ],
                script: [
                    classpath: [],
                    sandbox: true,
                    script:
                        ''' if (param1.equalsIgnoreCase('value1')){
                                return["1", "2"]
                            }
                            else if(param1.equalsIgnoreCase("value2")){
                                return["3", "4"]
                            }
                            else if(param1.equalsIgnoreCase("value3")){
                                return["5", "6"]
                            }
                        '''
                ]
            ]
        ]
    ])
])

pipeline {
  agent any
  // 移除原pipeline块内的parameters定义
  stages {
      stage ("Example") {
        steps {
         script{
          echo 'Hello'
        }
        }
      }
  }
}

关键说明

  • 将原来pipeline块里的param2移动到properties的parameters数组中,位置放在param1和param3之间,Jenkins会严格按照数组的顺序渲染所有参数。
  • 删除pipeline块内的parameters定义,避免参数重复定义导致的冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 19:41:05