Search code examples
shelljenkinsjenkins-pipelinestdoutstderr

How to return stdout and stderr together with the status from a Jenkins Pipeline sh script step


The answers to this question were inspired by @codeGeass' comment to an answer to How to execute a command in a Jenkins 2.0 Pipeline job and then return the stdout:

Can we use them together ? catch returnStdout in a variable and returnStatus in an other ? because it is not cool to repeat the sh script twice


Solution

  • To return stdout and stderr together with the status you can do the following:

    def runScript(command) {
        script {
            sh script: "set +x ; $command 2>&1 && echo \"status:\$?\" || echo \"status:\$?\" ; exit 0", returnStdout: true
        }
    }
    
    pipeline {
        agent any
    
        stages {
            stage('more values from sh') {
                steps {
                    script {
                        // inline
                        def stdoutAndStatus = sh script: 'set +x ; ls -a 2>&1 && echo "status:$?" || echo "status:$?" ; exit 0', returnStdout: true
                        echo "stdoutAndStatus: >$stdoutAndStatus<".trim()
                        
                        def stderrAndStatus = sh script: 'set +x ; ls notexisting 2>&1 && echo "status:$?" || echo "status:$?" ; exit 0', returnStdout: true
                        echo "stderrAndStatus: >$stderrAndStatus<".trim()
                        
                        // with function
                        echo "runScript: >${runScript('ls -a')}<".trim()
                        echo "runScript: >${runScript('ls notexisting')}<".trim()
                        
                        // failing the sh step
                        echo "runScript: >${runScript('not_existing_command')}<".trim()
                    }
                }
            }
        }
    }
    

    Console Output

    [Pipeline] stage
    [Pipeline] { (more values from sh)
    [Pipeline] script
    [Pipeline] {
    [Pipeline] sh
    + set +x
    [Pipeline] echo
    stdoutAndStatus: >.
    ..
    status:0
    <
    [Pipeline] sh
    + set +x
    [Pipeline] echo
    stderrAndStatus: >ls: notexisting: No such file or directory
    status:2
    <
    [Pipeline] script
    [Pipeline] {
    [Pipeline] sh
    + set +x
    [Pipeline] }
    [Pipeline] // script
    [Pipeline] echo
    runScript: >.
    ..
    status:0
    <
    [Pipeline] script
    [Pipeline] {
    [Pipeline] sh
    + set +x
    [Pipeline] }
    [Pipeline] // script
    [Pipeline] echo
    runScript: >ls: notexisting: No such file or directory
    status:2
    <
    [Pipeline] script
    [Pipeline] {
    [Pipeline] sh
    + set +x
    [Pipeline] }
    [Pipeline] // script
    [Pipeline] echo
    runScript: >C:/Users/jenkins/AppData/Local/Jenkins/.jenkins/workspace/SO-36956977 more values from sh@tmp/durable-af2cee22/script.sh: line 1: not_existing_command: command not found
    status:127
    <
    [Pipeline] }
    [Pipeline] // script
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    Finished: SUCCESS