Azure DevOps branch name issue with Default Branch

Must-share information:

  • SonarQube community 9.8 (on Docker)
  • I’m trying to run C# analysis within a Release Pipeline on my Azure Devops project

I’m using Git Flow paradigm to manage our git branches but, in my pipelines, I only use “develop” branch.

I setup pipeline tasks as suggested in SonarQube instruction (shown when I create the project).
When I exec the pipeline, it gaves me the following error:

ERROR: Validation of project failed:
  o To use the property "sonar.branch.name" and analyze branches, Developer Edition or above is require.

I search for this error and find in the forum my first error, I haven’t setup correctly my default branch.

I correct the default branch and tried to run again but the pipeline gaves me the same error.

In my pipeline I’ve the following tasks:

  • dotnet restore
  • Prepare analysis on SonarQube (with connection specified, analysis integrate with MS Build", project key and name and project version “1.0” (default).
  • dotnet build
  • dotnet test
  • Run Code Analysis (no arguments)
  • Publish Quality Gate Results (default arguments)
  • …other tasks

In my first run I had errors on “Prepare analysis”.

I’ve added the additional property “sonar.branch.name” and re-run pipeline, it gaves me error on Run Code Analysis.

After that I’ve found a forum threads that explain the “default branch” mechanism and setup my default branch with the pipeline 's branch and re-run again the pipeline.

In this case, Prepare analysis on SonarQube runs without error but have the same error on “sonar.branch.name” property in the “Run Code Analysis” (that has no arguments to setup)

Any idea?
Thank you in advance

Hi,

Welcome to the community!

I think this is what you’re looking for:

 
HTH,
Ann

Nope, I’ve tried a couple of minutes ago.

I’ve added a Powershell task with the code you suggested and execute the pipeline but I’ve got the same error.

I paste the whole log of “Code Analysis” task

2023-01-23T19:52:33.6410962Z ##[section]Starting: Run Code Analysis
2023-01-23T19:52:33.6417472Z ==============================================================================
2023-01-23T19:52:33.6417728Z Task         : Run Code Analysis
2023-01-23T19:52:33.6417865Z Description  : Run scanner and upload the results to the SonarQube server.
2023-01-23T19:52:33.6418090Z Version      : 5.9.0
2023-01-23T19:52:33.6418218Z Author       : sonarsource
2023-01-23T19:52:33.6418358Z Help         : Version: 5.9.0. This task is not needed for Maven and Gradle projects since the scanner should be run as part of the build.

[More Information](http://redirect.sonarsource.com/doc/install-configure-scanner-tfs-ts.html)
2023-01-23T19:52:33.6418829Z ==============================================================================
2023-01-23T19:52:33.7997978Z [command]/usr/bin/dotnet /home/vsts/work/_tasks/SonarQubePrepare_15b84ca1-b62f-4a2a-a403-89b77a063157/5.9.0/dotnet-sonar-scanner-msbuild/SonarScanner.MSBuild.dll end
2023-01-23T19:52:33.8937070Z SonarScanner for MSBuild 5.9.2
2023-01-23T19:52:33.8939275Z Using the .NET Core version of the Scanner for MSBuild
2023-01-23T19:52:33.9680370Z Post-processing started.
2023-01-23T19:52:34.1372293Z Calling the SonarScanner CLI...
2023-01-23T19:52:34.3516679Z INFO: Scanner configuration file: /home/vsts/work/_tasks/SonarQubePrepare_15b84ca1-b62f-4a2a-a403-89b77a063157/5.9.0/dotnet-sonar-scanner-msbuild/sonar-scanner-4.7.0.2747/conf/sonar-scanner.properties
2023-01-23T19:52:34.3518688Z INFO: Project root configuration file: /home/vsts/work/r1/a/.sonarqube/out/sonar-project.properties
2023-01-23T19:52:34.3943859Z INFO: SonarScanner 4.7.0.2747
2023-01-23T19:52:34.3945837Z INFO: Java 11.0.17 Eclipse Adoptium (64-bit)
2023-01-23T19:52:34.3947000Z INFO: Linux 5.15.0-1031-azure amd64
2023-01-23T19:52:34.7449821Z INFO: User cache: /home/vsts/.sonar/cache
2023-01-23T19:52:49.3446335Z INFO: Scanner configuration file: /home/vsts/work/_tasks/SonarQubePrepare_15b84ca1-b62f-4a2a-a403-89b77a063157/5.9.0/dotnet-sonar-scanner-msbuild/sonar-scanner-4.7.0.2747/conf/sonar-scanner.properties
2023-01-23T19:52:49.3450330Z INFO: Project root configuration file: /home/vsts/work/r1/a/.sonarqube/out/sonar-project.properties
2023-01-23T19:52:49.3555502Z INFO: Analyzing on SonarQube server 9.8.0.63668
2023-01-23T19:52:49.3568298Z INFO: Default locale: "en", source code encoding: "UTF-8" (analysis is platform dependent)
2023-01-23T19:52:49.9219708Z INFO: Load global settings
2023-01-23T19:52:50.2261795Z INFO: Load global settings (done) | time=307ms
2023-01-23T19:52:50.2288224Z INFO: Server id: 243B8A4D-AYW5x30Yfdpkld8dUONV
2023-01-23T19:52:50.2363950Z INFO: User cache: /home/vsts/.sonar/cache
2023-01-23T19:52:50.2392502Z INFO: Load/download plugins
2023-01-23T19:52:50.2393197Z INFO: Load plugins index
2023-01-23T19:52:50.4315505Z INFO: Load plugins index (done) | time=192ms
2023-01-23T19:53:48.7231793Z INFO: Load/download plugins (done) | time=58474ms
2023-01-23T19:53:49.2345346Z INFO: Process project properties
2023-01-23T19:53:49.2659181Z INFO: Process project properties (done) | time=35ms
2023-01-23T19:53:49.2671613Z INFO: Execute project builders
2023-01-23T19:53:49.3270442Z INFO: Execute project builders (done) | time=60ms
2023-01-23T19:53:49.3759568Z INFO: ------------------------------------------------------------------------
2023-01-23T19:53:49.3762605Z INFO: EXECUTION FAILURE
2023-01-23T19:53:49.3763924Z INFO: ------------------------------------------------------------------------
2023-01-23T19:53:49.3785674Z INFO: Total time: 1:15.037s
2023-01-23T19:53:49.4039495Z INFO: Final Memory: 7M/30M
2023-01-23T19:53:49.4040889Z INFO: ------------------------------------------------------------------------
2023-01-23T19:53:49.4042234Z ##[error]ERROR: Error during SonarScanner execution
2023-01-23T19:53:49.4043385Z ERROR: Error during SonarScanner execution
2023-01-23T19:53:49.4044957Z ##[error]ERROR: Validation of project failed:
  o To use the property "sonar.branch.name" and analyze branches, Developer Edition or above is required. See https://redirect.sonarsource.com/doc/branches.html for more information.
ERROR:
2023-01-23T19:53:49.4046713Z ERROR: Validation of project failed:
2023-01-23T19:53:49.4047946Z   o To use the property "sonar.branch.name" and analyze branches, Developer Edition or above is required. See https://redirect.sonarsource.com/doc/branches.html for more information.
2023-01-23T19:53:49.4049275Z ERROR: 
2023-01-23T19:53:49.4182951Z ##[error]The SonarScanner did not complete successfully
2023-01-23T19:53:49.4184206Z The SonarScanner did not complete successfully
2023-01-23T19:53:49.4185811Z ##[error]19:53:49.418  Post-processing failed. Exit code: 1
2023-01-23T19:53:49.4186766Z 19:53:49.418  Post-processing failed. Exit code: 1
2023-01-23T19:53:49.4294750Z ##[error]The process '/usr/bin/dotnet' failed with exit code 1
2023-01-23T19:53:49.4330075Z ##[section]Finishing: Run Code Analysis

The powershell script correctly remove the entry “sonar.branch.name”, after that, I run dotnet build and dotnet test tasks (that runs successfully) and then I run Code Analysis that fails.

I run SonarQube community 9.8 as a docker container (with the whole docker-compose file).
The entry “sonar.branch.name” is set with the correct branch name (before I remove it with the Powershell task").

Any idea?
Thank you in advance!

Hi,

I’m going to flag this for more expert attention.

 
Ann

Hello Ann and thank you for your help!

Have a nice day!

Ya, that solution didn’t end up working for me, either :slightly_smiling_face:

I have my own thread open about this issue, at Unset sonar.branch.name in Azure DevOps pipeline

If you find a fix, please let me know! :slightly_smiling_face:

Hello Eli and thanks for your reply (and link to your thread).

In this moment I’m stuck with the same issue. I’ve tried to remove property sonar.branch.name from environment variable as suggested with no luck.

If I’m lucky finding any solutions, I’ll share it immediatly

1 Like

Hi @ronkpunk

Where did you set the default branch ? On Azure directly ?

Does it show up like this ?

Screenshot 2023-02-01 at 13.39.21

Mickaël

Yep, in Azure Devops repository branches I’ve the following
branches

In my Release Pipeline I’ve this
Pipeline Artifact

In SonarQube (Community) I’ve added the project, adding Devops repository and selecting “Pipeline”

Is it right?

Yes, sounds good. And what is the value of the “Build.SourceBranch” variable when you execute the pipeline ?
In our behavior, it should be the same as the default branch, otherwise you’ll get this error message.

Where I can find it?

You can enable the debug mode for the task, it should be shown in the logs, like this :

Screenshot 2023-02-01 at 16.54.02

Ok I found it in Job initialization logs. Its name is a bit different but I think it’s almost the same.
it is set to “develop”
source_branchname

Thanks.

There’s also a log somewhere starting by this : "Default branch of this repository is " this is a debug log so it is shown only using the debug mode mentioned above.

Can you check if this is also “develop” here ?

I cannot find this phrase in debug logs, but I’ve found Build.SourceBranch in Prepare Analyis:
source_branchname

I’m looking for default branch of repo

I’ve found a strange thing.
I added a powershell task that removes “sonar.branch.name” in “SONARQUBE_SCANNER_PARAMS” environment variable.

The task is before “Run code Analysis” and contains the following script

$sonarQubeScannerParams = $($Env:SONARQUBE_SCANNER_PARAMS | ConvertFrom-Json)

$sonarQubeScannerParams.PSObject.Properties.Remove('sonar.branch.name')

$Env:SONARQUBE_SCANNER_PARAMS = $($sonarQubeScannerParams | ConvertTo-Json)

In the task I print the content of the variable before and after the script

2023-02-01T16:05:01.9442541Z  ------- PRE ------- 
2023-02-01T16:05:01.9451118Z {"sonar.host.url":"<myhost>","sonar.login":***,"sonar.projectKey":"<myprojectkey>","sonar.projectName":"<myprojectname>","sonar.projectVersion":"1.0",**"sonar.branch.name":"develop"**,"sonar.scanner.metadataFilePath":"<path>"}
2023-02-01T16:05:01.9452765Z  ------- /PRE -------
 
2023-02-01T16:05:02.1221470Z  ------- POST ------- 
2023-02-01T16:05:02.1225492Z {
2023-02-01T16:05:02.1226241Z   "sonar.host.url": "<myhost>",
2023-02-01T16:05:02.1227381Z   "sonar.login": ***,
2023-02-01T16:05:02.1227731Z   "sonar.projectKey": "<myprojectkey>",
2023-02-01T16:05:02.1228120Z   "sonar.projectName": "<myprojectname>",
2023-02-01T16:05:02.1228671Z   "sonar.projectVersion": "1.0",
2023-02-01T16:05:02.1229279Z   "sonar.scanner.metadataFilePath": "<path>"
2023-02-01T16:05:02.1229677Z }
2023-02-01T16:05:02.1229911Z  ------- /POST ------- 

so the script remove correctly the property “sonar.branch.name”

While “Run Code Analysis” task’s logs have this

2023-02-01T16:05:03.0562729Z ##[debug]SONARQUBE_SCANNER_PARAMS={"sonar.host.url":"<myhost>","sonar.login":***,"sonar.projectKey":"<myprojectkey>","sonar.projectName":"<myprojectname>","sonar.projectVersion":"1.0",**"sonar.branch.name":"develop"**,"sonar.scanner.metadataFilePath":"<path>"}

Maybe I’m updating the wrong variable?
Thank you in advance

Can you send me both Prepare and analyze logs, with debug logs enabled please ? You can redact things on it or I can send you a PM to transmit them. It would help me investigate. Thanks in advance !

I prefer to send logs privately to you via PM

Hello, in my case, I’m using Release pipeline (not Build Pipeline).

The problem is that Sonarqube plugin act through the Build Pipeline; this because in Release Pipeline, is there a variable “Provider-Something” that is set as “Git” and not “TfsGit”.

I tried to create a Build Pipeline with same tasks within the Release Pipeline and it ends ok.

Can it helps you?
Which kind of Pipeline are you using?
Hope this helps
Good luck!

1 Like

Thanks, @ronkpunk :slightly_smiling_face:

After reading through this thread again, I think your issue and mine are a bit different.

Glad you got it working, and thanks for trying to help my issue as well! :slight_smile:

@mickaelcaro and I are discussing my issue in my other thread, so I think we should be good. Thanks again, and best of luck on your future SonarQube endeavors :smile:

1 Like