0

Im readind a shell script file /tmp/cmd_list.sh with groove script and creating a dynamic stage to build.

The content of /tmp/cmd_list.sh is:

ls
pwd
aaaaaa
who

Only "aaaaaa" mut fail to execute (exit code 127). My problem is, all stages are marked as failed, but when i see the log, comands such as "ls", "pwd" and "who" work fine fine and return code is 0.

I tried to foce stage status for box, but without sucess ... My Groove script (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
            }

        }


    }

}

And the result is:

enter image description here

Anyone can help me to show right status? Thank you!

snieguu
  • 2,073
  • 2
  • 20
  • 39

2 Answers2

0

I changed my script and now work as expected!

new code:

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

}

expected Result

0

Small improvement on waldir's answer

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

}
Benoit Descamps
  • 464
  • 5
  • 7