[英]How to assign git commit hash to a variable in Jenkins File
I am trying to assign the git commit hash to a variable defines in Jenkins pipeline as follows我正在尝试将 git 提交 hash 分配给 Jenkins 管道中定义的变量,如下所示
GIT_COMMIT_HASH = sh "(git log -n 1 --pretty=format:'%H')"
This will print the commit hash in Jenkins build log but it fails to assign the value.这将在 Jenkins 构建日志中打印提交 hash,但无法分配该值。
When I try to print the value using当我尝试使用打印值时
steps{
script {
GIT_COMMIT_HASH = sh "(git log -n 1 --pretty=format:'%H')"
echo "**************************************************"
echo "${GIT_COMMIT_HASH}"
echo "**************************************************"
}
}
This will results null这将导致 null
How may I assign the value?我该如何分配价值?
你必须告诉sh脚本将stdout返回给你的脚本,而不是仅仅将它转储到stdout。
GIT_COMMIT_HASH = sh (script: "git log -n 1 --pretty=format:'%H'", returnStdout: true)
You may use this inside the environment block您可以在环境块中使用它
GITCOMMIT="${sh(returnStdout: true, script: 'git rev-parse HEAD')}" GITCOMMIT="${sh(returnStdout: true, script: 'git rev-parse HEAD')}"
"You can define a helper function and then call it within your pipeline您可以定义一个助手 function 然后在您的管道中调用它
def getCommitSha() {
return sh(returnStdout: true, script: 'git rev-parse HEAD')
}
// to an env var
env.GIT_COMMIT_HASH = getCommitSha()
// to a var within the pipeline
GIT_COMMIT_HASH = getCommitSha()
为什么不直接使用内置的环境变量GIT_COMMIT<\/code> ?
"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.