4

Question: Is there any way to get the name of the branch that is checkout by the Pipeline in a 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. 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).

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.
  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!

2
  • Where are you getting the commitid from ??? If it's a hook can you not add the branch name is a parameter ??? Commented Jun 29, 2017 at 19:43
  • the user specifies the commit he want to get built. Something along the lines `parameters { string(defaultValue: 'develop', description:'', name: '123456')}. This checks out the commit 123456 if the parameter is passed to build the pipeline and develop by default. Commented Jun 29, 2017 at 20:16

3 Answers 3

11

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

Sign up to request clarification or add additional context in comments.

1 Comment

you hero! <3 Actually nearly everything else didn't work when using pipelines! Saved my day!
0

See Pipeline: Nodes and Processes, sh: Shell Script :

returnStdout (optional)

If checked, standard output from the task is returned as the step value as a String, rather than being printed to the build log. (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.

and see git-branch:

--contains [<commit>]
      Only list branches which contain the specified commit (HEAD if not specified). Implies --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.

BTW, I'd rename your variables since:

  • name is your commit ID, a.k.a. (commit) hash, so I'd call it commitId or commitHash
  • you actually have some other thing with a name, the branch, which can be called branchName then to make future maintainers' lifes easier

2 Comments

One thing I never understood about the checkout command in Jenkins. you can either use the key namein userRemoteConfigs with either <commitId> or <branchName> as values or you can use the key branches to specify the <branchName>. Are you suggesting to define variables commitId and branchName which are then passed as values to userRemoteConfig? I cannot alter the key name to something else.
@tenticon According to "checkout, $class: GitSCM userRemoteConfigsname – [is] ID of the repository", not a hash or branch name and "branches – Nested Object – name – Specify the branches if you'd like to track a specific branch in a repository.".
0

Regarding:

Why is the branch name not exposed [...]?

see Git for Computer Scientists:

refs: References, or heads or branches, are like post-it notes slapped on a node in the DAG [Directed Acyclic Graph]. Where as the DAG only gets added to and existing nodes cannot be mutated, the post-its can be moved around freely. 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). E.g. from Pro Git, 3.1 Git Branching - Branches in a Nutshell where the three commits to the left belong to branches master and testing:

enter image description here


2 Comments

How does this help me in finding the name of the branch in the jenkins pipeline? sorry if I don't understand the obvious.
@tenticon Agreed, this doesn't help you in this case. It's an answer with background information to your question "Why is the branch name not exposed ...?". Sorry, if that wasn't obvious.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.