从Jenkins管道捕获shell脚本输出 [英] Capturing shell script output from Jenkins Pipeline

查看:1504
本文介绍了从Jenkins管道捕获shell脚本输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我试图像这样打印它:

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

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

 分支:org.jenkinsci。 plugins.pipeline.modeldefinition.ClosureModelTranslator@545511bf,Commit:org.jenkinsci.plugins.pipeline.modeldefinition.ClosureModelTranslator@545511bf 

我做错了什么,如何检索我需要的值?编辑:不,建议的重复不是答案,因为我知道用于检索我需要的信息的shell命令。我的问题是信息传递给我的方式,如 ClosureModelTranslator 而不是 String

解决方案

这是否适合您?为管道插件2.4工作。

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


I am trying to extract git branch and commit information in my Jenkinsfile as following:

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. My Problem is the way that info is being delivered to me, as a ClosureModelTranslator instead of a String.

解决方案

does this full pipeline work for you? working for me with Pipeline plugin 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}")
        }
      }
    }
  }
}

这篇关于从Jenkins管道捕获shell脚本输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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