Search code examples
jenkinsjenkins-pipeline

Pass Groovy object into active choice parameter


I use active choice reactive parameter with declarative pipeline. But I ran into a problem.

Is there any way to pass list object into script or call external method?

For example


environments = 'lab\nstage\npro'

List<String> someList = ['ccc', 'ddd']
def someMethod() { 
   return ['aaa', 'bbb']
}

properties([
    parameters([
        choice(name: 'ENVIRONMENT', choices: "${environments}"),
        [$class: 'CascadeChoiceParameter', 
            choiceType: 'PT_SINGLE_SELECT',
            description: 'Select a choice',
            filterLength: 1,
            filterable: true,
            name: 'choice1',
            referencedParameters: 'ENVIRONMENT',
            script: [$class: 'GroovyScript',
                fallbackScript: [
                    classpath: [], 
                    sandbox: true, 
                    script: 'return ["ERROR"]'
                ],
                script: [
                    classpath: [], 
                    sandbox: true, 
                    script: """
                        if (ENVIRONMENT == 'lab') { 
                            return someMethod() // !!! call method
                        }
                        else {
                            return someList // !!! return object
                        }
                    """.stripIndent()
                ]
            ]
        ]
    ])
])

pipeline {
    agent any
    ...
}

I would be grateful for any hints.


Solution

  • Solved it by using string interpolation

    List<String> someList = ['ccc', 'ddd']
    def someMethod() { 
       return ['aaa', 'bbb']
    }
    ...
    script: """
        if (ENVIRONMENT == 'lab') { 
            return ["${someMethod().join('","')}"] // !!! call method
        }
        else {
            return ["${someList.join('","')}"] // !!! return object
        }
    """.stripIndent()
    

    UPD: a function call with an argument that is a job parameter.

    In this case, the current solution will not work, since the argument variable will be searched for in the external context. You can replace it with params.ParamName:

    return ["${someMethod(params.ParamName).join('","')}"]
    

    but then its value will be null.

    So, you have to put whole function into parameter script like this:

    properties([
        parameters([
            [$class: 'ChoiceParameter',
                choiceType: 'PT_SINGLE_SELECT',
                filterable: false,
                name: 'PartnerName',
                script: [$class: 'GroovyScript',
                    fallbackScript: [
                        classpath: [],
                        sandbox: true,
                        script: 'return ["ERROR"]'
                    ],
                    script: [
                        classpath: [],
                        sandbox: true,
                        script: """
                            return ["single_partner"]
                        """.stripIndent()
                    ]
                ]
            ],
            [$class: 'CascadeChoiceParameter',
                choiceType: 'PT_SINGLE_SELECT',
                filterLength: 1,
                filterable: false,
                name: 'AutoCalcParam',
                referencedParameters: 'PartnerName',
                script: [$class: 'GroovyScript',
                    fallbackScript: [
                        classpath: [],
                        sandbox: true,
                        script: 'return ["ERROR"]'
                    ],
                    script: [
                        classpath: [],
                        sandbox: true,
                        script: """
                            def getGFEPartners(String partnerName) { 
                                return [partnerName+'_aaa', partnerName+'_bbb']
                            }
                            return getGFEPartners(PartnerName)
                             """.stripIndent()
                    ]
                ]
            ],
    

    And will see expected result:
    enter image description here