Static code analysis- SonarQube open source Tool
Introduction
SonarQube is an open source product, produced by SonarSource SA, which consists in a set of static analyzers (for many languages), a data mart, and a portal that enables you to manage your technical debt. SonarSource and the community provide additional analyzers (free or commercial) that can be added to a SonarQube installation as plug-ins. SonarSource and Microsoft have been working to integrate SonarQube with MSBuild.SonarQube is an open-source platform developed by SonarSource for continuous inspection of code quality to perform automatic reviews with static analysis of code to detect bugs, code smells, and security vulnerabilities on 20+ programming languages. SonarQube offers reports on duplicated code, coding standards, unit tests, code coverage, code complexity, comments, bugs, and security vulnerabilities. SonarQube can record metrics history and provides evolution graphs.
SonarQube includes support for the below programming languages
Java, C#, PHP, JavaScript, TypeScript, C/C++, Ruby, Kotlin, Go, COBOL, PL/SQL, PL/I, ABAP, VB.NET, VB6, Python, RPG, Flex, Objective-C, Swift, CSS, HTML, and XML.Some of these are only available via a commercial license .I have verified VB.NET, C#,JavaScript, CSS, HTML files and these are available in open-source.
Prerequisite
The only prerequisite for running SonarQube is to have Java (Oracle JRE 11 or OpenJDK 11) installed on your machine.
Installation Steps in developer machines
- Download the SonarQube Community Edition https://www.sonarqube.org/downloads/
- Unzip it, let's say in C:\sonarqube
- Start the SonarQube Server by running below command at command prompt # On Windows, execute:
- C:\sonarqube\bin\windows-x86-xx\StartSonar.bat # On other operating systems, as a non-root user execute:
- /sonarqube/bin/[OS]/sonar.sh console
Once the above .bat file runs on windows machine, by default sonarqube dashboard service will up and run on default port 9000. So, the url to see the dashboard is http://localhost:9000
Note: if you want to change the port number you can change it through the properties file which is at inside of sonarqube folder(C:\sonarqube)
4. Log in to http://localhost:9000 with System Administrator credentials (login=admin, password=admin)
5. Click the Create new project button to analyze your first project.
After we run StartSonar.bat file, SonarQube should be up and running!
Project analysis from the dashboard
The first thing we need to do is to create a new project
Then we need to generate a token that will be used for login purpose also to download sonar scanner for the targeted framework:
Now add its path as well as an MS-Build path to the environment path variable:
The only thing that's left to do now is to run given commands, one after another, from the root level of your project:
That's it ! Wait for a few minutes and the results will be displayed in the web portal i.e http://localhost:9000. Just don't be surprise if you get something like this:)
Project analysis from the project root folder
We can also do analysis from the project folder using below steps
- goto command prompt
- goto the root folder of project
- execute below commands
MSBuild.SonarQube.Runner.exe begin \k:<keyname> \n:<projectname> \v:<version> and press enter
MSBuild.exe /t:Rebuild
MSBuild.SonarQube.Runner.exe end and press enter to finish the analysis
After executing the above command then you will see below output
Integrating in Jenkins
- open local jenkins url i.e http://localhost:8080/
- Install SonarQube plug in
goto Manage Jenkins->Manage plugins
- Select Available tab and search for Sonar as like below
- After installing , got to Manage Jenkins->Global Tool Configuration
This step is mandatory if you want to trigger any of your analyses with the SonarScanner for MSBuild. You can define as many scanner instances as you wish. Then for each Jenkins job, you will be able to choose with which launcher to use to run the SonarQube analysis
- Configure the project, and go to the Build section.
- Add the SonarScanner build step to your build.
- Configure the SonarQube analysis properties. You can either point to an existing sonar-project.properties file or set the analysis properties directly in the Analysis properties field
For more details about how to set up Jenkins pipeline to run the sonar scanner on Git, pl referrer below url
Once the configuration done, click Build Now as like below
Once build starts then you can see how the sonar scanner invokes and analyse the code from the console output as like below
Comments
Post a Comment