14

I have setup Jenkins to automatically build several git branches of my project. The build also triggers Sonar analysis, as a post-build action.

The problem is that all branches point to the same Sonar project. I know that there is a sonar.branch property. Is it possible to have Jenkins automatically set sonar.branch property to the current git branch being built (without having to change project's pom.xml)?

spyk
  • 878
  • 1
  • 9
  • 26

5 Answers5

24

You can try adding -Dsonar.branch.name=something in the field MAVEN_OPTS in jenkins post-build action (Advanced configuration).

I don't know how to resolve something, since I don't know how you configure your jenkins job... but it will probably be something like $git.branch.

I didn't try, so i'm not sure it will work.

In a Jenkins multi-branch pipeline, the variable is BRANCH_NAME.

P.S. While reading your question it's difficult to say if you are using maven or not to trigger sonar. If you are not using maven: there is a project properties field in the Jenkins config where you can define sonar.branch.

Rick Moritz
  • 1,449
  • 12
  • 25
ben75
  • 29,217
  • 10
  • 88
  • 134
  • 1
    I ended up using the approach you suggested, using different jobs for each branch. I do use Jenkins to trigger sonar, so I set the sonar.branch property accordingly for each job. I think this has also the advantage that the build job is triggered only if a commit is made for this particular branch, and also you can create more fine-grained build schedule control/notifications for more important branches etc. – spyk Mar 25 '13 at 09:52
  • Not working for me :-( I added $GIT_BRANCH in MAVEN_OPTS. still when I am building from some other branch through some other job - the dashboard showing coverage is changing to the results from the latest branch run. I want it to remain same for my branch. – R11G Jul 05 '13 at 11:27
  • Branch name will be experted either as env variable or script interpolation. – Antoniossss Dec 28 '18 at 11:48
  • 2
    I think the `sonar.branch.name` property is only available in the commercial editions but not in the community edition. – Datz Jan 21 '20 at 11:22
  • 2
    @Datz, at the time this question was asked, 7 years ago, it was available in the Community Edition. You're correct that it is now only in Developer Edition and above. – Denham Coote Dec 09 '20 at 11:59
4

This should work

mvn sonar:sonar -U -Dsonar.branch.name=$BRANCH_NAME
Rick Moritz
  • 1,449
  • 12
  • 25
Gnani
  • 139
  • 1
  • 8
1

I am not sure if this helps you with the automated Jenkins execution, but to run a mvn sonar:sonar with the proper branch set, you can run the command (note the back-quotes), if you are in a Unix based environment:

mvn -Dsonar.branch=`git rev-parse --abbrev-ref HEAD` sonar:sonar
JeanMarc
  • 306
  • 1
  • 10
0

Did you use build parameters in order to specify the branch?

If yes, you can easily tell to Sonar which branch is it, juste add in config textarea:

sonar.branch=${branch}

Where branch is the name of the build parameter.

Thomas Decaux
  • 21,738
  • 2
  • 113
  • 124
  • sonar community ed. 9.5.0 - I got this message "The 'sonar.branch' parameter is no longer supported. You should stop using it. Branch analysis is available in Developer Edition and above. See https://redirect.sonarsource.com/editions/developer.html for more information". – TudorIftimie Jun 30 '22 at 10:09
0

For achieving this I generally use -Dsonar.branch.name=${env.CHANGE_BRANCH}

Alessandro S.
  • 875
  • 12
  • 24