Jenkins Groovy : Pass values(list) to Active Choice Parameter while calling different Jenkins job

Viewed 1267

I need to pass the value for Active choice parameter while calling a different Jenkins job from my groovy file and the value is of Array/List type.

I have tried this

build job: "myjob", parameters:[choice(name: 'paramName', choices:['a','b','c'])]

It is giving error

No known implementation of class hudson.model.ParameerValue is using symbol 'choice'

Jenkins pass value of Active Choices Parameter, here I found some thing to pass active choice parameter value but here they are passing string value only, which is not the my case.

1 Answers

I did some research and I was unable to pass these types of parameters to a second job, which uses the same parameter. I doubt it's not possible, since there is no "value" type which can be passed to this plugins parameter class.

So the only option I see here, is just to use a string parameter in the second job. I guess in the second job you already do some parsing of that parameter value, so you won't need to change much:

properties([
    parameters([
        [$class: 'ChoiceParameter',
            choiceType: 'PT_MULTI_SELECT',
            filterLength: 1,
            filterable: false,
            name: 'TEST_PARAM',
            script: [
                $class: 'GroovyScript',
                fallbackScript: [
                    classpath: [],
                    sandbox: false,
                    script: 'return ["Check Jenkins ScriptApproval page"]'
                ],
                script: [
                    classpath: [],
                    sandbox: false,
                    script: 'return ["One","Two:selected"]'
                ]
            ]
        ]
    ])
])

pipeline {
    agent any
    
    stages {
        stage('Test') {
            steps {
                print params.TEST_PARAM
                
                build job: 'test2', parameters: [
                    string(name: 'TEST_PARAM', value: "${params.TEST_PARAM}")
                ]
            }
        }
    }
}

And then in the second job (Name = test2):



pipeline {
    agent any

    parameters {
        string(name: 'TEST_PARAM', defaultValue: '')
    }
    
    stages {
        stage('Test') {
            steps {
                script {
                    // split values, guess you're doing this already
                    // since the value of the parameter is also comma-separted
                    // when you get from "Active choice parameter"
                    def testParamValues = "${params.TEST_PARAM}".split(',')
                    
                    testParamValues.each { testParamValue ->
                        print testParamValue.trim()
                    }
                }
            }
        }
    }
}

If you want to use both, the "Active choice parameter" and that string parameter in your pipeline, you could just check which one has a length > 0 and then work with that value.

Related