簡體   English   中英

Groovy 腳本在 Jenkins 作業中失敗,但從命令行運行良好

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

為簡潔起見,我將以下 Managed Jenkinsfile for Pipeline Job 剝離了階段。

#!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 文件更新時自動配置作業的參數(我完全知道 JSON 更新后運行的第一個作業將不包含預期的更改,但這對我們來說沒問題)。 MPSS_FLAVORTARGET按預期顯示值。 但是, BUILD_CMD Choice 參數返回錯誤。 當我在命令行中使用靜態定義的MPSS_FLAVORTARGET運行 groovy 腳本代碼時,腳本工作正常,並且返回的build_cmds符合預期。 但是通過Jenkins UI它顯示為錯誤(后備腳本)

我已經嘗試了幾次迭代,但都沒有成功。 我確定有一個小錯誤,我可以弄清楚。

我的問題是有什么方法可以查看用於選擇參數的腳本的打印日志以隔離問題。

更新相同的代碼適用於作業中定義的 Jenkins 文件。 問題似乎特定於托管 Jenkins 文件

只需使用 try-catch 包裝腳本

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

在這種情況下,您將看到錯誤作為參數值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM