Updated 03/11/2020
Sonarqube provides static code analysis, testing and continuous inspection.
You can use the public Sonarqube image and embedded H2 database to test out Sonarqube locally, H2 is not suitable for production but great to learn the product in a safe local environment.
This demo uses a random monorepository I forgot I had, the tests are MS Test but here Im just focusing on the code quality like smells and bugs, so the report wont include test coverage. Have a look at Sonarqube With Opencover And xUnit Reports if you looking for some test coverage.
Software Setup
Sonarqube Server
- Start the Sonarqube container, I like to use specific versions as
latest
can have breaking changes down the line, below I used6.7.4
. The default login isadmin\admin
1 | docker run -d --name sonarqube674 -p 9000:9000 sonarqube:6.7.4 |
- When Sonar v674 first starts it will ask you to create a token, the token is used to identify you when an analysis is performed. Generally you would store this in your secrets manager but as this is a local demo, my token was
sonardemo: a8d6e1e800aaeeec2986b202177d74677d8acbb4
, I skipped the rest of the tutorial when it asked me to create a new project, this will be done for you on the first scan.
Sonar Scanner
- Locally you will need sonar scanner, at the time of writing this the latest version was 4.3.1.1372
sonar-scanner-msbuild-4.3.1.1372-net46.zip
is for .Net Frameworksonar-scanner-msbuild-4.3.1.1372-netcoreapp2.0.zip
is for .Net Core
Download sonar-scanner-msbuild-4.3.1.1372-netcoreapp2.0.zip
Extract the contents of the zip file into
C:\dev\sonardemo\tools
, the key is to seeSonarScanner.MSBuild.dll
in the root of tools.The scanner has a Java dependancy, download and install JDK8 jre-8u181-windows-x64.exe, you can verify its installed from the console by checking the version
1 | java -version |
.Net Core SDK
- Download and install .NET Core SDK 2.1.202
This gives us MSBuild version 15x which will work with SonarScanner and the source code below.
Setup Source Code
You can scan any project, here I’ll just use a random project from my Github space.
Clone the project Boilerplate to
c:\dev\sonardemo\tmp
Copy the contents of
C:\dev\sonardemo\tmp\Boilerplate\Class Library\PatternsAndPrinciples
toC:\dev\sonardemo\
so that the.sln
file is in the root, this just makes the sonar steps easier.Ensure the 3 csproj files target
netcoreapp2.0
, its confusing because I thought 2.1 would work but it doesnt, its because the SDK we build with below, which is v2.1.202 targets.Net Core 2.0
… too many numbers, just go with it (o_O)
1 | <Project Sdk="Microsoft.NET.Sdk"> |
- Run
dotnet restore
to update the assets, this is magic the IDE normally does for us :D
Scan time!
- The first step is to run the scanner, here we are passing the key
PatternsAndPrinciples
for the project to be created with login details from the step steps.
1 | dotnet ./tools/SonarScanner.MSBuild.dll begin /k:"PatternsAndPrinciples" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="a8d6e1e800aaeeec2986b202177d74677d8acbb4" |
- The second step is to build the project, needs to use MSBuild 14.0 or 15.0. The
.
at the end means current context so will look for the.sln
you copied earlier to the root ofsonardemo
1 | dotnet "C:\Program Files\dotnet\sdk\2.1.202\MSBuild.dll" . |
- The last step is to end the scan
1 | dotnet ./tools/SonarScanner.MSBuild.dll end /d:sonar.login="a8d6e1e800aaeeec2986b202177d74677d8acbb4" |
The code analysis can then be seen at http://localhost:9000/dashboard?id=PatternsAndPrinciples
References
- Also see Quality Assurance Tools
- You can also have these validations as part of your IDE – https://www.sonarlint.org/visualstudio/
- https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+MSBuild
- https://docs.sonarqube.org/display/SONAR/Project+Existence