Sonar analysis using multiple git branches in Jenkins
Solution 1
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
.
Solution 2
This should work
mvn sonar:sonar -U -Dsonar.branch.name=$BRANCH_NAME
Solution 3
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
spyk
Java/Scala developer and Data/ML engineer with more than 15 years of industry experience. Interested in enterprise search (Solr, Elasticsearch, Lucene) and big data applications (Scala, Spark).
Updated on July 05, 2022Comments
-
spyk almost 2 years
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 about 11 yearsI 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.
-
R11G almost 11 yearsNot 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.
-
Antoniossss over 5 yearsBranch name will be experted either as env variable or script interpolation.
-
Datz over 4 yearsI think the
sonar.branch.name
property is only available in the commercial editions but not in the community edition. -
Denham Coote over 3 years@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.
-
Iftimie Tudor almost 2 yearssonar 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 redirect.sonarsource.com/editions/developer.html for more information".