简体   繁体   English

从Jenkins管道捕获shell脚本输出

[英]Capturing shell script output from Jenkins Pipeline

I am trying to extract git branch and commit information in my Jenkinsfile as following: 我试图提取git分支并在我的Jenkinsfile中提交信息,如下所示:

def commit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
def branch = sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()

I am trying to print it afterwards like this: 我试着像这样打印它:

println("Branch: ${branch}, Commit: ${commit}")

Instead of getting real values, I am left with this: 我没有获得真正的价值,而是留下了这个:

Branch: org.jenkinsci.plugins.pipeline.modeldefinition.ClosureModelTranslator@545511bf, Commit: org.jenkinsci.plugins.pipeline.modeldefinition.ClosureModelTranslator@545511bf

Am I doing something wrong and how can I retrieve values I need properly? 我做错了什么,如何正确检索我需要的值?

Edit: No, the suggested duplicate is not an answer, because I am aware of the shell commands used to retrieve the info I need. 编辑:不,建议的副本不是答案,因为我知道用于检索我需要的信息的shell命令。 My Problem is the way that info is being delivered to me, as a ClosureModelTranslator instead of a String . 我的问题是信息传递给我的方式,作为ClosureModelTranslator而不是String

does this full pipeline work for you? 这个完整的管道对你有用吗? working for me with Pipeline plugin 2.4. 使用Pipeline插件2.4为我工作。

pipeline {
  agent { label 'docker' }
  stages {
    stage("test_capture_output_and_print") {
      steps {
        script {
          def commitSha = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
          println("commitSha: ${commitSha}")
        }
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM