【问题标题】:Use MsBuild scripts in TeamCity to run Sonar Scanner for MsBuild在 TeamCity 中使用 MsBuild 脚本为 MsBuild 运行 Sonar Scanner
【发布时间】:2018-09-02 02:05:56
【问题描述】:

我正在升级 TeamCity 2017.1.4 和 SonarQube 6.7.1,并且正在升级分析方法以使用 Sonar Scanner for MsBuild (4.0.2) 而不是之前使用的 Sonar Runner。

我们使用 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" />

我想使用的命令是,因为这应该使用在 TeamCity 中选择的 MsBuild 版本:

<Exec Command="$(SonarScannerMSBuildPath) begin /d:sonar.verbose=$(SonarScannerVerboseLogging) /k:&quot;$(SonarProjectKey)&quot; /n:&quot;$(SonarProjectName)&quot; /v:&quot;$(Build_Number)&quot;" />  
<MSBuild Targets="Rebuild" Properties="Configuration=Debug" Projects="@(Solution)" />
<Exec Command="$(SonarScannerMSBuildPath) end" />

在 TeamCity 中,我将 MsBuild 版本设置为:

我注意到,当我运行 Exec 命令来构建解决方案时,我看到了分析器警告,但是当通过 MsBuild 命令运行构建时,这些警告不会出现。 teamcity 输出表明正在使用正确的 MsBuild 版本:

[11:40:34]  [Step 1/1] Starting: D:\TeamCity\buildAgent\plugins\dotnetPlugin\bin\JetBrains.BuildServer.MsBuildBootstrap.exe /workdir:D:\TeamCity\buildAgent\work\488e6f245ac98af3 "/msbuildPath:C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\bin\MSBuild.exe"

错误信息说明:

[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 文件夹。

【问题讨论】:

    标签: c# sonarqube teamcity sonarqube-scan


    【解决方案1】:

    我可以推荐在 TeamCity 中使用普通的 Commnand 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_Path%

    是来自 TeamCity 的参数,指向 Visual Studio 2017 构建工具。

    我的工作目录设置为

    %teamcity.build.checkoutDir%

    整个配置的打印屏幕:

    【讨论】:

    • 这与我提出的解决方案相似。使用 MsBuild 脚本,我必须创建一个设置为 %MSBuildTools15.0_x86_Path% 的环境变量(我称之为 MSBuildTools15_0_x86_Path)。然后在我的构建脚本中,我通过 引用了它
    【解决方案2】:

    MSBuild 的扫描程序要求 beginbuildend 步骤从同一目录运行。我的猜测是 ExecMSBuild 任务使用不同的工作目录。

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

    【讨论】:

    • 我认为这可能是原因,但我仍在苦苦挣扎。我已将 WorkingDirectory 添加到开始和结束步骤,并使用了 .sln 文件的目录,但仍然出现相同的错误。我在想我需要为 MsBuild 步骤设置文件夹,因为 .sln 文件是 TeamCity workdir 深处的几个文件夹,但 MsBuild 任务没有 WorkingFolder 参数。
    • 您可以尝试运行带有诊断详细信息的 MSBuild 并检查保留属性 $(MSBuildStartupDirectory) 的值 - 这可能是您需要设置的“工作目录”。
    • 我只能在从 Exec 任务运行 MsBuild 时设置详细程度。该变量可用,我尝试将 Begin 和 End 任务设置为在该文件夹中启动,遗憾的是我仍然遇到相同的错误。
    猜你喜欢
    • 1970-01-01
    • 2016-06-07
    • 2011-05-06
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 2018-05-27
    相关资源
    最近更新 更多