Groovy脚本在Jenkins作业中失败,但可以从命令行正常运行

时间:2018-11-13 06:04:04

标签: jenkins groovy

为了简洁起见,我删除了以下管道作业的Managed Jenkinsfile。

#!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;
                    '''
                ]
            ]
        ]
    ])
])

以及以下JSON文件包含配置

    {
    "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"
            ]
        }
    ]
}

我想在更新JOSN文件时自动为Job配置参数(我完全知道,在JSON Update之后运行的第一个Job将不包含预期的更改,但这对我们来说是可以的)。 MPSS_FLAVORTARGET显示了预期的值。但是,BUILD_CMD选择参数返回错误。当我在命令行中使用静态定义的MPSS_FLAVORTARGET运行groovy脚本代码时,脚本可以正常工作,并且返回的build_cmds与预期的一样。但是,通过Jenkins UI,它显示为ERROR(Fallback脚本)

我尝试了几次迭代都没有成功。我确信这是一个小错误,我可以找出。

“我的问题”有什么办法可以查看用于选择参数以隔离问题的脚本的打印日志。

更新 对于Job中定义的Jenkins文件,相同的代码也可以正常工作。问题似乎特定于Managed Jenkins文件

1 个答案:

答案 0 :(得分:0)

使用try-catch换行脚本

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

在这种情况下,您会看到错误作为参数值