Container used to build projects and provide SonarQube analysis using Sonar-Scanner for DotNetCore.
636
This is a container used to build projects and provide SonarQube analysis using Sonar-Scanner for DotNetCore.
It also allows you to run Docker in Docker using a docker.sock mount.
This latest image was built with the following components:
Tags are written using the following pattern: dotnet-sonar:<year>.<month>.<revision>
21.06.1, latest
21.06.0
21.05.1
21.05.0
21.01.1
21.01.0
20.11.1
20.11.0
19.10.1
19.09.0
More info on docker hub: https://hub.docker.com/r/gafda/dotnet-sonar/
Full documentation: https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+MSBuild
Inside container:
dotnet /sonar-scanner/SonarScanner.MSBuild.dll begin /k:sonarProjectKey
dotnet build
dotnet /sonar-scanner/SonarScanner.MSBuild.dll end
Configure external SonarQube Server:
dotnet /sonar-scanner/SonarScanner.MSBuild.dll begin /k:sonarProjectKey /d:sonar.host.url="<SonarQubeServerUrl:Port>" /d:sonar.login="<SonarQubeServerToken>"
dotnet build
dotnet /sonar-scanner/SonarScanner.MSBuild.dll end /d:sonar.login="<SonarQubeServerToken>"
Outside container:
Simple Usage:
docker run -it --rm -v <my-project-source-path>:/source gafda/dotnet-sonar:latest bash -c "cd source \
&& dotnet /sonar-scanner/SonarScanner.MSBuild.dll begin /k:sonarProjectKey /name:sonarProjectName /version:buildVersion \
&& dotnet restore \
&& dotnet build -c Release \
&& dotnet /sonar-scanner/SonarScanner.MSBuild.dll end"
Advanced Usage:
docker run -it --rm \
-v <my-project-source-path>:/source \
-v <my-nugetconfig-source-path>:/nuget \
dotnet-sonar:21.01.1 \
bash -c \
"cd source \
&& dotnet /sonar-scanner/SonarScanner.MSBuild.dll begin \
/k:<ProjectName> /name:<my-project-name> /version:<my-project-version> \
/d:sonar.host.url="<my-sonar-server-url>" \
/d:sonar.login="<my-sonar-server-user>" \
/d:sonar.password="<my-sonar-server-pass>" \
/d:sonar.cs.opencover.reportsPaths='tests/**/coverage.opencover.xml' \
&& dotnet restore --configfile /nuget/NuGet.Config \
&& dotnet build -c Release \
&& dotnet publish -c Release -r linux-x64 -o deployment \
&& dotnet test --no-build -c Release --filter "Category=Unit" --logger trx --results-directory testResults /p:CollectCoverage=true /p:CoverletOutputFormat=\"opencover\" \
&& dotnet /sonar-scanner/SonarScanner.MSBuild.dll end \
/d:sonar.login="<my-sonar-server-user>" \
/d:sonar.password="<my-sonar-server-pass>""
The script above does the following:
The following pipeline code will:
Note that in order for coverage analysis to work, you need to add the coverlet NuGet package to the unit test project.
node('somenode-with-docker') {
withSonarQubeEnv('my-jenkins-configured-sonar-environment') {
docker.image('gafda/dotnet-sonar:latest').inside() {
withEnv(['HOME=/tmp/home','DOTNET_CLI_TELEMETRY_OPTOUT=1']) {
stage('build') {
checkout scm
sh "dotnet /sonar-scanner/SonarScanner.MSBuild.dll begin /k:someKey /name:someName /version:someVersion /d:sonar.cs.opencover.reportsPaths='tests/**/coverage.opencover.xml'"
sh "dotnet build -c Release /property:Version=someVersion"
sh "rm -drf ${env.WORKSPACE}/testResults"
sh (returnStatus: true, script: "find tests/**/* -name \'*.csproj\' -print0 | xargs -L1 -0 -P 8 dotnet test --no-build -c Release --logger trx --results-directory ${env.WORKSPACE}/testResults /p:CollectCoverage=true /p:CoverletOutputFormat=opencover")
step([$class: 'XUnitPublisher', testTimeMargin: '3000', thresholdMode: 1, thresholds: [[$class: 'FailedThreshold', unstableThreshold: '0']
, [$class: 'SkippedThreshold']], tools: [[$class: 'MSTestJunitHudsonTestType', deleteOutputFiles: true, failIfNotNew: false
, pattern: 'testResults/**/*.trx', skipNoTestFiles: true, stopProcessingIfError: true]]])
sh "dotnet /sonar-scanner/SonarScanner.MSBuild.dll end"
}
}
}
}
}
timeout(time: 1, unit: 'HOURS') {
def qualityGate = waitForQualityGate()
if (qualityGate.status == 'ERROR') {
currentBuild.result = 'UNSTABLE'
}
}
If you want to use Docker in Docker:
Please note that if you want to use Docker inside Docker (DinD) you need to perform additional actions when mounting the docker image in the pipeline.
The following actions will expose your host to several security vulnerabilities and therefore this should only be used when you absolutely must to:
docker.image('gafda/dotnet-sonar:latest').inside("--group-add docker -v /var/run/docker.sock:/var/run/docker.sock") {
// Some stuff
docker.image.('hello-world:latest').inside() {
sh "echo 'hello from docker inside docker'"
}
}
The above code will:
The above examples already implement the code-coverage analysis, provided you add the coverlet NuGet package to your unit test project.
If you want to know more, check https://dev.to/deinsoftware/net-core-unit-test-and-code-coverage-with-visual-studio-code-37bp.
Also, coverlet documentation here: https://github.com/tonerdo/coverlet/.
Content type
Image
Digest
Size
563.9 MB
Last updated
about 5 years ago
docker pull gafda/dotnet-sonar