Search code examples
jenkinsgitlabsonarqubesonarqube-scansonarscanner

How do I get git current branch in Jenkins and pass it to Sonar scanner?


I have a Jenkins Job that runs Sonnar Scanner to publish analysis to SonarQube. The job triggers on push to Gitlab. On the Jenkins job configuration, I've setup the location of the Git repo with a "Branch Specifier" '**' which is recognized as wildcard that includes the separator '/'. As the Git repo is cloned, it appears to be in detached HEAD state and the branch is unknown in this context.

This displays nothing :

git branch --show-current

This displays HEAD:

git rev-parse --abbrev-ref HEAD

I need to get the current branch and pass it to Sonar scanner into property sonar.branch.name, otherwise the results are just shown under "master" in SonarQube, which is not correct.

How do I do this ?


Solution

  • The solution was to use the environment variable provided by the Gitlab plugin :

    sonar.branch.name=${gitlabSourceBranch}