Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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!

like image 550
tenticon Avatar asked Dec 23 '22 16:12

tenticon


1 Answers

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

like image 118
Arjan Schokking Avatar answered Dec 26 '22 10:12

Arjan Schokking