SonarQube,Cake和TeamCity集成问题

问题描述:

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

我想将SonarQube添加到Cake构建脚本中。已经有一个Cake的插件(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脚本正在运行两种情况下的开始,构建和结束步骤。我已经注销了当前目录,并且正如预期的那样,MSBuild版本完全相同。

在日志中,SonarBegin任务对于命令行和TC都记录相同。这两个日志中唯一重要的差异是在构建步骤:

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

任何人有任何建议如何解决发生了什么事?

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

 
ValidateSolutionConfiguration: 
    Building solution configuration "Release|Any CPU". 
The target "RunCodeAnalysis" listed in a BeforeTargets attribute at "C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\.sonarqube\bin\targets\SonarQube.Integration.targets (340,11)" does not exist in the project, and will be ignored. 
The target "RunCodeAnalysis" listed in an AfterTargets attribute at "C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\.sonarqube\bin\targets\SonarQube.Integration.targets (391,11)" does not exist in the project, and will be ignored. 
Project "C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\ContractManagement.Common.sln" (1) is building "C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\src\ContractManagement.Common\ContractManagement.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:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\.sonarqube\out\\ContractManagement.Common_AnyCPU_Release_5799" doesn't exist. Skipping. 
    Creating directory "C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\.sonarqube\out\\ContractManagement.Common_AnyCPU_Release_5799". 
    Directory "C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\.sonarqube\conf\\ContractManagement.Common_AnyCPU_Release_5799" doesn't exist. Skipping. 
    Creating directory "C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\.sonarqube\conf\\ContractManagement.Common_AnyCPU_Release_5799". 
CoreCompile: 
    C:\Program Files (x86)\MSBuild\14.0\bin\amd64\csc.exe /noconfig /nowarn:1701,1702 /nostdlib+ /errorreport:prompt /warn:4 /define:TRACE /highentropyva+ /reference:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\packages\Dapper.1.50.2\lib\net451\Dapper.dll /reference:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\packages\Microsoft.ApplicationInsights.2.2.0\lib\net45\Microsoft.ApplicationInsights.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\Microsoft.CSharp.dll" /reference:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\packages\morelinq.2.1.0\lib\net35\MoreLinq.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\mscorlib.dll" /reference:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Configuration.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Core.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Data.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Xml.dll" /debug:pdbonly /filealign:512 /optimize+ /out:obj\Release\UdGroup.ContractManagement.Common.dll /ruleset:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\.sonarqube\conf\SonarQubeRoslyn-cs.ruleset /errorlog:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\src\ContractManagement.Common\bin\Release\UdGroup.ContractManagement.Common.dll.RoslynCA.json /subsystemversion:6.00 /target:library /warnaserror- /utf8output /analyzer:C:\Users\michael.tong\AppData\Local\Temp\.sonarqube\.static\csharp_1.22.0.1631\SonarAnalyzer-1.22.0.1631.zip\Google.Protobuf.dll /analyzer:C:\Users\michael.tong\AppData\Local\Temp\.sonarqube\.static\csharp_1.22.0.1631\SonarAnalyzer-1.22.0.1631.zip\SonarAnalyzer.CSharp.dll /analyzer:C:\Users\michael.tong\AppData\Local\Temp\.sonarqube\.static\csharp_1.22.0.1631\SonarAnalyzer-1.22.0.1631.zip\SonarAnalyzer.dll /additionalfile:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\.sonarqube\conf\cs\SonarLint.xml /additionalfile:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\.sonarqube\conf\\ContractManagement.Common_AnyCPU_Release_5799\ProjectOutFolderPath.txt Authentication\AuthDetails.cs Authentication\Authenticator.cs Authentication\AuthResult.cs Authentication\IAuthenticator.cs Dto\BaseRuleSet.cs Dto\CompanyCheckResult.cs Dto\CompanyTypeEnum.cs Dto\Contract.cs Dto\ContractHeader.cs Dto\ContractSubmission.cs Dto\CreditCheckData.cs Dto\CreditCheckDecisionEnum.cs Dto\CreditCheckResult.cs Dto\CreditDecision.cs Dto\DbDataValue.cs Dto\DatabaseLocation.cs Dto\DbMetaData.cs Dto\DbMetaDataDictionary.cs Repositories\BaseContractRepository.cs Managers\ContractManager.cs Dto\ContractStatus.cs Dto\DbDataValueDictionary.cs Dto\FieldValueDictionary.cs Managers\ContractStatusManager.cs Managers\IContractStatusManager.cs Managers\ICreditVetRuleSet.cs Util\AppInsights.cs Util\Constants.cs Util\ContractSubmissionGenerator.cs Repositories\IContractRepository.cs Dto\IFieldWithDependencySupport.cs Dto\ISiteFieldValidator.cs Dto\PopulatedField.cs Dto\FieldMetaData.cs Dto\StatusResponse.cs Dto\ValidatedSiteField.cs Dto\ValidationFailureType.cs Dto\ValidationRequest.cs Dto\ValidationResponse.cs Properties\AssemblyInfo.cs Util\IContractSubmissionGenerator.cs Repositories\IEntityDataRepository.cs Repositories\IFieldMetaDataRepository.cs Repositories\QuickQuoteRepository.cs Repositories\RegistrationRepository.cs Repositories\ContractDataRepository.cs Repositories\FieldMetaDataRepository.cs Dto\EntityType.cs Util\AppConfigReader.cs Util\EnumHelper.cs Util\Extensions.cs Repositories\IEntityRepository.cs Util\FieldDependencyHelper.cs Util\IUDLogger.cs Util\SiteDetailsConfigDecorator.cs Util\IConfigReader.cs Util\TypeHelper.cs "C:\Users\michael.tong\AppData\Local\Temp\.NETFramework,Version=v4.5.2.AssemblyAttributes.cs" 
... Warnings here 

(不工作 - 通过TeamCity的运行)

 
ValidateSolutionConfiguration: 
    Building solution configuration "Release|Any CPU". 
Project "C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\ContractManagement.Common.sln" (1) is building "C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\src\ContractManagement.Common\ContractManagement.Common.csproj" (2) on node 1 (default targets). 
CoreCompile: 
    C:\Program Files (x86)\MSBuild\14.0\bin\amd64\csc.exe /noconfig /nowarn:1701,1702 /nostdlib+ /errorreport:prompt /warn:4 /define:TRACE /highentropyva+ /reference:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\packages\Dapper.1.50.2\lib\net451\Dapper.dll /reference:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\packages\Microsoft.ApplicationInsights.2.2.0\lib\net45\Microsoft.ApplicationInsights.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\Microsoft.CSharp.dll" /reference:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\packages\morelinq.2.1.0\lib\net35\MoreLinq.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\mscorlib.dll" /reference:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Configuration.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Core.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Data.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Xml.dll" /debug:pdbonly /filealign:512 /optimize+ /out:obj\Release\UdGroup.ContractManagement.Common.dll /subsystemversion:6.00 /target:library /utf8output Authentication\AuthDetails.cs Authentication\Authenticator.cs Authentication\AuthResult.cs Authentication\IAuthenticator.cs Dto\BaseRuleSet.cs Dto\CompanyCheckResult.cs Dto\CompanyTypeEnum.cs Dto\Contract.cs Dto\ContractHeader.cs Dto\ContractSubmission.cs Dto\CreditCheckData.cs Dto\CreditCheckDecisionEnum.cs Dto\CreditCheckResult.cs Dto\CreditDecision.cs Dto\DbDataValue.cs Dto\DatabaseLocation.cs Dto\DbMetaData.cs Dto\DbMetaDataDictionary.cs Repositories\BaseContractRepository.cs Managers\ContractManager.cs Dto\ContractStatus.cs Dto\DbDataValueDictionary.cs Dto\FieldValueDictionary.cs Managers\ContractStatusManager.cs Managers\IContractStatusManager.cs Managers\ICreditVetRuleSet.cs Util\AppInsights.cs Util\Constants.cs Util\ContractSubmissionGenerator.cs Repositories\IContractRepository.cs Dto\IFieldWithDependencySupport.cs Dto\ISiteFieldValidator.cs Dto\PopulatedField.cs Dto\FieldMetaData.cs Dto\StatusResponse.cs Dto\ValidatedSiteField.cs Dto\ValidationFailureType.cs Dto\ValidationRequest.cs Dto\ValidationResponse.cs Properties\AssemblyInfo.cs Util\IContractSubmissionGenerator.cs Repositories\IEntityDataRepository.cs Repositories\IFieldMetaDataRepository.cs Repositories\QuickQuoteRepository.cs Repositories\RegistrationRepository.cs Repositories\ContractDataRepository.cs Repositories\FieldMetaDataRepository.cs Dto\EntityType.cs Util\AppConfigReader.cs Util\EnumHelper.cs Util\Extensions.cs Repositories\IEntityRepository.cs Util\FieldDependencyHelper.cs Util\IUDLogger.cs Util\SiteDetailsConfigDecorator.cs Util\IConfigReader.cs Util\TypeHelper.cs "C:\TeamCity\buildAgent\temp\buildTmp\.NETFramework,Version=v4.5.2.AssemblyAttributes.cs" 

几个侦探工作的天之后,我已经找到了这个问题。根本原因是MSBuild运行时没有拾取与MSBuild集成所需的声纳目标。

这样做的原因是在TeamCity的生成代理是作为本地系统运行账户,这意味着它使用存储目标文件的MSBuild目录是:

C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\MSBuild

然而,SonarQube.Scanner.MSBuild.exe编译为32位,所以这条道路上的64位操作系统的仿真过程中获取到的变化:

C:\Windows\sysWOW4\config\systemprofile\AppData\Local\Microsoft\MSBuild 

然而,当MSBuild的步骤运行,即 64位,所以看起来在system32文件夹!

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

我得到它的工作方式是下载sonarqube扫描器的源代码并编译一个64位版本。

可能工作的其他选项,我要去看看现在:

  1. 使用的MSBuild的32位版本
  2. 手动更改ENV变量指向的目标使用的MSBuild文件夹所以在c:\ windows \ system32以外的文件夹
+0

最后,我去了选项2,并创建了以下TeamCity env变量修补了怪癖,这意味着设置不会在运行BuildAgent时自动设置服务在用户帐户下。 – TheLogicMan

+0

\t env.APPDATA \t \t C:\ Users \用户TeamCityAgent \应用程序数据\漫游 env.LOCALAPPDATA \t C:\ Users \用户TeamCityAgent \应用程序数据\本地 env.USERPROFILE \t \t C:\用户\ TeamCityAgent – TheLogicMan