jenkinsgroovyparameterspipelineextended-choice-parameter

how to configure jenkins extendedChoice parameter to print value from map but see key in selection dropdown


I am having a somewhat difficult time figuring out how to make a simple Jenkins pipeline to print values from a simple map.

I use extendedChoice plugin.

The requirement is the following:

The user has a dropdown selection of names, once selected a name, job will simply print (in log) its value (.key).

this is the code I am trying to work with, made numerous changes and still get various errors and nothing works.

if anyone has any idea, will be glad to hear about it :D

def data = ["john": "33", "alex": "45", "michael": "22"]

properties([
    parameters ([
        extendedChoice( 
             name: 'CHOICE', 
             description: 'name and age selection', 
             type: 'PT_SINGLE_SELECT',
             value: data.key // i think i am writing this wrong.. i need to see names in selection dropdown box
             )
    ])
])

pipeline {
    agent any
    stages {
        stage('print choice') {
            steps {
                println params.CHOICE.value // how to print .value for user i selected?
            }
        }
    }
}


Solution

  • Here is a working Pipeline for your example.

    def data = ["john": "33", "alex": "45", "michael": "22"]
    
    properties([
        parameters ([
            extendedChoice( 
                 name: 'CHOICE', 
                 description: 'name and age selection', 
                 type: 'PT_SINGLE_SELECT',
                 value: "${data.keySet().join(',').toString()}"
                 )
        ])
    ])
    
    pipeline {
        agent any
        stages {
            stage('print choice') {
                steps {
                    println params.CHOICE
                    println data.get(params.CHOICE)
                }
            }
        }
    }