Jenkinsfile class CascadeChoiceParameter return my array as a string

Viewed 1543

I'm trying to use the CascadeChoiceParameter to dynamically mount my parameters form, using commands to create my list of options:

                    choiceType: 'PT_SINGLE_SELECT', 
                    description: 'Informations about the application on kubernetes', 
                    name: 'deployments', 
                    omitValueField: false, 
                    randomName: 'choice-parameter-5633384460832175', 
                    referencedParameters: 'namespaces', 
                    script: [
                        $class: 'GroovyScript', 
                        script: [
                            classpath: [],
                            sandbox: true,
                            script: """
                                if (namespaces.equals("Select")){
                                    return["Nothing to do - Select your deployment"]
                                } else {
                                    def kubecmd = "kubectl get deploy --kubeconfig=${kubefileHlg} -o jsonpath={.items[*].metadata.name} -n " + namespaces
                                    return [kubecmd.execute().in.text.split()]

                                }
                            """

                        ]

The form parameter on Jenkins shows me this - a single option with all values comma separated:

Here the image

Do you have any idea how can I mount these options as a real list on it?

1 Answers

Your script in the "else" part returns a list that contains a single element of type String[] (an array of strings). What you need to return instead is a List<String>. Replace

return [kubecmd.execute().in.text.split()]

with

return kubecmd.execute().in.text.split().toList()

and you will see the expected result.

Quick example:

node {
    properties([
        parameters([
            [
                $class: 'CascadeChoiceParameter',
                choiceType: 'PT_SINGLE_SELECT',
                name: 'someChoice', 
                script: [
                    $class: 'GroovyScript',
                    script: [
                        sandbox: true,
                        classpath: [],
                        script: '''
                            return "Lorem ipsum dolor sit amet".split().toList()
                        '''
                    ]
                ]
            ]
        ])
    ])

    stage("Test") {
        echo env.someChoice
    }
}

Output:

enter image description here

Related