从Jenkins管道捕获shell脚本输出

ior*_*vic 3 git shell groovy jenkins jenkins-pipeline

我试图提取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()
Run Code Online (Sandbox Code Playgroud)

我试着像这样打印它:

println("Branch: ${branch}, Commit: ${commit}")
Run Code Online (Sandbox Code Playgroud)

我没有获得真正的价值,而是留下了这个:

Branch: org.jenkinsci.plugins.pipeline.modeldefinition.ClosureModelTranslator@545511bf, Commit: org.jenkinsci.plugins.pipeline.modeldefinition.ClosureModelTranslator@545511bf
Run Code Online (Sandbox Code Playgroud)

我做错了什么,如何正确检索我需要的值?

编辑:不,建议的副本不是答案,因为我知道用于检索我需要的信息的shell命令.我的问题是信息传递给我的方式,ClosureModelTranslator而不是一个String.

bur*_*ttk 8

这个完整的管道对你有用吗?使用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}")
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)