【问题标题】:Is it possible to capture the stdout from the sh DSL command in the pipeline是否可以从管道中的 sh DSL 命令捕获标准输出
【发布时间】:2016-07-30 04:16:39
【问题描述】:

例如:

var output=sh "echo foo";
echo "output=$output";

我会得到:

output=0

所以,显然我得到的是退出代码而不是标准输出。是否可以将标准输出捕获到管道变量中,这样我可以获得: output=foo 作为我的结果?

【问题讨论】:

    标签: jenkins jenkins-workflow jenkins-pipeline


    【解决方案1】:

    Nowsh step 支持通过提供参数returnStdout 返回stdout

    // These should all be performed at the point where you've
    // checked out your sources on the slave. A 'git' executable
    // must be available.
    // Most typical, if you're not cloning into a sub directory
    gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
    // short SHA, possibly better for chat notifications, etc.
    shortCommit = gitCommit.take(6)
    

    this example

    【讨论】:

    • 注意这个答案的.trim()部分,否则你可能会在行尾得到一个换行符
    • append --short to rev-parse 可以直接得到一个短哈希
    • 不确定是什么导致失败,但我也必须像这样gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').toString().trim() 将输出转换为字符串
    • 嗨,'.take(6)' 代表什么?
    • @Vano 指的是 Groovy 方法 take(),在这种情况下它将获取前 6 个字符。 docs.groovy-lang.org/docs/groovy-2.3.2/html/api/org/codehaus/…
    【解决方案2】:

    注意:链接的 Jenkins 问题已经解决。

    正如JENKINS-26133 中提到的,无法将 shell 输出作为变量获取。作为一种解决方法,建议使用从临时文件中读取的命令。所以,你的例子看起来像:

    sh "echo foo > result";
    def output=readFile('result').trim()
    echo "output=$output";
    

    【讨论】:

    • 对于新手,请参阅下面的答案stackoverflow.com/a/38912813/345845,因为将新的returnStdout 参数传递给sh 步骤后,这变得更容易了。
    • “不可能将 shell 输出作为变量” - 不正确。这是一个 hack,正确答案是 returnStdout。
    • 如果你需要 both 来自 shell 命令的 stdoutexit status,这实际上是一个好的答案。其他时候,使用returnStdout 参数。
    【解决方案3】:

    试试这个:

    def get_git_sha(git_dir='') {
        dir(git_dir) {
            return sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
        }
    }
    
    node(BUILD_NODE) {
        ...
        repo_SHA = get_git_sha('src/FooBar.git')
        echo repo_SHA
        ...
    }
    

    测试日期:

    • 詹金斯版本。 2.19.1
    • 流水线 2.4

    【讨论】:

      【解决方案4】:

      您也可以尝试使用此函数来捕获 StdErr StdOut 并返回代码。

      def runShell(String command){
          def responseCode = sh returnStatus: true, script: "${command} &> tmp.txt" 
          def output =  readFile(file: "tmp.txt")
      
          if (responseCode != 0){
            println "[ERROR] ${output}"
            throw new Exception("${output}")
          }else{
            return "${output}"
          }
      }
      

      注意:

      &>name means 1>name 2>name -- redirect stdout and stderr to the file name
      

      【讨论】:

        【解决方案5】:

        一个简短的版本是:

        echo sh(script: 'ls -al', returnStdout: true).result
        

        【讨论】:

          【解决方案6】:
          def listing = sh script: 'ls -la /', returnStdout:true
          

          参考:http://shop.oreilly.com/product/0636920064602.do第433页

          【讨论】:

            【解决方案7】:

            我遇到了同样的问题,并且在我知道我在错误的街区尝试之后发现了几乎所有的东西。我在步骤块中尝试它,而它需要在环境块中。

                    stage('Release') {
                                environment {
                                        my_var = sh(script: "/bin/bash ${assign_version} || ls ", , returnStdout: true).trim()
                                            }
                                steps {                                 
                                        println my_var
                                        }
                            }
            

            【讨论】:

              猜你喜欢
              • 2015-02-05
              • 1970-01-01
              • 1970-01-01
              • 2021-04-29
              • 2018-02-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-08-10
              相关资源
              最近更新 更多