|

1. 前言
C#語言接入Sonar代碼靜態(tài)掃描相較于Java、Python來說,相對麻煩一些。Sonar檢測C#代碼時需要預(yù)先編譯,而且C#代碼必須用MSbuid進行編譯,如果需要使用SonarQube對C#進行代碼質(zhì)量分析,則需要下載Sonar-Scanner-MSBuild和MSBuild,其中要求MSBuild在V14.0以上。
2. Sonar-Scanner for MSBuild安裝與配置 1、下載SonarQube Scanner for MSBuild,它是C# Framework的Sonar分析插件。
下載地址: https://github.com/SonarSource/sonar-scanner-msbuild/releases/download/4.3.1.1372/sonar-scanner-msbuild-4.3.1.1372-net46.zip
2、下載并解壓之后,設(shè)置SonarQube Scanner for MSBuild的環(huán)境變量。
例如我的解壓路徑是:C:\Users\Administrator\Downloads\sonar-scanner-msbuild-4.3.1.1372-net466,則把該路徑添加到Path下。
SonarQube Scanner for MSBuild解壓目錄如下圖所示: 
3、修改SonarQube.Analysis.xml文件,要修改的地方只是關(guān)于SonarQube服務(wù)器的一些配置,如服務(wù)器URL、USER、PASSWORD等,詳細配置修改如下:
<?xml version="1.0" encoding="utf-8" ?><!-- This file defines properties which would be understood by the SonarQube Scanner for MSBuild, if not overridden (see below) By default the SonarScanner.MSBuild.exe picks-up a file named SonarQube.Analysis.xml in the folder it is located (if it exists). It is possible to use another properties file by using the /s:filePath.xml flag The overriding strategy of property values is the following: - A project-specific property defined in the MSBuild *.*proj file (corresponding to a SonarQube module) can override: - A property defined in the command line (/d:propertyName=value) has which can override: - A property defined in the SonarQube.Analysis.xml configuration file [this file] which can override: - A property defined in the SonarQube User Interface at project level which can override: - A property defined in the SonarQube User Interface at global level which can't override anything. Note that the following properties cannot be set through an MSBuild project file or an SonarQube.Analysis.xml file: sonar.projectName, sonar.projectKey, sonar.projectVersion The following flags need to be used to set their value: /n:[SonarQube Project Name] /k:[SonarQube Project Key] /v:[SonarQube Project Version]--><SonarQubeAnalysisProperties xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:xsd="http://www./2001/XMLSchema" xmlns="http://www./msbuild/integration/2015/1"> <Property Name="sonar.host.url">http://sonar_ip:sonar_port</Property> <Property Name="sonar.login">login_username</Property> <Property Name="sonar.password">login_password</Property> <!-- Required only for versions of SonarQube prior to 5.2 --> <Property Name="sonar.jdbc.url">jdbc:mysql://db_ip:db_port/sonar?useUnicode=true;characterEncoding=utf8;rewriteBatchedStatements=true;useConfigs=maxPerformance;useSSL=false</Property> <Property Name="sonar.jdbc.username">jdbc.username</Property> <Property Name="sonar.jdbc.password">jdbc.password</Property> </SonarQubeAnalysisProperties>
3. MSBuild安裝與配置 Visual Studio IDE在編譯*.sln解決方案時默認是調(diào)用msbuild.exe來實現(xiàn)的。如果你的機器上沒有裝有Visual Studio,那么也可以單獨使用MSBuild來編譯.sln(工程解決方案)或.csproj(項目)。MSBuild可以直接通過.NETFramework來安裝獲得。
msbuild.exe的路徑一般如下:
X86: C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exeX64: C:\Program Files (x86)\MSBuild\14.0\Bin\amd64\MSBuild.exe
msbuild.exe 目錄如下所示:

將MSBuild.exe添加到Path環(huán)境變量,便于后面在命令行中調(diào)用MSBuild。
msbuild常用編譯命令:
MSBuild MyApp.sln /t:Rebuild /p:Configuration=ReleaseMSBuild MyApp.csproj /t:Clean /p:Configuration=Debug;/p:Platform=x86;TargetFrameworkVersion=v3.5 編譯為 Release 代碼 -p:configuration="release"清理項目 -t:clean重新編譯 -t:rebuild編譯項目 -t:build 默認可以忽略這個參數(shù)發(fā)布 -t:Publish 注意:這里的 -t 和 /t 作用是相同的。
4. Sonar+命令行分析C#代碼 1、打開CMD,切換到指定的項目根目錄,必須和.sln或者.csproj同級目錄。例如以\hcloud\Common\KDY.WebApi.Core項目為例,如下圖所示。

2、使用MSBuild方式進行Sonar Scanner掃描代碼前期準備文件生成,CMD命令下運行: SonarScanner.MSBuild.exe begin /k:"hcloud.Common.KDY.WebApi.Core" /n:"hcloud.Common.KDY.WebApi.Core" /v:"1.0"
命令執(zhí)行結(jié)果如下:

參數(shù)說明: /key(簡寫k):對應(yīng)projectKey即項目的唯一代碼,如兩套源代碼使用同一個projectKey那掃描的結(jié)果將混在一起,所以一個項目需要有一個單獨的projectKey /name(簡寫n):對應(yīng)projectName即項目的名稱,為項目的一個顯示的名稱,建立使用完整的項目名稱 /version(簡寫v):對應(yīng)projectVersion即項目的版本,項目在不同的時期版本也是不一樣的,如果方便,可以在sonarQube的服務(wù)器中查看到不同的版本代碼其中問題的變化
執(zhí)行上述命令后,在項目目錄下,生成.sonarqube目錄。 3、通過MSBuild命令編譯項目,在CMD命令行下執(zhí)行: MSBuild.exe /t:Rebuild (默認為Debug模式)
或者MSBuild.exe /t:Rebuild /p:Configuration=Release (指定編譯模式) 或者MSBuild.exe D:\hcloud\Common\Common.sln /t:Rebuild (指定具體的.sln解決方案)
編譯項目運行結(jié)果如下所示: 
0個錯誤,則代表MSBuild編譯成功,編譯成功后,在當(dāng)前目錄下會生成一個obj目錄。(編譯成功后默認生成Debug產(chǎn)物),SonarQube分析C#項目工程時,前提需要MSBuild能預(yù)編譯成功,如果存在錯誤,則無法成功完成后續(xù)Sonar分析動作。 4、分析C#掃描結(jié)果,將分析報告上傳給SonarQube,CMD命令下運行: SonarScanner.MSBuild.exe end
執(zhí)行結(jié)果如下圖所示: 
溫馨提示: 1、如果運行出現(xiàn)錯誤請檢查sonar server的log,路徑為Snoar\sonarqube-6.7\logs下的sonar.log,web.log和access.log。 2、如果遇到需要檢測比較大的項目,可能上傳的mysql數(shù)據(jù)量會很大,會超出默認的mysql上傳的最大值,此時需要設(shè)置mysql的max_allowed_packet。 5、查看Sonar分析掃描后的結(jié)果,訪問http://10.0.0.147:9000/dashboard?id=hcloud.Common.KDY.WebApi.Core,分析結(jié)果如下圖所示: 
5. Jenkins+Sonar+MSBuild分析C#代碼 1、編譯.NET(C#)應(yīng)用程序可通過微軟提供的MSBuild工具,先安裝插件MSBuild,在Jenkins中搜索并安裝MSBuild插件,如下圖所示。

2、插件安裝完畢后,進入系統(tǒng)管理->全局工具配置(Global Tool Configuration)找到MSBuild配置選項,如下圖所示。 
3、配置SonarScanner for MSBuild,如下圖所示。 
4、由于示例中的Jenkins服務(wù)是部署在Linux系統(tǒng)中,故此處可添加一臺Windows主機(10.0.0.148)作為C#項目編譯運行環(huán)境,在Windows從節(jié)點配置中,添加并配置相應(yīng)工具,如下圖所示。 
5、新建并配置JOB,添加JOB運行節(jié)點(編譯C#工程項目的運行機),如下圖所示。 
6、配置源碼管理及其它所需配置(較為簡單,此處省略)后,添加并配置構(gòu)建選項,如下圖所示。

7、JOB構(gòu)建運行結(jié)果如下圖所示。

8、JOB構(gòu)建成功后,Sonar代碼分析報告如下圖所示。 
6. 常見問題 1、解決SonarQube檢測C#執(zhí)行成功,但不能獲取檢測結(jié)果的問題,現(xiàn)象如下圖所示。

由圖中可以看到文件掃描成功了,但是卻沒有任何文件被發(fā)現(xiàn),所有的指標(biāo)數(shù)據(jù)皆為0。 將Sonar插件中的C#插件改為5.9的版本即可。修改方式將plugin目錄下原本的C#插件刪除掉,將5.9版本的插件放入進來。重啟SonarQube后問題即可解決。(備注示例中的SonarQube版本為6.7.5) plugin目錄替換后如下圖所示:

2. Jenkins +MSBuild+Sonar構(gòu)建編譯Job時提示Running the Scanner for MSBuild under Local System or Network Service account is not supported. Please, use a local or domain user account instead. 現(xiàn)象如下圖所示: 
登錄從節(jié)點10.0.0.148(windows主機),右擊我的電腦選擇管理然后從管理界面里面找到服務(wù)或者在cmd界面輸入services.msc打開服務(wù)管理界面,從服務(wù)管理界面找到j(luò)enkins slave服務(wù),右鍵點擊屬性,在彈出的對話框中切換到登陸標(biāo)簽,默認登錄方式為本地系統(tǒng)賬號,此處我們選擇此賬戶。然后輸入賬戶和密碼點擊確定,完成以上操作以后重新啟動jenkins slave服務(wù)然后再重新執(zhí)行即可。 
3、Jenkins單獨構(gòu)建沒問題,Sonar靜態(tài)檢查代碼單獨執(zhí)行也沒問題,但是Jenkins+Sonar集成時出現(xiàn)未經(jīng)授權(quán)問題,現(xiàn)象如下圖所示。 
原因是由于Jenkins上已經(jīng)通過admin生成了Token來進行連接認證,需要注釋掉SonarQube.Analysis.xml里面的sonar.login和sonar.password,刪除或者注釋后,再重新執(zhí)行即可。 修改如下圖所示(下圖采用注釋來解決該問題的)。

|