如何 return stdout 和 stderr 以及来自 Jenkins Pipeline sh 脚本步骤的状态

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

这个问题的答案受到@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

要return stdoutstderr连同状态你可以做以下事情:

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()
                }
            }
        }
    }
}

控制台输出

[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

在普通的 (Windows Git) Bash 它的工作原理如下(启发通过 @Ian W 对上述问题的评论):

stdErrAndOutAndStatus.sh

stdErrAndOutAndStatus () {
     2>&1 ; echo "status:$?"
}

stdOutAndStatus=$( stdErrAndOutAndStatus 'ls -a' )
echo -e "$stdOutAndStatus\n"
stdErrAndStatus=$( stdErrAndOutAndStatus 'ls notexisting' )
echo -e "$stdErrAndStatus\n"
stdErrAndStatus=$( stdErrAndOutAndStatus 'not_existing_command' )
echo -e "$stdErrAndStatus\n"

输出

$ ./stdErrAndOutAndStatus.sh
.
..
stdErrAndOutAndStatus.sh
status:0

ls: cannot access 'notexisting': No such file or directory
status:2

./stdErrAndOutAndStatus.sh: line 3: not_existing_command: command not found
status:127

注意:我希望没有陷阱 of/in Bash 我还不知道(还)。我不经常写脚本。