SonarQubeScanner & UnitTest 使用 Jenkins Pipeline
使用 Jenkins Plugin 可以參考這篇,此篇是使用 Pipeline 來實現 SonarQube 掃描
.net framework
3. Pipeline
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| node {
VERSION = VersionNumber([projectStartDate: '2015-01-01', versionNumberString: '${YEARS_SINCE_PROJECT_START}.${BUILD_MONTH}.${BUILD_DAY}.${BUILDS_TODAY}', versionPrefix: '', worstResultForIncrement: 'NOT_BUILT']) TheJobName ="${env.JOB_NAME}"
stage('Sonarqube Scan Begin'){ echo "Sonarqube Scan Begin Start" bat "D:\\tools\\sonar-scanner-msbuild-4.4.2.1543-net46\\SonarQube.Scanner.MSBuild.exe begin /k:${TheJobName} /n:${TheJobNopame} /v:${VERSION} /d:sonar.exclusions=obj\\*,bin\\*,packages\\**,Properties\\*" echo "Sonarqube Scan Begin OK" } stage('Msbuild'){ echo "Msbuild Start" bat "\"C:/Program Files (x86)/MSBuild/14.0/bin/amd64/msbuild.exe\" ${TheJobName}.sln /t:Rebuild /p:Configuration=Release" echo "Msbuild OK" } stage('OpenCover') { echo "OpenCover Start" bat "%LOCALAPPDATA%\\Apps\\OpenCover\\OpenCover.Console.exe -output:\"%CD%\\opencover.xml\" -register:user -target:\"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Enterprise\\Common7\\IDE\\CommonExtensions\\Microsoft\\TestWindow\\vstest.console.exe\" -targetargs:\"${TheJobName}.Test\\bin\\Release \\${TheJobName}.Test.dll \"/logger:trx\"" echo "OpenCover OK" } stage('Sonarqube Scan End'){ echo "Sonarqube Scan End Start" bat "D:\\tools\\sonar-scanner-msbuild-4.4.2.1543-net46\\SonarQube.Scanner.MSBuild.exe end" echo "Sonarqube Scan End OK" } }
|
.Net Core
1. 專案安裝 coverlet.msbuild,因為要產生 coverage.opencover.xm
1
| dotnet add package coverlet.msbuild
|
2. jenkins Server 安裝 dotnet-sonarscanner
1
| dotnet tool install --global dotnet-sonarscanner --version 4.3.1
|
3. pipeline
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| node {
VERSION = VersionNumber([projectStartDate: '2015-01-01', versionNumberString: '${YEARS_SINCE_PROJECT_START}.${BUILD_MONTH}.${BUILD_DAY}.${BUILDS_TODAY}', versionPrefix: '', worstResultForIncrement: 'NOT_BUILT']) TheJobName ="${env.JOB_NAME}"
stage('dotnet sonarscanner begin') { echo "dotnet sonarscanner begin Start" bat "dotnet sonarscanner begin /k:${TheJobName} /n:${TheJobName} /v:${VERSION} /d:sonar.exclusions=obj\\*,bin\\*,packages\\**,Properties\\* /d:sonar.cs.opencover.reportsPaths=\"${TheJobName}.Test\\coverage.opencover.xml\" " echo "dotnet sonarscanner begin OK" } stage('dotnet sonarscanner build') { echo "dotnet sonarscanner build Start" bat "dotnet build ${TheJobName}.sln -c Release -p:Version=${VERSION}" echo "dotnet sonarscanner build OK" } stage('dotnet test') { echo "dotnet test Start" bat "dotnet test ${TheJobName}.Test --logger:trx /p:CollectCoverage=true /p:CoverletOutputFormat=opencover" echo "dotnet test OK" } stage("dotnet sonarscanner end") { echo "dotnet sonarscanner end Start" bat "dotnet sonarscanner end" echo "dotnet sonarscanner end OK" } }
|
參考