我正尝试在我的 Jenkinsfile 中提取 git 分支和提交信息,如下所示:
def commit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
def branch = sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()
之后我想像这样打印它:
println("Branch: ${branch}, Commit: ${commit}")
我没有得到真正的值(value),而是留下了这个:
Branch: org.jenkinsci.plugins.pipeline.modeldefinition.ClosureModelTranslator@545511bf, Commit: org.jenkinsci.plugins.pipeline.modeldefinition.ClosureModelTranslator@545511bf
我做错了什么吗?我该如何正确检索我需要的值?
编辑:不,建议的重复不是答案,因为我知道用于检索我需要的信息的 shell 命令。我的问题是信息传递给我的方式,作为 ClosureModelTranslator
而不是 String
。
最佳答案
这个完整的流程适合您吗?使用 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}")
}
}
}
}
}
关于git - 从 Jenkins 管道捕获 shell 脚本输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43738237/