如何从 Jenkins Pipeline sh 脚本步骤返回 stdout 和 stderr 以及状态 [英] How to return stdout and stderr together with the status from a Jenkins Pipeline sh script step

查看:24
本文介绍了如何从 Jenkins Pipeline sh 脚本步骤返回 stdout 和 stderr 以及状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的答案的灵感来自 @codeGeass'如何在 Jenkins 2.0 流水线作业中执行命令,然后返回标准输出:

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:

我们可以一起使用它们吗?在一个变量中捕获 returnStdout 并在另一个中捕获 returnStatus ?因为重复两次sh脚本不爽

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

推荐答案

要返回 stdoutstderr 以及状态,您可以执行以下操作:

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

控制台输出

[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

这篇关于如何从 Jenkins Pipeline sh 脚本步骤返回 stdout 和 stderr 以及状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆