简体   繁体   English

Jenkins管道检索Git分支和哈希

[英]Jenkins pipeline retrieving Git branch and hash

I am trying to retrieve the git branch and hash in the Jenkins pipeline but for some strange reason, the stdout for branch in particular always comes back as 'HEAD'. 我正在尝试在Jenkins管道中检索git分支和哈希,但出于某些奇怪的原因,特别是分支的stdout总是返回为“ HEAD”。 If I run the exact command in bash it works just fine. 如果我在bash中运行确切的命令,它将正常工作。 I get 'develop' and the hash. 我得到“发展”和哈希。

Pipeline 管道

stage('Build') {
    script {
        GIT_BRANCH = sh(returnStdout: true, script: "git rev-parse --abbrev-ref HEAD").trim()
        GIT_HASH = sh(returnStdout: true, script: "git rev-parse --short HEAD").trim()
    }
    echo "branch is: ${GIT_BRANCH}"
    echo "hash is: ${GIT_HASH}"
}

Output 输出量

Running shell script
+ git rev-parse --abbrev-ref HEAD
Running shell script
+ git rev-parse --short HEAD
branch is: HEAD
hash is: dd96820

That seems related to issue JENKINS-45962 : " GIT_BRANCH variable is not populated in jenkins pipeline" 似乎与问题JENKINS-45962有关 :“ jenkins管道中未填充GIT_BRANCH变量”

It refers to JENKINS-26100 , which allows SCM steps to return the revision state (not the branch). 它指的是JENKINS-26100 ,它允许SCM步骤返回修订状态(而不是分支)。

Hence the comment: 因此,评论:

I don't plan to change this, since JENKINS-26100 allows the checkout step (and the git step) to return a map of names and values from the checkout. 我不打算更改此设置,因为JENKINS-26100允许检出步骤(和git步骤)从检出返回名称和值的映射。

Pipelines can (and often do) use multiple checkout steps, and it is cleaner, safer, and more understandable to store the returned values for a specific checkout in a map than it is to read environment variables and decide which checkout provided which environment variables. 管道可以(并且经常这样做)使用多个检出步骤,与读取环境变量并确定哪个检出提供哪些环境变量相比,将特定检出的返回值存储在映射中更加干净,安全和易于理解。

Since there are multiple checkout step, one environment variable (like GIT_BRANCH ) is no longer populated. 由于存在多个签出步骤,因此不再填充一个环境变量(例如GIT_BRANCH )。
Since HEAD is always detached, ... rev-parse would always return a commit, not a branch name. 由于HEAD始终是分离的,因此... rev-parse将始终返回提交,而不是分支名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM