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

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, @anon67236913 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.

We recently upgraded our sonar to 7.9.5 LTS version and we are also running into same issue but for us Sonar upgrade to 8 is not an option and we are using windows scanner. Is there any equivalent commands available for windows?

I’m not sure if I understand the question. It sounds to me that you are asking

is it possible to use if-else blocks on Windows?

Yes, it is: Microsoft “if” command.