How can I use the Extended Choice Parameter plugin in a Jenkins pipeline script?

前端 未结 6 1800
暖寄归人
暖寄归人 2021-02-02 09:01

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

How

相关标签:
6条回答
  • 2021-02-02 09:33

    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.

    0 讨论(0)
  • 2021-02-02 09:33

    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.

    0 讨论(0)
  • 2021-02-02 09:34

    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)
    

    0 讨论(0)
  • 2021-02-02 09:37

    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.

    0 讨论(0)
  • 2021-02-02 09:53

    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
                '''
            )
        ])
    ]) 
    
    0 讨论(0)
  • 2021-02-02 09:58

    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:

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

    0 讨论(0)
提交回复
热议问题