0

I am completely novice at Jenkins, this post Jenkins Plugin:Extended Choice Parameter from JSON file does work for me.

I want to load a list of versions from a json file, which name is input.json, and use this version list as choices of one parameter, named version_list. The input.json file is in my $WORKSPACE, it's content is :

{
    "versions":["V1","V2"]
}

The pipeline script is as follows:

pipeline {
agent any


    stages {
        
        stage("load json") {
            steps {
                echo "version_list = ${params.version_list}"
            }
        }
        stage("open_json") {
            steps {
                script {
                    def prop = readJSON file: "${env.WORKSPACE}/input.json"
                    def version_list = prop.versions
    
                    println version_list.join(',')
                }
            }
        }
    }
}

The parameter version_list is setted as follows:

  • Extended Choice Paramter

  • name: versioin_list

  • Parameter Type: Check Boxes

  • Choose Source for Value: Groovy Script, and it's content is:

def prop = readJSON file: "${env.WORKSPACE}/input.json"
def version_list = prop.versions
return version_list.join(',')

When I build this job, there is nothing to choice for parameter version_list, but the output shows it can read data from input.json, this is one output:

Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /home/hdl/.jenkins/workspace/json_as_parameter
[Pipeline] {
[Pipeline] stage
[Pipeline] { (load json)
[Pipeline] echo
version_list = null
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (open_json)
[Pipeline] script
[Pipeline] {
[Pipeline] readJSON
[Pipeline] echo
"V1","V2"
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

The load json stage can not get the value of ${params.version_list} but the open_json stage can load json properly.

If is replace groovy script of versison_list simpley as follows:

def version_list = ["V1","V2"]
return version_list.join(',')

It works well.

I don't know why. Any help, thanks!

1 Answer 1

0

It looks like there might be a small issue with how the Extended Choice Parameter is set up.

Try this:

pipeline {
    agent any

    parameters {
        extendedChoice(
            name: 'version_list',
            type: 'PT_CHECKBOX',
            groovyScript: '''
                import groovy.json.JsonSlurper

                def jsonFile = new File("${WORKSPACE}/input.json")
                def jsonContent = new JsonSlurper().parseText(jsonFile.text)
                return jsonContent.versions
            '''
        )
    }

    stages {
        stage("load json") {
            steps {
                echo "version_list = ${params.version_list}"
            }
        }
        stage("open_json") {
            steps {
                script {
                    def prop = readJSON file: "${env.WORKSPACE}/input.json"
                    def version_list = prop.versions

                    println version_list.join(',')
                }
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.