如何使用groovy脚本对Jenkins参数排序

w1jd8yoj  于 2023-01-29  发布在  Jenkins
关注(0)|答案(1)|浏览(202)

我使用这个groovy脚本来参数化Jenkins作业:

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"]'
                ]
            ]
        ],
        [$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
      parameters {
        string(name: "param2", defaultValue: "test1", description: "Test value")
    }
  stages {
      stage ("Example") {
        steps {
         script{
          echo 'Hello'
        }
        }
      }
  }
}

如果我按原样使用此脚本,在Jenkins作业中,参数将按以下顺序显示:param2,param1,param3。我真正想要的是按以下顺序排列它们:参数1、参数2、参数3。
从代码中可以看到,对于param 1和3,我使用的是主动选择参数和主动选择React参数,它们将取决于param1中选择的值。对于param2,我需要它作为字符串。
有没有办法做到这一点,有他们在这个顺序:参数1参数2参数3

ki0zmccv

ki0zmccv1#

好的,我发现我也可以在属性中使用这个:

[$class: 'StringParameterDefinition',
    description: 'Enter param2',
    name: 'param2',
    defaultValue: '',
    randomName: 'string-parameter-1343433232',
    trim: true
]

相关问题