29
votes

The Extended Choice Parameter plugin is great and I use it in jobs configured via the UI https://wiki.jenkins-ci.org/display/JENKINS/Extended+Choice+Parameter+plugin

However, I'm struggling to get it working in a Jenkinsfile style pipeline script. It would appear that the Extended Choice Parameter plugin isn't yet fully compatible with Pipeline scripts since Jenkins pipeline-syntax generator creates the following snippet:

parameters([<object of type com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition>])

If I create the parameters manually I get the same behavior as mentioned in https://issues.jenkins-ci.org/browse/JENKINS-32188

org.kohsuke.stapler.NoStaplerConstructorException: There's no @DataBoundConstructor on any constructor of class 

Does anyone know of any workarounds that can get around the issue of ExtendedChoiceParameterDefinition not using @DataBoundConstructor?

  • Jenkins 2.19.2
  • Extended Choice Parameter plugin 0.75
6
JENKINS-34617 is an open issue for this.mkobit

6 Answers

20
votes

Since April's 2nd, 2019 it's now possible because of this commit: https://github.com/jenkinsci/extended-choice-parameter-plugin/pull/25

You can use it like this for instance:

properties([
    parameters([
        extendedChoice( 
            name: 'PROJECT', 
            defaultValue: '', 
            description: 'Sélectionnez le projet à construire.', 
            type: 'PT_SINGLE_SELECT', 
            groovyScript: valueKeysScript,
            descriptionGroovyScript: valueNamesScript
        )
    ])
])

If you want to know every possible parameter you have to refer to the source code. If you want to know every possible value for the "type" key, have a look at the PT_* constants.

6
votes

Here is my workaround for this pb:

https://gist.github.com/jgraglia/44a7443847cff6f0d87387a46c7bb82f

ie : manually instanciate the parameter by declaring all the args

I was able to add a multi checklist parameter to my pipeline with that.

4
votes

Navigate to your http://jenkins-url.com/pipeline-syntax.

On the Sample step dropdown select 'Properties: Set job properties'

There is a checkbox for 'This project is parameterized', then you can select Add parameter > Extended Choice Parameter. Add the menu items there then click 'Generate Pipeline Script' to convert.

Trim it so you remove the 'properties([parameters([' before and the '])])' after:

extendedChoice(defaultValue: 'whatif', description: 'Run as what if?', multiSelectDelimiter: ',', name: 'whatif', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_SINGLE_SELECT', value: 'whatif, LIVE', visibleItemCount: 2)

enter image description here

2
votes

Like mkobit said it is currently not possible to use the extended choice plugin as a build parameter.

What I like to use as a workaround is a construct like the following

timeout(time: 5, unit: TimeUnit.MINUTES) {
    def result = input(message: 'Set some values', parameters: [
        booleanParam(defaultValue: true, description: '', name: 'SomeBoolean'),
        choice(choices: "Choice One\nChoice Two", description: '', name: 'SomeChoice'),
        stringParam(defaultValue: "Text", description: '', name: 'SomeText')
    ]) as Map<String, String>
}

echo "${result.SomeBoolean}, ${result.SomeChoice}, ${result.SomeText}"

And call it in the beginning of my pipeline. You then get asked for these inputs shortly after your build starts.

1
votes

Use the below code to build multichoice checkbox parameter:

parameters {
  extendedChoice description: '', multiSelectDelimiter: ',', name: 'a', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_CHECKBOX', value: 'a,b,c', visibleItemCount: 3
}

It looks like in Jenkins UI:

enter image description here

Use Declarative Directive Generator to generate various pipeline source code which uses Extended Choice Parameter plugin

enter image description here

0
votes

Works for me :

I needed to retrieve all the versions number of artifacts from a Nexus Repo:

 properties ([
    parameters([
        choice(choices: ['PROD', 'DEV', 'QA'], description: '', name: 'ParamEnv' ),   
        string(name: 'ParamVersion', defaultValue: '', description: 'Version to deploy'),
        extendedChoice(
            name: 'someName',
            description: '',
            visibleItemCount: 50,
            multiSelectDelimiter: ',',
            type: 'PT_SINGLE_SELECT',
            groovyScript: '''
            import groovy.json.JsonSlurper
                List<String> nexusPkgV = new ArrayList<String>()        
                def pkgObject = ["curl", "https://xxxx:xxxx@xxxxxxxxxx"].execute().text
                def jsonSlurper = new JsonSlurper()
                def artifactsJsonObject = jsonSlurper.parseText(pkgObject)
                def dataA = artifactsJsonObject.items
                for (i in dataA) {
                    nexusPkgV.add(i.version)
                }
            return nexusPkgV
            '''
        )
    ])
])