0

我用凹槽脚本读取了一个 shell 脚本文件 /tmp/cmd_list.sh 并创建了一个动态阶段来构建。

/tmp/cmd_list.sh 的内容是:

ls
pwd
aaaaaa
who

只有“aaaaaa”mut 无法执行(退出代码 127)。我的问题是,所有阶段都标记为失败,但是当我看到日志时,“ls”、“pwd”和“who”等命令工作正常,返回码为 0。

我试图关注盒子的阶段状态,但没有成功......我的 Groove 脚本(Jenkinsfile):

import hudson.model.Result

node('master') {

    stage ('\u27A1 Checkout'){
        sh "echo 'checkout ok'"
    }

    def BUILD_LIST = readFile('/tmp/cmd_list.sh').split()
    for (CMDRUN in BUILD_LIST) {

        def status;

        try {
            node{
                stage(CMDRUN) {

                    println "Building ..."

                    status = sh(returnStatus: true, script: CMDRUN )
                    println "---> EX CODE: "+ status

                    if(status == 0){
                        currentBuild.result = 'SUCCESS'
                        currentBuild.rawBuild.@result = hudson.model.Result.SUCCESS
                    }
                    else{ 
                        currentBuild.result = 'UNSTABLE'
                        currentBuild.rawBuild.@result = hudson.model.Result.UNSTABLE
                    }

                    def e2e = build job:CMDRUN, propagate: false

                }
            }
        }
        catch (e) {
            println "===> " + e
            currentBuild.result = 'UNSTABLE'

            println "++++> EX CODE: "+ status

            if(status == 0){ 
                println "++++> NEW STATUS: "+ status
                currentBuild.rawBuild.@result = hudson.model.Result.SUCCESS
                currentBuild.result = 'SUCCESS'
            }
            else{
                println "++++> NEW STATUS: "+ status
                currentBuild.rawBuild.@result = hudson.model.Result.UNSTABLE
            }

        }


    }

}

结果是:

在此处输入图像描述

任何人都可以帮助我显示正确的状态?谢谢!

4

2 回答 2

0

我更改了脚本,现在按预期工作!

新代码:

node('master') {

    def build_ok = true

    stage ('\u27A1 Checkout'){
        sh "echo 'checkout ok'"
    }


    def BUILD_LIST = readFile('/tmp/cmd_list.sh').split()
    for (CMDRUN in BUILD_LIST) {

        try {
            stage(CMDRUN) {

                println "Building ..."
                sh CMDRUN

            }

        }
        catch (e) { build_ok = false }

    }


    if(build_ok) { currentBuild.result = "SUCCESS" }
    else { currentBuild.result = "FAILURE" }

}

预期结果

于 2019-02-14T18:05:35.373 回答
0

waldir 回答的小改进

node('master') {

    def build_ok = true

    stage ('\u27A1 Checkout'){
        sh "echo 'checkout ok'"
    }


    def BUILD_LIST = readFile('/tmp/cmd_list.sh').split()
    for (CMDRUN in BUILD_LIST) {

        catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
            stage(CMDRUN) {

                println "Building ..."
                sh CMDRUN

            }

        }


    }


    if(build_ok) { currentBuild.result = "SUCCESS" }
    else { currentBuild.result = "FAILURE" }

}
于 2021-11-30T11:27:48.977 回答