Clarify the use of `sonar.branch.name`

My team found the usage of property sonar.branch.name confusing. They executed the following command on a fresh, previously never analyzed Git repo:

sonar-scanner -Dsonar.branch.name=master ...

The result was:

ERROR: Project was never analyzed. A regular analysis is required before a branch analysis

While the documentation clearly says:

A branch is created when the sonar.branch.name parameter is passed during analysis.

It may not be so obvious, as it happened with us, thus I think there could be more emphasis in the documentation on the fact that we should never use the property for the main branch.

Thanks in advance.

Hi,

i guess you’re still on Sonarqube 7.9.1 ?
This a known (annoying) shortcoming in this version with the use of sonar.branch.name in the
initial analysis. This is fixed in Sonarqube 8
But there’s a simple workaround.
Create a shared library for jenkins pipelines. It checks via Sonarqube web api for
project existence and - if it not exists already - create it. Call it before the analysis stage and all fine.
Otherwise you may create the project also manually via Sonarqube web ui before the analysis.

Gilbert

1 Like

The problem is caused that SQ tries to compare sonar.branch.name=master with the main branch. A very simple way to solve it is to not adding sonar.branch.name when you scan your main branch. Example in Jenkinsfile style:

sh """
    sonar-scanner ${branch != 'master' ? "-Dsonar.branch.name=${branch}" : ''} ... 
"""

Thanks, @Rebse and @agabrys. Yes we’re still on SonarQube 7, and upgrading to 8 should be the solution then. And we’ll be using the suggested shell script as a workaround for now.