SonarQube doesn’t recognize branch name in version 10.6 when using Azure Pipelines

Hi,

I’m facing a similar issue to this existing but old topic:
SonarQube doesn’t recognize branch name in version 10.1 when using Azure Pipelines - SonarQube - Sonar Community (sonarsource.com)

SonarQube Version 10.6
Azure DevOps Tasks

  • SonarQubePrepare@6
  • SonarQubeAnalyze@6
  • SonarQubePublish@6

When running a pipeline with this setup, sometimes the analysis is affiliated with the “main” branch in SonarQube, even though the source branch is definitely different, as we never use the “main” branch in our repository.

Debugging, by echoing the environment variables after the SonarQubePrepare tasks, gives an indication. The SONARQUBE_SCANNER_PARAMS variable whose value is defined by the Prepare@6 task is set differently.

I ran the pipeline twice, for the same commit ID, just with different branch names:

Branch name “develop”:
{"sonar.host.url":"***","sonar.token":***,"project.settings":"/__w/1/s/sonar-project.properties","sonar.scanner.metadataFilePath":"/.../report-task.txt","sonar.cfamily.compile-commands":"build/compile_commands.json"}

Branch name “pipeline”
{"sonar.host.url":"***","sonar.token":***,"project.settings":"/__w/1/s/sonar-project.properties","sonar.branch.name":"pipeline","sonar.scanner.metadataFilePath":"/.../report-task.txt","sonar.cfamily.compile-commands":"build/compile_commands.json"}

For some reason the branch name is not included when it’s named “develop”, which seems to cause SonarQube to associate this run with the default (“main”) branch.

Any idea how to resolve this issue?

Kind regards
Fabi

Hey there.

Which branch is configured as your default branch in Azure DevOps?

Hey Colin,

Thanks for your quick reply!

After looking at the code you linked in the other topic more in detail, I also figured it may be related to the default branch settings in Azure Devops vs. SonarQube.

To my own surprise for this particular repo it was set to “develop” in Azure DevOps. I’ve adjusted the default branch setting in SonarQube an I’m right now rerunning the pipeline. I’m expecting it now to be reported to the “develop” branch in SonarQube, which would resolve this issue rather quickly.

Still, I don’t understand why that piece of code is relying on the default branch setting in Azure DevOps to decide wether or not to set the sonar.branch property. Basically, it only excludes it for the default branch. Any other branch is included.

I may not get all the details, but here’s my take: If it’s of importance to you to have both default branch settings aligned, why not always set this property but issue a warning or even error when executing the SonarQubePrepare task? I feel this would be better than reporting to the wrong branch silently.

Kind regards
Fabi

Hey @Fabi

Thanks for the feedback. There’s a looooong history here. I’ll try and offer my recollection here.

  • sonar.branch.name enables branch analysis which is available in the Developer Edition of SonarQube.
  • For the Community Edition of SonarQube, we want the Extension for Azure DevOps to be able to analyze the default branch of a project no problem, but not other branches.
  • To do that, we make sure that sonar.branch.name isn’t set when the default branch is being analyzed.

There are other things we could have considered doing (like checking the edition of the SonarQube instance before running the analysis), but this is the decision we took circa 2019, and it works pretty well most of the time when the default branch is set correctly.

This is an interesting thought I’ll pass along.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.