【问题标题】:How to return stdout and stderr together with the status from a Jenkins Pipeline sh script step如何从 Jenkins Pipeline sh 脚本步骤返回 stdout 和 stderr 以及状态
【发布时间】:2021-10-28 05:36:22
【问题描述】:

这个问题的答案受到@codeGeass'How to execute a command in a Jenkins 2.0 Pipeline job and then return the stdout 答案的评论的启发:

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

【问题讨论】:

  • A solution was provided 类似的问题,但仅适用于 Jenkins 之外的 shell cmd。
  • @IanW 感谢您的信息。我认为 OP 的需求“Filter stderr”使事情变得复杂......而且......正如你所说,它没有 Jenkins。
  • 不。我应该吗?
  • 我不再想知道为什么有些 Q 几乎立即被否决并关闭,而其他显然应该被否决并关闭或只是删除的 Q(家庭作业类型、没有研究、帮助台等)似乎从来没有.也许改变措辞不说自我回答,而是在这个 Q 的扩展中添加场景......

标签: shell jenkins jenkins-pipeline stdout stderr


【解决方案1】:

简单地说 (Windows Git) Bash 它的工作原理如下(灵感来自@Ian W 对上述问题的评论):

stdErrAndOutAndStatus.sh

stdErrAndOutAndStatus () {
    $1 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

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

【讨论】:

    【解决方案2】:

    要返回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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 2023-03-23
      • 2018-03-04
      • 1970-01-01
      • 2019-11-13
      • 1970-01-01
      相关资源
      最近更新 更多