C# 在TeamCity中使用MsBuild脚本为MsBuild运行Sonar Scanner

C# 在TeamCity中使用MsBuild脚本为MsBuild运行Sonar Scanner,c#,sonarqube,teamcity,sonarqube-scan,C#,Sonarqube,Teamcity,Sonarqube Scan,我正在升级TeamCity 2017.1.4和SonarQube 6.7.1,并正在升级分析方法,以使用MsBuild 4.0.2的声纳扫描仪,而不是以前使用的声纳转轮 我们使用MsBuild脚本,以便对更改进行源代码控制,我正在努力使声纳扫描仪正常工作 问题似乎是当我使用MSBuild任务构建项目时,Sonar抱怨说它尚未构建或未使用较新版本的MSBuild构建。 但是,如果我使用Exec命令指定构建,它将正确处理所有内容 使用Exec运行MsBuild不是我想做的: <Exec Com

我正在升级TeamCity 2017.1.4和SonarQube 6.7.1,并正在升级分析方法,以使用MsBuild 4.0.2的声纳扫描仪,而不是以前使用的声纳转轮

我们使用MsBuild脚本,以便对更改进行源代码控制,我正在努力使声纳扫描仪正常工作

问题似乎是当我使用MSBuild任务构建项目时,Sonar抱怨说它尚未构建或未使用较新版本的MSBuild构建。 但是,如果我使用Exec命令指定构建,它将正确处理所有内容

使用Exec运行MsBuild不是我想做的:

<Exec Command="$(SonarScannerMSBuildPath) begin /d:sonar.verbose=$(SonarScannerVerboseLogging) /k:&quot;$(SonarProjectKey)&quot; /n:&quot;$(SonarProjectName)&quot; /v:&quot;$(Build_Number)&quot;" />  
<Exec Command="&quot;C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\msbuild.exe&quot; @(Solution)  /t:Rebuild" />
<Exec Command="$(SonarScannerMSBuildPath) end" />
错误信息说明:

[12:23:07]  [Exec] The SonarQube MSBuild integration failed: SonarQube was unable to collect the required information about your projects.
[12:23:07]  [Exec] Possible causes:
[12:23:07]  [Exec] 1. The project has not been built - the project must be built in between the begin and end steps
[12:23:07]  [Exec] 2. An unsupported version of MSBuild has been used to build the project. Currently MSBuild 14.0 upwards are supported
[12:23:07]  [Exec] 3. The begin, build or end steps have not all been launched from the same folder

如果浏览到TeamCity生成代理上的文件夹,则缺少.sonar和.sonarqube文件夹。

MSBuild的扫描程序希望从同一目录运行开始、生成和结束步骤。我猜Exec和MSBuild任务使用的是不同的工作目录


您可以尝试设置Exec任务的WorkingDirectory参数,看看这是否有帮助。我会先尝试将其设置为包含解决方案的目录。

我建议使用TeamCity中的普通命令和line runner来运行SonarQube Scanner。您可以在那里毫无问题地设置工作目录。以下是我的自定义脚本示例:

"%sonar.scanner.msbuild%\SonarQube.Scanner.MSBuild.exe" begin^
 /k:"ProjectKey"^
 /n:"ProjectName"^
 /v:"%build.vcs.number%.%build.counter%"
"%MSBuildTools15.0_x64_Path%\MSBuild.exe" SolutionFile.sln /t:Rebuild
"%sonar.scanner.msbuild%\SonarQube.Scanner.MSBuild.exe" end
在哪里

%sonar.scanner.msbuild%

是我的自定义参数-SonarQube.Scanner.MSBuild.exe的完整路径,以及

%MSBuildTools15.0_x64_路径%

是TeamCity中指向Visual Studio 2017构建工具的参数

我的工作目录设置为

%teamcity.build.checkoutDir%

整个配置的打印屏幕:


我想这可能是原因,但我仍在挣扎。我已经在开始和结束步骤中添加了WorkingDirectory,并使用了.sln文件的目录,但仍然出现相同的错误。我认为需要为MsBuild步骤设置文件夹,因为.sln文件在TeamCity workdir中有几个文件夹,但MsBuild任务中没有WorkingFolder参数。您可以尝试使用诊断详细性运行MsBuild,并检查保留属性$MSBuildStartupDirectory的值-这可能是您需要设置的工作目录。我只能在从Exec任务运行MsBuild时设置详细信息。变量是可用的,我已经尝试将开始和结束任务设置为在该文件夹中开始,遗憾的是,我仍然得到相同的错误。这与我提出的解决方案类似。使用MsBuild脚本,我必须创建一个名为MSBuildTools15\u 0\u x86\u Path的环境变量,该变量设置为%MSBuildTools15.0\u x86\u Path%。然后在我的构建脚本中,我通过
[12:23:07]  [Exec] The SonarQube MSBuild integration failed: SonarQube was unable to collect the required information about your projects.
[12:23:07]  [Exec] Possible causes:
[12:23:07]  [Exec] 1. The project has not been built - the project must be built in between the begin and end steps
[12:23:07]  [Exec] 2. An unsupported version of MSBuild has been used to build the project. Currently MSBuild 14.0 upwards are supported
[12:23:07]  [Exec] 3. The begin, build or end steps have not all been launched from the same folder
"%sonar.scanner.msbuild%\SonarQube.Scanner.MSBuild.exe" begin^
 /k:"ProjectKey"^
 /n:"ProjectName"^
 /v:"%build.vcs.number%.%build.counter%"
"%MSBuildTools15.0_x64_Path%\MSBuild.exe" SolutionFile.sln /t:Rebuild
"%sonar.scanner.msbuild%\SonarQube.Scanner.MSBuild.exe" end