Sonar branch auto-configuring

Dear Sonar community,

I am looking for help to understand how the Sonar Scanner chooses the branch name.

On the one-hand, my understanding is that if the property sonar.branch.name is left empty, the analysis results will be uploaded to the “master” branch.
This is consistent with what I observed when I ran sonar-scanner on my local machine and uploaded the results to SonarCloud.

However, when running the same command from our remote Gitlab-CI machine, I noticed a new line in the sonar-scanner log stating:

INFO: Auto-configuring branch XXX

(with XXX being the name of the GIT branch of the project being analysed), and the analysis results were uploaded in SonarCloud in a new “XXX” short-lived branch instead of the Sonar’s master branch.

In our case, although XXX is not called master, it is our main development branch which serves as a reference for short-lived branches, and as such we want it to be used as the Sonar’s master branch.

I could not find information about what this “auto-configuration” does.
Are there some documentation available that I missed ? Is there an inconsistency with the documentation of the sonar.branch.name property ?

Note: version used:

INFO: SonarScanner 4.3.0.2102
DEBUG: SonarCloud 8.0.0.8830

Thanks a lot !

Stephane

Hello,
Welcome to the community forum!

Your understanding is correct, and in Gitlab-CI the scanner is using the environment variables provided by Gitlab-CI to auto-fill the informations about the branch and PR’s so that the users don’t have to do it themselves.

What you want to do is to rename your main branch to XXX, so that the next analysis of XXX will be taken as the main branch.
For this you want to go in the “Branch and Pull Requests” page in the administration, and click “Rename Branch” next to the main branch, like this:

You might want to delete the previously create XXX branch, to be able to rename the main branch to XXX.
Once done you just have to trigger a new analysis of XXX and it should be taken as the main branch from now on.

HTH,

Greg

Hi Grégoire,

Thanks a lot for your answer. We did manage to rename the master branch as “XXX” and now new analysis of the XXX branch are indeed considered as being on the main branch.
So everything is (almost) fine now.

However, when the project in Sonar was not created yet, therefore when the branch XXX did not exist yet, sonar-scanner failed with a Java back-trace ending with “Could not find a default branch to fall back on.”

I found a post in the forum talking about this issue, but did not quite understand how to solve it.
Therefore, in order to create the project and the master branch, I had to manually run sonar-scanner on my local machine (ie. not on a Gitlab CI slave) with sonar.branch.name not filled.

After this, and renaming master to XXX, sonar-scanner run from Gitlab CI worked fine as explained before.

May I suggest you add a note to https://docs.sonarqube.org/latest/branches/overview/, in the paragraph ““master” or “head” and is analysed when no specific branch parameters are provided” to bring attention to the fact that this branch name might be automatically filled in some circumstances ?

We would also like to be able to fully reproduce on our local machine what happens in the Gitlab CI slave, for testing purposes.

May I suggest you add a few details to https://docs.sonarqube.org/latest/analysis/gitlab-cicd/ or https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/ about what which environment variables are used and how ?

Thanks again.

Stéphane

1 Like

Hi Stéphane,

Thanks for the suggestions. We will update the documentation as you suggest.
I have opened the following ticket to track this change:
https://jira.sonarsource.com/browse/SCCOMM-34

1 Like

I think this can be solved by changing the main branch name just after importing the project, before the first analysis is done. In this case it should directly go into the main branch and not show the error message. And I think it should also be possible to rename the main branch after seeing this error message without having to run the scanner locally.