简体   繁体   English

Jenkins-管道访问git分支名称

[英]Jenkins - Pipeline access git branch name

Question: Is there any way to get the name of the branch that is checkout by the Pipeline in a jenkinsfile? 问题:是否有任何方法可以在jenkinsfile中获取由管道签出的分支的名称?

NOTE : I am not in a Multibranch Pipeline! 注意 :我不在多分支管道中!

In my pipeline im using 在我的管道中即时通讯使用

checkout([$class: 'GitSCM', 
          userRemoteConfigs: [[url:'https://github.com/repo', 
                               credentialsId: 'xxxx',name: '1234']]])

where name is the commit ID. 其中name是提交ID。 I need to verify that this commit id is from the branch that a user has specified as parameter (user specifies both branch name and commit id and I need to check if there is no error before building the project). 我需要验证此提交ID来自用户已将其指定为参数的分支(用户同时指定了分支名称和提交ID,并且我需要在构建项目之前检查是否没有错误)。

Why is the branch name not exposed in Pipeline but only in Multibranch Pipeline? 为什么分支名称未在管道中公开,而仅在多分支管道中公开? Doesn't make sense to me. 对我来说没有意义。

There are three reasons why I don't want to convert to the multi branch pipeline: 我不想转换为多分支管道的原因有三个:

  1. I am using another plugin Build Blocker Plugin in my pipeline that doesn't seem available in the multibranch pipeline. 我在管道中使用了另一个插件Build Blocker插件 ,该插件在多分支管道中似乎不可用。
  2. I have a parameterized pipeline, I don't see that option in the multibranch pipeline. 我有一个参数化管道,在多分支管道中没有看到该选项。
  3. A lot of time has gone into this pipeline, I simply don't want to set up this project again from scratch just for this branch name feature. 很多时间进入了这个管道,我只是不想仅仅为了这个分支名称功能而从头开始设置这个项目。

I have printed out all environment variables, branch name is not among them. 我已经打印出所有环境变量,分支名称不在其中。

Thanks for the help! 谢谢您的帮助!

if you are using pipeline code I assume you are doing something like: 如果您使用的是管道代码,那么我假设您正在执行以下操作:

checkout scm

Nice thing is this returns a map with some useful stuff from which you can grab the checked out branch in the following manner: 令人高兴的是,这将返回一个包含一些有用内容的地图,您可以通过以下方式从中获取已签出的分支:

def scmVars = checkout scm def branchName = scmVars.GIT_BRANCH

See Pipeline: Nodes and Processes, sh : Shell Script : 请参阅管道:节点和进程, sh :Shell脚本

returnStdout (optional) returnStdout (可选)

If checked, standard output from the task is returned as the step value as a String , rather than being printed to the build log. 如果选中,则任务的标准输出将作为步骤值作为String ,而不是打印到构建日志中。 (Standard error, if any, will still be printed to the log.) You will often want to call .trim() on the result to strip off a trailing newline. (如果有标准错误,仍然会打印到日志中。)您通常会希望在结果上调用.trim()来删除尾随的换行符。

and see git-branch : 并查看git-branch

--contains [<commit>] -包含[<commit>]
Only list branches which contain the specified commit (HEAD if not specified). 仅列出包含指定提交的分支(如果未指定,则为HEAD)。 Implies --list . 暗示--list

Combine these with: 将它们与:

...
def branches = sh(returnStdout: true, script: "git branch --contains ${commitId}")
...

and check whether branches contains the branch name given by the user. 检查branches是否包含用户指定的分支名称。

BTW, I'd rename your variables since: 顺便说一句,我重命名了您的变量,因为:

  • name is your commit ID, aka (commit) hash, so I'd call it commitId or commitHash name是您的提交ID,又名(提交)哈希,因此我将其commitIdcommitHash
  • you actually have some other thing with a name, the branch , which can be called branchName then to make future maintainers' lifes easier 您实际上还有其他名称,即branch ,可以称为branchName ,这样可以使将来的维护者的生活更轻松

Regarding: 关于:

Why is the branch name not exposed [...]? 为什么分支名称不公开[...]?

see Git for Computer Scientists : 请参阅计算机科学家的Git

refs : References, or heads or branches, are like post-it notes slapped on a node in the DAG [Directed Acyclic Graph]. refs :引用或头或分支,就像在DAG [有向无环图]中的某个节点上拍的便笺一样。 Where as the DAG only gets added to and existing nodes cannot be mutated, the post-its can be moved around freely. 在仅添加DAG且无法更改现有节点的情况下,后期可以自由移动。 They don't get stored in the history, and they aren't directly transferred between repositories. 它们不会存储在历史记录中,也不会在存储库之间直接传输。 They act as sort of bookmarks, "I'm working here". 它们就像书签一样,“我在这里工作”。

Such it's easy to find a commit from a branch: by walking down from the branch ref through the graph, but it's tricky the other way round since a commit can belong to more than one branch (if the branch is once again branched later). 这样就很容易从分支中找到一个提交:通过从分支ref遍历整个图,但是反过来比较棘手,因为一个提交可以属于多个分支(如果该分支以后再分支一次)。 Eg from Pro Git, 3.1 Git Branching - Branches in a Nutshell where the three commits to the left belong to branches master and testing : 例如,来自Pro Git的3.1 Git分支-坚果壳中的分支,其中左边的三个提交都属于分支master testing

在此处输入图片说明


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

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