Search code examples
jenkinsmsbuildsonarqubesonarqube-msbuild-runner

SonarQube MSBuild integration failed with Jenkins, works fine on command line


I am analyzing my .NET project with sonarqube and I want to automate it with my jenkins build server.

I use MSBuild 14.0.23107.0, sonarqube 6.7.1.35068, sonar-scanner-msbuild-4.0.2.892, sonar-csharp-plugin-6.7.1.4347, Jenkins 2.89.3

I tried the jenkins "SonarQube Scanner for MSBuild - Begin Analysis" and "SonarQube Scanner for MSBuild - End Analysis" build steps but they failed with the following error message:

The SonarQube MSBuild integration failed: SonarQube was unable to collect the required information about your projects. Possible causes:

  1. The project has not been built - the project must be built in between the begin and end steps
  2. An unsupported version of MSBuild has been used to build the project. Currently MSBuild 14.0 upwards are supported
  3. The begin, build or end steps have not all been launched from the same folder

I tried a couple of things and then switched to a windows batch file build step with the following content:

SonarQube.Scanner.MSBuild.exe begin /k:"eGate" /v:"%SVN_REVISION%" /d:sonar.login="%SONAR_AUTH_TOKEN%"
MSBuild.exe /property:Configuration=AutoBuild /t:Rebuild
SonarQube.Scanner.MSBuild.exe end /d:sonar.login="%SONAR_AUTH_TOKEN%"

Because this is the most minimal example I could think of. It still fails:

12:41:39 C:\Jenkins\workspace\eGate>SonarQube.Scanner.MSBuild.exe begin /k:"eGate" /v:"8503" /d:sonar.login="******" 
12:41:39 SonarQube Scanner for MSBuild 4.0.2
12:41:39 Default properties file was found at C:\sonarqube\bin\SonarQube.Analysis.xml
12:41:39 Loading analysis properties from C:\sonarqube\bin\SonarQube.Analysis.xml
12:41:39 Pre-processing started.
12:41:39 Preparing working directories...
12:41:39 12:41:39.232  Updating build integration targets...
12:41:39 12:41:39.243  Fetching analysis configuration settings...
12:41:39 12:41:39.403  Provisioning analyzer assemblies for cs...
12:41:39 12:41:39.404  Installing required Roslyn analyzers...
12:41:39 12:41:39.44  Pre-processing succeeded.
12:41:39 
12:41:39 C:\Jenkins\workspace\eGate>MSBuild.exe /property:Configuration=AutoBuild /t:Rebuild 
12:41:39 Microsoft (R)-Buildmodul, Version 14.0.23107.0

SNIP

12:42:28 
12:42:28     4 Warnung(en)
12:42:28     0 Fehler
12:42:28 
12:42:28 Verstrichene Zeit 00:00:48.97
12:42:28 
12:42:28 C:\Jenkins\workspace\eGate>SonarQube.Scanner.MSBuild.exe end /d:sonar.login="******" 
12:42:28 SonarQube Scanner for MSBuild 4.0.2
12:42:28 Default properties file was found at C:\sonarqube\bin\SonarQube.Analysis.xml
12:42:28 Loading analysis properties from C:\sonarqube\bin\SonarQube.Analysis.xml
12:42:28 Post-processing started.
12:42:28 The SonarQube MSBuild integration failed: SonarQube was unable to collect the required information about your projects.
12:42:28 Possible causes:
12:42:28   1. The project has not been built - the project must be built in between the begin and end steps
12:42:28   2. An unsupported version of MSBuild has been used to build the project. Currently MSBuild 14.0 upwards are supported
12:42:28   3. The begin, build or end steps have not all been launched from the same folder
12:42:28 Generation of the sonar-properties file failed. Unable to complete SonarQube analysis.
12:42:28 12:42:28.73  Creating a summary markdown file...
12:42:28 12:42:28.732  Post-processing failed. Exit code: 1
12:42:28 
12:42:28 C:\Jenkins\workspace\eGate>exit 1 
12:42:28 Build step 'Windows Batch-Datei ausführen' marked build as failure
12:42:29 Finished: FAILURE

The interesting part is, when I open a command line, switch to the jenkins workspace and then execute the commands manually everything works as expected:

The SonarQube Scanner has finished
12:28:23.407  Creating a summary markdown file...
12:28:23.411  Analysis results: http://localhost:9000/dashboard/index/eGate
12:28:23.411  Post-processing succeeded.

Any hints how to debug this? I can provide additional logs and information if you need them.


Solution

  • I could kind of resolve this issue. I am not 100% happy but I can make this work by calling SonarQube.Scanner.MSBuild.exe and MSBuild.exe with the full absolute path:

    "C:\sonarqube\bin\SonarQube.Scanner.MSBuild.exe" begin /k:"eGate" /v:"%SVN_REVISION /d:sonar.login="%SONAR_AUTH_TOKEN%"
    "C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" /property:Configuration=AutoBuild /t:Rebuild
    "C:\sonarqube\bin\SonarQube.Scanner.MSBuild.exe" end /d:sonar.login="%SONAR_AUTH_TOKEN%"
    

    But I can't figure out why this works but calling them from the %PATH% env doesn't. It is really weird...