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 :
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?