3

I am trying to setup SonarQube for a C# project, using Teamcity. The problem is that no C# files gets analyzed.

Can you please double check my configuration and let me know if I might have missed anything ? I am all out of ideas on why it does not analyse any C# files.

Teamcity configuration

Teamcity configuration (Continued)

Project Analysis

SonarQube plugins, SonarC# is in the list

If you need any additional info please let me know and I'll edit the question.

Marcel De Villiers
  • 1,682
  • 4
  • 15
  • 17

2 Answers2

1

Inside additional parameters, try adding this:

-Dsonar.lanauge=c#

If it doesn't work, try using command line runner instead of a TeamCity plugin:

Step 1:

Download and install SonarQube MSBuild runner from here.

Step 2:

Create a command line runner in your project build steps in TeamCity with commands below, don't forget to re-order this item to make it run before MSBuild.

SonarQube.Scanner.MSBuild.exe begin /k:"%sonar.project%" /d:"sonar.host.url=%sonar.host.url%" /d:"sonar.login=%sonar.login%" /d:"sonar.organization=%sonar.organization%" /v:"%build.number%"

This makes SonarQubeRunner hooks into MSBuild.

Step 3:

Create a command line build step again with the command below:

SonarQube.Scanner.MSBuild.exe end /d:"sonar.login=%sonar.login%"

This will send the analysis to SonarCube.

Update 1:

As you know, I have used a couple of params such as sonar.login and etc, don't forget to add them inside Parameters in TeamCity.

sonar.login => your login

sonar.organization => the organization

sonar.project => your project in SonarQube

sonar.host.url => host url of SonarCube eg.: https://sonarcloud.io

Ali Bahrami
  • 5,935
  • 3
  • 34
  • 53
  • 1
    Hi @ali-bahrami, It would appear that running the sonar scanner from command line is the way to go. -Dsonar.lanauge=c# did not work for me. Thank you for your time and answer, it really helped me out a lot. – Marcel De Villiers Jun 12 '19 at 05:00
  • 1
    @Marcel-Is-Hier Hi, Your welcome. I personally prefer to run the runners instead of the plugins. Even I had problems with `xunit` plugin TeamCity but the runner worked fine. I'm glad that your problem solved. – Ali Bahrami Jun 12 '19 at 05:05
0

To analyze C# code from Jenkins, follow the instructions in the Analyzing with SonarQube Scanner for MSBuild of the Analyzing with SonarQube Scanner for Jenkins docs page.

You need to use the SonarQube Scanner for MSBuild to analyse C# because the analysis is done as part of the MSBuild step, and only the SonarQube Scanner for MSBuild correctly hooks in to the MSBuild step - the others versions of the SonarQube scanner don't.

A couple of other points:

  • the sonar.organization property is only relevant if you are using SonarCloud; it's not used with SonarQube.
  • the sonar.language property has been deprecated since SonarQube 4.5 - don't supply it.
  • don't set the sonar.sources property when you are using the SonarQube Scanner for MSBuild. The scanner will automatically set the property based on the files that are in the MSBuild projects that are build.

One downside of the SonarQube Scanner for MSBuild setting the sonar.sources property automatically is that all of the files to be analysed have to be referenced by an MSBuild project i.e. all of the XML, JavaScript, CSS etc files.

If those other files types are not referenced by an MSBuild project but you want to analyse them then you can either add references to the files to an MSBuild project, or run a separate analysis using one of the other SonarQube scanners instead (although if you run a separate analysis then you will need to supply a different sonar.project key so the analysis results don't overwrite each other).

duncanp
  • 1,572
  • 1
  • 10
  • 8