1

I'm using Jenkins for running jobs automatically. I'm using also Bitbucket Server as a version control system.

I set up my Jenkins job DSL on any changes in my Bitbucket repo(on each commit) using some groovy script for job creation:

job(jobName) {
  steps {
    // Doing some staff. Need name of branch where last commit was done
  }
  scm {
      git('ssh://git@<my_git_host>/<my_project>/<my_repo>.git', '**')
  }
  triggers {
    bitbucketPush()
    scm ''
  }

So, when I'm doing some commits into my_repo this job runs and I want to extract name of branch where commit was done. Is it possible to do it somehow?

P.S. on Jenkins side(UI) I can see name of branch for current execution, but I have no idea how to extract it on groovy side.

smart
  • 1,975
  • 5
  • 26
  • 46
  • Are you looking for the plan to run on the branch where the commit happens? Or just a specific branch every time there is a commit on it? – KeepCalmAndCarryOn Feb 26 '17 at 19:36
  • @KeepCalmAndCarryOn, I'm interesting on each branch from my repository where commit happens – smart Feb 26 '17 at 21:38

1 Answers1

1

Basically, the branch information will be stored in the Environment Variables with ID "GIT_BRANCH" for each build, So there are lot ways to extract it.

you can simply add the following script

job(jobName) {
  steps {
       shell('''
               echo $GIT_BRANCH
             '''.stripIndent().trim())
            }
  scm {
      git('ssh://git@<my_git_host>/<my_project>/<my_repo>.git', '**')
  }
  triggers {
    bitbucketPush()
    scm ''
  }