Home > OS >  How to order Jenkins Parameters using groovy script
How to order Jenkins Parameters using groovy script

Time:02-01

I am using this groovy script to parameterize a Jenkins Job:

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'
        }
        }
      }
  }
}

If I use this script as is, On the Jenkins job, parameters will be shown in this order: param2, param1, param3. What I really want, is to have them in this order: param1, param2, param3.

From the code as you can see, for param 1 and 3 I am using Active Choice Parameter and Active Choice Reactive Parameter which will be dependent from the value selected in param1. For param2, I need it as string.

Is there a way to achieve this, have them in this order: param1, param2, param3?

CodePudding user response:

Okay, i found that i can use also this in the properties:

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