31

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
mkobit
  • 43,979
  • 12
  • 156
  • 150
Spangen
  • 4,420
  • 5
  • 37
  • 42
  • 4
    [JENKINS-34617](https://issues.jenkins-ci.org/browse/JENKINS-34617) is an open issue for this. – mkobit Apr 04 '17 at 14:03

6 Answers6

28

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.

DevAntoine
  • 1,932
  • 19
  • 24
  • Hi Dev I'm trying to find a way to bind a set of variables - and fail. Any idea howto ?? Thx – albertsha Jul 25 '19 at 12:48
  • @BigAlbert You should post a new question and send me the link it'd be easier for me to help you. – DevAntoine Jul 26 '19 at 14:55
  • Hi Dev, thanks for responding - found the issue - my bad – albertsha Jul 28 '19 at 15:33
  • 1
    Where can 'valueKeysScript' be defined? Or does the entire groovy code need to be inline? I've tried defining a function in the Jenkinsfile above the pipeline but it's not found at runtime, maybe that doesn't work with declarative pipelines? – NeilS Nov 20 '19 at 01:53
7

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.

jgraglia
  • 591
  • 6
  • 4
  • Multi select example: https://support.cloudbees.com/hc/en-us/articles/115003895271-How-to-do-a-multiselect-input-in-a-pipeline – qwerty Jan 21 '19 at 07:04
6

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

ahmoreish
  • 180
  • 3
  • 8
  • Where does once find 'Generate Pipeline Script'? – Dave Nov 04 '19 at 18:47
  • 2
    If your Jenkins server is 10.10.10.10 on port 12345 the URL is 10.10.10.10:12345/pipeline-syntax Then, 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. Then click 'Generate Pipeline Script' to convert. – ahmoreish Nov 04 '19 at 23:29
  • 1
    This is what I needed +1 @icey – bhordupur Apr 10 '20 at 16:42
2

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.

Torbilicious
  • 467
  • 1
  • 4
  • 17
1

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
            '''
        )
    ])
]) 
EricD
  • 306
  • 1
  • 4
1

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

rok
  • 9,403
  • 17
  • 70
  • 126