简体   繁体   中英

Groovy Script failing in Jenkins job but runs fine from command line

I have the following Managed Jenkinsfile for Pipeline Job stripped of the Stages for brevity.

#!groovy
import groovy.json.JsonSlurperClassic

def json = new File("TEST_JSON.json").text
def data = new JsonSlurperClassic().parseText(json)

def string_1 = data.test

properties([
    parameters([
        string(defaultValue: string_1, description: 'STRING 1', name: 'STRING_1', trim: false), 
        string(defaultValue: string_1, description: 'STRING 2', name: 'STRING_2', trim: false), 
        [$class: 'ChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'MPSS Flavor', 
            filterLength: 1, filterable: true, 
            name: 'MPSS_FLAVOR', randomName: 'choice-parameter-10980926894589', 
            script: [
                $class: 'GroovyScript', 
                fallbackScript: [classpath: [], sandbox: false, script: ''], 
                script: [
                    classpath: [], sandbox: false, 
                    script: '''
                        import groovy.json.JsonSlurperClassic
                        def json = new File("TEST_JSON.json").text
                        def data = new JsonSlurperClassic().parseText(json)
                        mpss_flavors = []
                        for (option in data.options) {
                            println option
                            mpss_flavors.add(option.mpss_flavor)
                        }
                        return mpss_flavors
                    '''
                ]
            ]
        ], 
        [$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'TARGET', 
            filterLength: 1, filterable: true, 
            name: 'TARGET', randomName: 'choice-parameter-10980967122105', 
            referencedParameters: 'MPSS_FLAVOR', 
            script: [
                $class: 'GroovyScript', 
                fallbackScript: [classpath: [], sandbox: false, script: ''], 
                script: [
                    classpath: [], sandbox: false, 
                    script: '''
                        import groovy.json.JsonSlurperClassic
                        def json = new File("TEST_JSON.json").text
                        def data = new JsonSlurperClassic().parseText(json)
                        targets = []
                        for (option in data.options) {
                            if (option.mpss_flavor == MPSS_FLAVOR) {
                                targets.add(option.target);
                            }
                        }
                        return targets;
                    '''
                ]
            ]
        ],
        [$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'Build Command', 
            filterLength: 1, filterable: true, 
            name: 'BUILD_CMD', randomName: 'choice-parameter-11980967122105', 
            referencedParameters: 'TARGET,MPSS_FLAVOR',
            script: [
                $class: 'GroovyScript', 
                fallbackScript: [classpath: [], sandbox: false, script: 'return ["ERROR"]'], 
                script: [
                    classpath: [], sandbox: false, 
                    script: '''
                        import groovy.json.JsonSlurperClassic;
                        def json = new File("TEST_JSON.json").text;
                        def data = new JsonSlurperClassic().parseText(json);
                        build_cmds = [];
                        for (option in data.options) {
                            if ((option.mpss_flavor == MPSS_FLAVOR) && (option.target == TARGET)) {
                                build_cmds.addAll(option.build_commands);
                            }
                        }
                        return build_cmds;
                    '''
                ]
            ]
        ]
    ])
])

And the following JSON File containg the configurations

    {
    "test": "TEST STRING FROM JSON",
    "options": [
        {
            "mpss_flavor": "MPSS1",
            "target": "TARGET1",
            "build_commands": [
                "BUILD_COMMAND_1"
            ]
        },
        {
            "mpss_flavor": "MPSS2",
            "target": "TARGET2",
            "build_commands": [
                "BUILD_COMMAND_2",
                "BUILD_COMMAND_3"
            ]
        }
    ]
}

I would like to configure the Parameters for the Job automatically when the JOSN file is updated (I am fully aware that the first Job run after the JSON Update will not contain the intended changes, but that is Ok for us). MPSS_FLAVOR and TARGET are showing the Values as intended. However the BUILD_CMD Choice parameter is returning an error. When I run the groovy script code with Statically defined MPSS_FLAVOR and TARGET in command line the Script works fine and the returned build_cmds are as expected. However Through Jenkins UI It is showing as ERROR (Fallback Script)

I have tried several iterations without any success. I am sure there is a minor mistake and I Could Figure out.

My Question is there any way to see the print Logs of the script that is used for choice parameter to isolate the issue.

Update Same code works fine for Jenkins file defined within the Job. issue seems to be specific to Managed Jenkins file

just wrap script with try-catch

try{
   ...your parameter script here
}catch(Throwable t){
    return [t.toString()]
}

in this case you'll see error as a parameter value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM