Jenkins dynamic user input using JsonSlurper throwing error

68 views Asked by At

I'm trying to create a dynamic Jenkins pipeline where users can target a cloud container registry and list the tags of a particular image in drop down menu on Jenkins console to deploy it further in selected environments. For this, I came across an article that does exactly what I'm looking for. Unfortunately, the same isn't working for me just yet. The idea is to parse the output (preferably just tags, if not image name included) and have it in a drop down menu on Jenkins console (as shown in article). Any help in this would be greatly appreciated. Thanks.

Error: Jenkins console output throws below error, although the output is close enough (I'd prefer just the tags/version instead of full image name but I'll take what I can get).

groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object

The current character read is 's' with an int value of 115
Unable to determine the current character, it is not a string, number, array, or object
line number 1
index number 0
test-image:01.23 test-image:0b476c72 test-image:1.0.0 test-image:12.22 test-image:20578681 test-image:6902cdaa test-image:b14008ba test-image:c0e6882f 
^
    at groovy.json.internal.JsonParserCharArray.decodeValueInternal(JsonParserCharArray.java:206)
    at groovy.json.internal.JsonParserCharArray.decodeValue(JsonParserCharArray.java:157)
    at groovy.json.internal.JsonParserCharArray.decodeFromChars(JsonParserCharArray.java:46)
    at groovy.json.internal.JsonParserCharArray.parse(JsonParserCharArray.java:384)
    at groovy.json.internal.BaseJsonParser.parse(BaseJsonParser.java:11

Jenkinsfile: Instead of doing curl for API endpoints (like in the example article), I'm using cloud cli command to fetch the images. Additional arguments in "cmd" is for parsing the output to somewhat acceptable format.

import groovy.json.JsonSlurper

def getDockerImages() {
    def cmd =  ['bash', '-c', "artifacts container image list | grep \"test-image:\" | grep -v \"sha\" | awk '{print \$2}' | sed 's/\"//g' | cut -d- -f5 | tr '\n' ' '".toString()]
    def result = cmd.execute().text
    print result
    def slurper = new JsonSlurper()    
    def json = slurper.parseText(result)
    print json
    def tags = new ArrayList()
    if (json.tags == null || json.tags.size == 0)
      tags.add("unable to fetch tags for repo")
    else
      tags.addAll(json.tags)
    return tags.join('\n')
 
}
pipeline {
    agent any
    stages {
        stage("Gather Deployment Parameters") {
            steps {
                timeout(time: 30, unit: 'MINUTES') {
                    script {
                        // Show the select input modal
                       def INPUT_PARAMS = input message: 'Please Provide Parameters', ok: 'Next',
                                        parameters: [
                                        choice(name: 'ENVIRONMENT', choices: ['Test','Pre-Prod'].join('\n'), description: 'Please select the Environment'),
                                        choice(name: 'IMAGE_TAG', choices: getDockerImages(), description: 'Available Docker Images')]
                        env.ENVIRONMENT = INPUT_PARAMS.ENVIRONMENT
                        env.IMAGE_TAG = INPUT_PARAMS.IMAGE_TAG
                    }
                }
            }
        }
        stage("Use Deployment Parameters") {
         steps {
                script {
                    echo "All parameters have been set as Environment Variables"
                    echo "Selected Environment: ${env.ENVIRONMENT}"
                    echo "Selected Tag: ${env.IMAGE_TAG}"
                }
         }
        }
    }
}
0

There are 0 answers