I am trying to assign the git commit hash to a variable defines in Jenkins pipeline as follows
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.
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
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
You can define a helper function and then call it within your pipeline
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> ?
"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.