简体   繁体   中英

How to take the result of a precious build step in Jenkins with Groovy?

Is there an example script of taking the previous build step's return code? I would like to know how to do this with Groovy. The previous build step is running an SSH command remotely and returns a specific return code. How can I read this return code in the next build step with Groovy?

If you go to the pipeline snippet generator from a pipeline job in the Jenkins UI (click on 'pipeline syntax' on the left) it will give you the syntax of each step like "sh". For a shell command you can do it like this example:

pipeline {

    // Assumes you have Linux agents..
    agent any

    stages{
        stage('Test') {
            steps {
                script {

                    def result = sh returnStatus: true, script: 'ls -a'

                    echo "Return code of shell script: ${result}"
                }
            }
        }
    }
}

I don't know if there is any way of getting it for the previous step, if you did not get the result like this for every step though.

In case of failure, when the returnStatus is explicitly requested like this, an exception is not thrown so you will need to handle the return status and fail the job explicitly with error('message..') if that is what is required.

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