ActiveChoice Plugin

Jenkins setup: Docker container with version 2.474

I have a sample repository( lets say A) and in one branch(lets say branch1), I have the Jenkinsfile.
This is identified by the Jenkins and the pipeline runs.

I would like to introduce some parameters and make the pipeline more dynamic.
The Branch1 has a config file(config.json) which contains information as below:

{
   "information_to_display" : ["value_1", "value_2", "value_3"]
}

Now, I am using the Active chocie plugin for creating dynamic parameters.

Since the Jenkinsfile ultimately picks up code from the shared library, I have the following setup.

def call(body) {
    // Configuration from the argument
    def config = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config
    body()
    config =config
        node{
            ui_component = new UI()
            branch = env.BRANCH_NAME
            ui_component.setup(branch)
       //stages follow
        }

I have created a UI.groovy, which only contains the UI section of the code.
Inside UI.groovy, I have a setup() method, which takes branch information as the input and reads the config.json and should return the values for “information_to_display”.

The code :

def setup(String branch_param){
        properties([
            parameters([   
                
                 [$class: 'CascadeChoiceParameter',             
                choiceType: 'PT_SINGLE_SELECT',             
                name: 'VALUES',
                description: 'VALUES in config.json?', 
                script: [                 
                    $class: 'GroovyScript', 
                    fallbackScript: [
                        classpath: [], 
                        sandbox: true, 
                        script: 'return ["ERROR"]'
                    ],                
                
                script: [       
                    classpath: [], 
                    sandbox: false,             
                    script:'''
                        import java.net.*
                        import groovy.json.*
                        def getModels(branch_param) {     
                            valu = "${branch_param}"
                            def PASSWORD = com.cloudbees.plugins.credentials.SystemCredentialsProvider.getInstance().getStore().getCredentials(com.cloudbees.plugins.credentials.domains.Domain.global()).
  				                          find { it.getId().equals("XX") }.getPassword()
                            url = new URL("https://XXX/config.json?ref=$valu&token=$PASSWORD")
                            response = new JsonSlurperClassic().parseText(url.getText())
                            encoded_content= response.get('content')
                            decoded_content= new JsonSlurperClassic().parseText(new String(encoded_content.decodeBase64()))
                            return decoded_content.get('information_to_display')
                        }
                        default_entry = getModels(branch_param) 
                        return default_entry
                    '''        
                    ]             
                ]],        

I expect the values to be present in the jenkins UI. But I get the following :

image

If I hardcode the branch name inside getModels() it works. But I want it to be dynamic.

Is it not possible to dynamically fetch data from the same branch as where the pipeline runs? What am I doing wrong here?

You have to consider that the code inside the parameter for Active Choice is not run in the context of the Jenkins pipeline but is executed when you click on Build with Parameters. At that time the job (pipeline) hasn’t started running yet.
When you define parameters for a job inside a Jenkinsfile, changes to those parameters become only active on the next execution of the job.

The problem is also that you use single quotes for the script (script: ''' )
So when you get to the line default_entry = getModels(branch_param) branch_param is not defined.

Thank you for your reply.

Won’t the branch_param take the value from the main parameter - setup(String branchname)? is it technically possible?
Is using the triple single quotes incorrect?

When you use single quotes groovy will not do string interpolation only with double quotes.
The problem is that you define a script inside a script.
You can try below code but I assume it will not do what you want. The parameter will not affect the current run of the pipeline but will only set the parameters for the next execution.
If you want user input during pipeline execution you will have to use an input step

def setup(String branch_param){
        properties([
            parameters([   
                
                 [$class: 'CascadeChoiceParameter',             
                choiceType: 'PT_SINGLE_SELECT',             
                name: 'VALUES',
                description: 'VALUES in config.json?', 
                script: [                 
                    $class: 'GroovyScript', 
                    fallbackScript: [
                        classpath: [], 
                        sandbox: true, 
                        script: 'return ["ERROR"]'
                    ],                
                
                script: [       
                    classpath: [], 
                    sandbox: false,             
                    script:"""
                        import java.net.*
                        import groovy.json.*
                        def getModels(branch_param) {     
                            valu = "${branch_param}"
                            def PASSWORD = com.cloudbees.plugins.credentials.SystemCredentialsProvider.getInstance().getStore().getCredentials(com.cloudbees.plugins.credentials.domains.Domain.global()).
  				                          find { it.getId().equals("XX") }.getPassword()
                            url = new URL("https://XXX/config.json?ref=$valu&token=$PASSWORD")
                            response = new JsonSlurperClassic().parseText(url.getText())
                            encoded_content= response.get('content')
                            decoded_content= new JsonSlurperClassic().parseText(new String(encoded_content.decodeBase64()))
                            return decoded_content.get('information_to_display')
                        }
                        default_entry = getModels("${branch_param}") 
                        return default_entry
                    """        
                    ]             
                ]],

Hmm, so on clicking the “Build with Parameters” the udpated values from the underlying config is fetched.

Since i am executing this UI portion of the code within the node{} block, the use of env.BRANCH_NAME is also not possible. I expected that to work atleast?

Inside the getModels() { valu = env.BRANCH_NAME }

The code inside the script of the Active choice parameter is not executed in the context of the pipeline. So there is no env variable that contains the branch_name.

this holds true for currentBuild construct too right?

yes, currentBuild is not available inside an ActiveChoice parameter script.
At the time the ActiveChoice script is executed there is no active build yet, no Jenkinsfile has been loaded, no build number has been set and so on.