Hi everyone. We meet again to see another great way to enhance our CI pipeline and code quality.
Code quality is always a challenge. Yes you are right there are common standards for best programming practices but this task – code quality and review – needs time and effort from the development team. Fortunately we have some software do the trick where you can define your rules and it will scan the code against them.
One of these software is SonarQube which is a quality management platform, dedicated to continuously analyze and measure technical quality, from project portfolio to method.
So today I will show you how you can create VM on Azure to host, install, and configure SonarQube to use it by VSTS.
So enough talking and let’s start working.
First we will start by creating a server for SonarQube which will be VM hosted on MS Azure and it will be a Windows Server 2016 Data Center.
Create SonarQube Server/VM:
- Login to your Azure portal.
- Click on Virtual machines from the left menu.
- Click on Add.
- Filter by typing Windows Server 2016 Datacenter.
- Choose Windows Server 2016 Datacenter from the result list.
- Make sure to select Resource Manager as a deployment model.
- Click on Create.
- Fill required information as showing in below screenshots (note these screenshots are just to illustrate and guide you through this post and they are not compulsory. You can change the configuration based on your requirements)
Configure SonarQube Server:
-
- Download Java runtime from Oracle website. Make sure to choose the correct jre based on your server type.
- Install Java runtime by double clicking the installation file.
- Click on Next.
- Click on Close once the installation finished.
- Install SonarQube from its official page.
- Click on Download button then choose the latest LTS version.
- Unzip the folder to the your required path e.g. C:\sonarqube-5.6.6
- SonarQube by default runs under port 9000, but this can be changed to any thing you want. We are going to change it to be 80 the default HTTP port so navigate to the SonarQube’s config folder e.g. C:\sonarqube-5.6.6\conf folder.
- Open sonar.properties file using your favorite text editor e.g. notepad.
- Find the line says #sonar.web.port=9000 and change it to be sonar.web.port=80. Notice we removed the hash # and changed the 9000 to be 80.
# Web context. When set, it must start with forward slash (for example /sonarqube). # The default value is root context (empty value). #sonar.web.context= # TCP port for incoming HTTP connections. Default value is 9000. sonar.web.port=80
- Now we going to install SonarQube into our server. Open command line in administrator mode.
- In cmd navigate to your SonarQube directory, then to bin folder, then to the folder matching your server type. e.g. C:\sonarqube-5.6.6\bin\windows-x86-64
- Run InstallNTService.bat batch file. This will install a service into your server called SonarQube.
- Run StartNTService.bat batch file. This will start the created service.
- Close command line and open your browser from the server itself and type http://localhost/ .
- By default the administrative credentials for SonarQube are admin as username and password.
- By default Windows Server 2016 firewall blocks all incoming connection on port 80, so we need to open the port to accept the connection from VSTS. Use Windows Server 2016 search and type Windows Firewall with Advanced Security then click on the result.
- Firewall window will open click on Inbound Rules from left menu.
- Click on New Rule from right menu.
- A wizard form will appear to guide you through the setup. In First step choose Port and click on Next.
- In the protocol and ports step choose TCP and Specific local ports and make it 80 then click on Next.
- Choose Allow the connection in the Action step and click Next.
- In the Profile step make sure you check all options then click on Next.
- In last step give the rule a name e.g. SonarQube Portal and a description and click on Finish. This will create the new rule.
- Next you need to open the connection from Azure Portal itself since the server is behind a network firewall as well.
- Go to Azure portal and open your server network security group then click on Inbound security rules.
- Click on Add.
- Fill the new rule with following details:
- Name: SonarQube Portal
- Prority: 1010
- Source: Any
- Service: HTTP
- Action: Allow
- Click on OK.
- Now you need to associate server’s virtual network with a subnet. Choose Subnets from the network security group.
- Click on Associate.
- Choose your server’s virtual network (vnet).
- Choose your server’s Subnet.
- Click OK.
- To make sure the port is opened you from your local machine open the Internet browser and type http://%5BYour Server IP]/ e.g. http://52.47.123.17/ if SonarQube’s portal opened then everything is OK.
Configure VSTS:
- Go to VisualStudio Marketplace.
- Click on Visual studio team services from upper menu.
- Type SonarQube on the search text box and click on search button.
- Click on SonarQube from the result section.
- In the next page click on Install.
- On the modal window click choose the account where you want to install SonarQube to.
- Click on Continue then on Confirm.
- Open your VSTS account and navigate to the project you want to scan its code.
- In the build definition add two new tasks.
- SonarQube Scanner for MSBuild – Begin Analysis (new)
- SonarQube Scanner for MSBuild – End Analysis (new)
- Move SonarQube Scanner for MSBuild – Begin Analysis (new) task before the Build solution task.
- Move SonarQube Scanner for MSBuild – End Analysis (new) to be after the Unit test task.
- We will generate a token to allow the VSTS accessing the SonarQube. Login to the SonarQube portal and click on My Account.
- Click on Security tab.
- Type VSTS into the text box and click on Generate.
- A token will be generated. Click on Copy button next to it. Make sure not to lose it because you will not be able to get the value again and you need to revoke the token and generate another one.
- Go back to the SonarQube Scanner for MSBuild – Begin Analysis (new) task on the build definition and click on the plus sign to configure the SonarQube EndPoint.
- Enter the connection name e.g. SonarQube
- Server Url e.g. http://52.47.123.17/
- Token generated from SonarQube Portal.
- Click on OK.
- Enter a project key e.g. EpicCalculator. This is a unique key will be used by SonarQube to differentiate your project from another projects.
- Enter a project name e.g. Epic Calculator. This is the display name from the project which will show in the SonarQube portal.
- Save and queue your build.
- Once the build finishes successfully you will get the results in the SonarQube’s portal. (You may need to change the Quality Gate to make sure the build success).
So this reach us to the end of this post, until next post hope you happy coding 😉
where can I get this IP: http://52.47.123.17/
LikeLike
This IP is an example of your SonarQube server. In my case it was this one but in yours it will be different.
LikeLike
followed every step given in your blog above but sonar is not running
LikeLike
Can I know what is the issue you are facing?
Is there any error message, log files?
LikeLike
Sir I am configuring SonarQube on Azure’s Virtual Machine but I am getting error i.e
The SonarQube service was launched, but failed to start.
Kindly tell how to resolve this error
LikeLike
Hi Vikhyat,
Kindly note that, this is an old post with old software versions where a lot of things may not work as expected on the current versions.
I’ll do my best to update the post this weekend and will let you know, so you can retry yourself.
LikeLike