Azee77
Azee77

Reputation: 140

fetch source values from jenkins extended choice parameter

Extended Choice Param

I have added an extended choice paramter. Now the source values are lin1, lin2, lin3 as listed in screenshot

now when I run, If I select lin1 then I get param3 = lin1,

If I select lin1 and lin2 then I get param2 - lin1,lin2 ( delimiter is comma )

The question here is, inside jenkins pipeline how can get what all source values were set when the param was created. In short, without selecting any of the checkboxes, want to get the list of the possible values probably in a list

Eg: list1 = some_method(param3) // expected output >> list1 = [lin,lin2,lin3]

Let me know if this description is not clear.

The user who runs this does not have configure access ( we dont want to give configure access to anonynmous user ) Hence the job/config.xml idea will not work here

Upvotes: 0

Views: 4556

Answers (2)

crash
crash

Reputation: 670

As requested you can also get the values dynamically:

import hudson.model.*
import org.jenkinsci.plugins.workflow.job.*
import com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition

def getJob(name) {
    def hi = Hudson.instance
    return hi.getItemByFullName(name, Job)
}

def getParam(WorkflowJob job, String paramName) {
    def prop = job.getProperty(ParametersDefinitionProperty.class)
    for (param in prop.getParameterDefinitions()) {
        if (param.name == paramName) {
            return param
        }
    }
    return null
}

pipeline {
    agent any

    parameters {
        choice(name: 'FOO', choices: ['1','2','3','4'])
    }

    stages {
        stage('test') {
            steps {
                script {
                    def job = getJob(JOB_NAME)
                    def param = getParam(job, "FOO")
                    if (param instanceof ChoiceParameterDefinition) {
                        // for the standard choice parameter
                        print param.getChoices()
                    } else if (param instanceof ExtendedChoiceParameterDefinition) {
                        // for the extended choice parameter plugin
                        print param.getValue()
                    }
                }
            }
        }
    }
}

As you can see it requires a lot of scripting, so just must either disable the Groovy sandbox or approve most of the calls on the script approval page.

Upvotes: 1

crash
crash

Reputation: 670

I couldn't find any variable or method to get the parameter list. I guess it's somehow possible through a undocumented method on the param or currentBuild maps.

A possible solution to your problem could be defining the map outside of the pipeline and then just use that variables like this:

def param3Choices = ['lin1', 'lin2', 'lin3']

pipeline {
   parameters {
       choice(name: 'PARAM3', choices: param3Choices, description: '')
   }

   stage('Debug') {
       steps {
          echo param.PARAM3
          print param3Choices
       }
   }
}

Upvotes: 1

Related Questions