SonarQube,Cake和TeamCity集成问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SonarQube,Cake和TeamCity集成问题相关的知识,希望对你有一定的参考价值。

我正在使用TeamCity和Cake建立一个新的构建服务器来构建一个c#类库和测试项目。

我正在尝试将SonarQube添加到Cake构建脚本中。已经存在一个Cake for this插件(https://github.com/AgileArchitect/Cake.Sonar),我在TeamCity BuildAgent的工作文件夹中手动运行cake build脚本时工作得很好,但是当它作为TeamCity构建步骤运行时,MSBuild和SonarQube之间的集成似乎打破了。

通过TC运行时,我在构建日志中遇到的错误是:

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 12.0 upwards are supported
  3. The begin, build or end steps have not all been launched from the same folder

这些都不适用于我。 cake脚本在两种情况下都运行begin,build和end步骤。我已经注销了当前目录,并且正如预期的那样,两种情况都与MSBuild版本完全相同。

在日志中,SonarBegin任务以相同的方式记录命令行和TC。以下是构建步骤中两个日志的唯一显着差异:

在我看来,就像集成应该由MSBuild从Sonar中获取一些设置一样,比如设置RunCodeAnalysis目标并在csc.exe调用中添加/ ruleset等参数,从TC运行时就不会发生这种情况。

任何人都有任何建议如何解决正在发生的事情?

(工作 - 从TeamCity代理工作目录中的powershell命令行运行)

ValidateSolutionConfiguration:
  Building solution configuration "Release|Any CPU".
The target "RunCodeAnalysis" listed in a BeforeTargets attribute at "C:TeamCityuildAgentworkfd1026da6d1bbdb9.sonarqubein	argetsSonarQube.Integration.targets (340,11)" does not exist in the project, and will be ignored.
The target "RunCodeAnalysis" listed in an AfterTargets attribute at "C:TeamCityuildAgentworkfd1026da6d1bbdb9.sonarqubein	argetsSonarQube.Integration.targets (391,11)" does not exist in the project, and will be ignored.
Project "C:TeamCityuildAgentworkfd1026da6d1bbdb9ContractManagement.Common.sln" (1) is building "C:TeamCityuildAgentworkfd1026da6d1bbdb9srcContractManagement.CommonContractManagement.Common.csproj" (2) on node 1 (default targets).
GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
CreateProjectSpecificDirs:
  Directory "C:TeamCityuildAgentworkfd1026da6d1bbdb9.sonarqubeout\ContractManagement.Common_AnyCPU_Release_5799" doesn't exist. Skipping.
  Creating directory "C:TeamCityuildAgentworkfd1026da6d1bbdb9.sonarqubeout\ContractManagement.Common_AnyCPU_Release_5799".
  Directory "C:TeamCityuildAgentworkfd1026da6d1bbdb9.sonarqubeconf\ContractManagement.Common_AnyCPU_Release_5799" doesn't exist. Skipping.
  Creating directory "C:TeamCityuildAgentworkfd1026da6d1bbdb9.sonarqubeconf\ContractManagement.Common_AnyCPU_Release_5799".
CoreCompile:
  C:Program Files (x86)MSBuild14.0inamd64csc.exe /noconfig /nowarn:1701,1702 /nostdlib+ /errorreport:prompt /warn:4 /define:TRACE /highentropyva+ /reference:C:TeamCityuildAgentworkfd1026da6d1bbdb9packagesDapper.1.50.2lib
et451Dapper.dll /reference:C:TeamCityuildAgentworkfd1026da6d1bbdb9packagesMicrosoft.ApplicationInsights.2.2.0lib
et45Microsoft.ApplicationInsights.dll /reference:"C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5.2Microsoft.CSharp.dll" /reference:C:TeamCityuildAgentworkfd1026da6d1bbdb9packagesmorelinq.2.1.0lib
et35MoreLinq.dll /reference:"C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5.2mscorlib.dll" /reference:C:TeamCityuildAgentworkfd1026da6d1bbdb9packagesNewtonsoft.Json.9.0.1lib
et45Newtonsoft.Json.dll /reference:"C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5.2System.Configuration.dll" /reference:"C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5.2System.Core.dll" /reference:"C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5.2System.Data.dll" /reference:"C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5.2System.dll" /reference:"C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5.2System.Xml.dll" /debug:pdbonly /filealign:512 /optimize+ /out:objReleaseUdGroup.ContractManagement.Common.dll /ruleset:C:TeamCityuildAgentworkfd1026da6d1bbdb9.sonarqubeconfSonarQubeRoslyn-cs.ruleset /errorlog:C:TeamCityuildAgentworkfd1026da6d1bbdb9srcContractManagement.CommoninReleaseUdGroup.ContractManagement.Common.dll.RoslynCA.json /subsystemversion:6.00 /target:library /warnaserror- /utf8output /analyzer:C:Usersmichael.tongAppDataLocalTemp.sonarqube.staticcsharp_1.22.0.1631SonarAnalyzer-1.22.0.1631.zipGoogle.Protobuf.dll /analyzer:C:Usersmichael.tongAppDataLocalTemp.sonarqube.staticcsharp_1.22.0.1631SonarAnalyzer-1.22.0.1631.zipSonarAnalyzer.CSharp.dll /analyzer:C:Usersmichael.tongAppDataLocalTemp.sonarqube.staticcsharp_1.22.0.1631SonarAnalyzer-1.22.0.1631.zipSonarAnalyzer.dll /additionalfile:C:TeamCityuildAgentworkfd1026da6d1bbdb9.sonarqubeconfcsSonarLint.xml /additionalfile:C:TeamCityuildAgentworkfd1026da6d1bbdb9.sonarqubeconf\ContractManagement.Common_AnyCPU_Release_5799ProjectOutFolderPath.txt AuthenticationAuthDetails.cs AuthenticationAuthenticator.cs AuthenticationAuthResult.cs AuthenticationIAuthenticator.cs DtoBaseRuleSet.cs DtoCompanyCheckResult.cs DtoCompanyTypeEnum.cs DtoContract.cs DtoContractHeader.cs DtoContractSubmission.cs DtoCreditCheckData.cs DtoCreditCheckDecisionEnum.cs DtoCreditCheckResult.cs DtoCreditDecision.cs DtoDbDataValue.cs DtoDatabaseLocation.cs DtoDbMetaData.cs DtoDbMetaDataDictionary.cs RepositoriesBaseContractRepository.cs ManagersContractManager.cs DtoContractStatus.cs DtoDbDataValueDictionary.cs DtoFieldValueDictionary.cs ManagersContractStatusManager.cs ManagersIContractStatusManager.cs ManagersICreditVetRuleSet.cs UtilAppInsights.cs UtilConstants.cs UtilContractSubmissionGenerator.cs RepositoriesIContractRepository.cs DtoIFieldWithDependencySupport.cs DtoISiteFieldValidator.cs DtoPopulatedField.cs DtoFieldMetaData.cs DtoStatusResponse.cs DtoValidatedSiteField.cs DtoValidationFailureType.cs DtoValidationRequest.cs DtoValidationResponse.cs PropertiesAssemblyInfo.cs UtilIContractSubmissionGenerator.cs RepositoriesIEntityDataRepository.cs RepositoriesIFieldMetaDataRepository.cs RepositoriesQuickQuoteRepository.cs RepositoriesRegistrationRepository.cs RepositoriesContractDataRepository.cs RepositoriesFieldMetaDataRepository.cs DtoEntityType.cs UtilAppConfigReader.cs UtilEnumHelper.cs UtilExtensions.cs RepositoriesIEntityRepository.cs UtilFieldDependencyHelper.cs UtilIUDLogger.cs UtilSiteDetailsConfigDecorator.cs UtilIConfigReader.cs UtilTypeHelper.cs "C:Usersmichael.tongAppDataLocalTemp.NETFramework,Version=v4.5.2.AssemblyAttributes.cs"
... Warnings here

(不工作 - 由TeamCity运行)

ValidateSolutionConfiguration:
  Building solution configuration "Release|Any CPU".
Project "C:TeamCityuildAgentworkfd1026da6d1bbdb9ContractManagement.Common.sln" (1) is building "C:TeamCityuildAgentworkfd1026da6d1bbdb9srcContractManagement.CommonContractManagement.Common.csproj" (2) on node 1 (default targets).
CoreCompile:
  C:Program Files (x86)MSBuild14.0inamd64csc.exe /noconfig /nowarn:1701,1702 /nostdlib+ /errorreport:prompt /warn:4 /define:TRACE /highentropyva+ /reference:C:TeamCityuildAgentworkfd1026da6d1bbdb9packagesDapper.1.50.2lib
et451Dapper.dll /reference:C:TeamCityuildAgentworkfd1026da6d1bbdb9packagesMicrosoft.ApplicationInsights.2.2.0lib
et45Microsoft.ApplicationInsights.dll /reference:"C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5.2Microsoft.CSharp.dll" /reference:C:TeamCityuildAgentworkfd1026da6d1bbdb9packagesmorelinq.2.1.0lib
et35MoreLinq.dll /reference:"C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5.2mscorlib.dll" /reference:C:TeamCityuildAgentworkfd1026da6d1bbdb9packagesNewtonsoft.Json.9.0.1lib
et45Newtonsoft.Json.dll /reference:"C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5.2System.Configuration.dll" /reference:"C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5.2System.Core.dll" /reference:"C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5.2System.Data.dll" /reference:"C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5.2System.dll" /reference:"C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5.2System.Xml.dll" /debug:pdbonly /filealign:512 /optimize+ /out:objReleaseUdGroup.ContractManagement.Common.dll /subsystemversion:6.00 /target:library /utf8output AuthenticationAuthDetails.cs AuthenticationAuthenticator.cs AuthenticationAuthResult.cs AuthenticationIAuthenticator.cs DtoBaseRuleSet.cs DtoCompanyCheckResult.cs DtoCompanyTypeEnum.cs DtoContract.cs DtoContractHeader.cs DtoContractSubmission.cs DtoCreditCheckData.cs DtoCreditCheckDecisionEnum.cs DtoCreditCheckResult.cs DtoCreditDecision.cs DtoDbDataValue.cs DtoDatabaseLocation.cs DtoDbMetaData.cs DtoDbMetaDataDictionary.cs RepositoriesBaseContractRepository.cs ManagersContractManager.cs DtoContractStatus.cs DtoDbDataValueDictionary.cs DtoFieldValueDictionary.cs ManagersContractStatusManager.cs ManagersIContractStatusManager.cs ManagersICreditVetRuleSet.cs UtilAppInsights.cs UtilConstants.cs UtilContractSubmissionGenerator.cs RepositoriesIContractRepository.cs DtoIFieldWithDependencySupport.cs DtoISiteFieldValidator.cs DtoPopulatedField.cs DtoFieldMetaData.cs DtoStatusResponse.cs DtoValidatedSiteField.cs DtoValidationFailureType.cs DtoValidationRequest.cs DtoValidationResponse.cs PropertiesAssemblyInfo.cs UtilIContractSubmissionGenerator.cs RepositoriesIEntityDataRepository.cs RepositoriesIFieldMetaDataRepository.cs RepositoriesQuickQuoteRepository.cs RepositoriesRegistrationRepository.cs RepositoriesContractDataRepository.cs RepositoriesFieldMetaDataRepository.cs DtoEntityType.cs UtilAppConfigReader.cs UtilEnumHelper.cs UtilExtensions.cs RepositoriesIEntityRepository.cs UtilFieldDependencyHelper.cs UtilIUDLogger.cs UtilSiteDetailsConfigDecorator.cs UtilIConfigReader.cs UtilTypeHelper.cs "C:TeamCityuildAgent	empuildTmp.NETFramework,Version=v4.5.2.AssemblyAttributes.cs"

答案

经过几天的侦探工作,我已经找到了问题所在。根本原因是当MSBuild运行时,需要与MSBuild集成的声纳目标未被拾取。

原因是TeamCity构建代理作为本地系统帐户运行,这意味着它用于存储目标文件的MSBuild目录是:

C:Windowssystem32configsystemprofileAppDataLocalMicrosoftMSBuild

但是,SonarQube.Scanner.MSBuild.exe是针对32位编译的,因此在64位操作系统仿真期间,此路径会发生变化:

C:WindowssysWOW4configsystemprofileAppDataLocalMicrosoftMSBuild

但是,当MSBuild步骤运行时,即64位,所以它在system32文件夹中查找!

您可以尝试更改构建代理程序在其下运行的用户,但这不起作用。这是我相信TeamCity Build代理如何工作的一个怪癖。基本上,环境设置中的appData文件夹无法正确调整。

我让它工作的方式是下载sonarqube扫描仪的源代码并编译64位版本。

其他可能有用的选项,我现在要研究一下:

  1. 使用32位版本的msbuild
  2. 手动更改指向msbuild用于目标的文件夹的env变量,以便在c: windows system32之外的文件夹

以上是关于SonarQube,Cake和TeamCity集成问题的主要内容,如果未能解决你的问题,请参考以下文章

在 TeamCity 上运行 SonarQube

用于 .Net Framework 和 .Net Core 的 TeamCity 和 SonarQube 依赖项检查器

SonarQube + JaCoCo + TeamCity 模块名称错误

Sonarqube css扫描问题

MSBuild.SonarQube.Runner.Tool不分析.cs文件

持续集成术语和定义(TeamCity)