2

Is there an easy way to find out which branch triggered a build process in Jenkins? I am using the Jenkins GitHub plugin and GitHub Webhooks.

Instead of setting up a CI Job for each Branch I want to create a central script that will handle this automatically.

Any Ideas?

Cheers!

therod
  • 174
  • 2
  • 9

2 Answers2

1

i had a look into the source and it looks like the GitHub plugin uses the Git plugin under the hood: http://wiki.hudson-ci.org/display/HUDSON/Git+Plugin

if that's the case, you should be able to use the env-var GIT_BRANCH

phoet
  • 18,243
  • 3
  • 42
  • 71
  • I just tried this recently and it's not working as expected. I'm also using the Jenkins Github plugin version 1.19.2 and `GIT_BRANCH` is `null` both when the build is triggered by pushing and when it's triggered by merging a pull request. Any pointers? – Mig82 Jun 19 '17 at 10:59
  • GIT_BRANCH and GIT_COMMIT should be set. look into the current documentation https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin – phoet Jun 20 '17 at 12:28
-1

I'm using version 1.19.2 of the Github Plugin and relying on the GIT_BRANCH didn't work. This and the other variables specified in the Git plugin's documentation are all null in my case. Don't know why.

So, after researching these posts here and here I decided to use the git-name-rev command. This is how I did it:

def cmd = 'git name-rev --name-only HEAD > branch'
isUnix()?sh(cmd):bat(cmd)
def branch = readFile('branch').trim()
echo("Branch is '${branch}'")

I hope this helps others.

Mig82
  • 3,665
  • 1
  • 30
  • 55
  • if you have a comment, please use the comment functionality. – phoet Jun 20 '17 at 12:29
  • Hi @phoet , this is not a comment. Look again. It's an answer. Since this is a misunderstanding, I would appreciate it if you could remove the down-vote. – Mig82 Jun 20 '17 at 13:13
  • don't know what happened here... unfortunately i can't revoke my vote unless the answer has been edited. – phoet Jul 03 '17 at 15:13