Step by step how to create SonarQube VM using MS Azure, and configure it to be integrated with VSTS

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:

  1. Login to your Azure portal.
  2. Click on Virtual machines from the left menu.
  3. Click on Add.CreateServer1
  4. Filter by typing Windows Server 2016 Datacenter.
  5. Choose Windows Server 2016 Datacenter from the result list.
  6. Make sure to select Resource Manager as a deployment model.
  7. Click on Create.CreateServer2
  8. 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)CreateServer3

CreateServer4CreateServer5CreateServer6


Configure SonarQube Server:

    1. Download Java runtime from Oracle website. Make sure to choose the correct jre based on your server type.Sonar2
    2. Install Java runtime by double clicking the installation file.
    3. Click on Next.Sonar3
    4. Click on Close once the installation finished.Sonar4
    5. Install SonarQube from its official page.Sonar5
    6. Click on Download button then choose the latest LTS version.Sonar6
    7. Unzip the folder to the your required path e.g. C:\sonarqube-5.6.6
    8. 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.
    9. Open sonar.properties file using your favorite text editor e.g. notepad.
    10. 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
      
    11. Now we going to install SonarQube into our server. Open command line in administrator mode.
    12. 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
    13. Run InstallNTService.bat batch file. This will install a service into your server called SonarQube.
    14. Run StartNTService.bat batch file. This will start the created service.Sonar7Sonar8
    15. Close command line and open your browser from the server itself and type http://localhost/ .
    16. By default the administrative credentials for SonarQube are admin as username and password.
    17. 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.sonar13
    18. Firewall window will open click on Inbound Rules from left menu.
    19. Click on New Rule from right menu.sonar14
    20. A wizard form will appear to guide you through the setup. In First step choose Port and click on Next.sonar15
    21. In the protocol and ports step choose TCP and Specific local ports and make it 80 then click on Next.sonar16
    22. Choose Allow the connection in the Action step and click Next.sonar17
    23. In the Profile step make sure you check all options then click on Next.sonar18
    24. 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.sonar19sonar20
    25. Next you need to open the connection from Azure Portal itself since the server is behind a network firewall as well.
    26. Go to Azure portal and open your server network security group then click on Inbound security rules.
    27. Click on Add.sonar21
    28. Fill the new rule with following details:
      • Name: SonarQube Portal
      • Prority: 1010
      • Source: Any
      • Service: HTTP
      • Action: Allow
    29. Click on OK.sonar22
    30. Now you need to associate server’s virtual network with a subnet. Choose Subnets from the network security group.
    31. Click on Associate.sonar23
    32. Choose your server’s virtual network (vnet).sonar24
    33. Choose your server’s Subnet.sonar25
    34. Click OK.
    35. 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:

  1. Go to VisualStudio Marketplace.
  2. Click on Visual studio team services from upper menu.
  3. Type SonarQube on the search text box and click on search button.
  4. Click on SonarQube from the result section.Sonar10
  5. In the next page click on Install.Sonar11
  6. On the modal window click choose the account where you want to install SonarQube to.
  7. Click on Continue then on Confirm.Sonar12
  8. Open your VSTS account and navigate to the project you want to scan its code.
  9. In the build definition add two new tasks.
    • SonarQube Scanner for MSBuild – Begin Analysis (new)
    • SonarQube Scanner for MSBuild – End Analysis (new)
  10. Move SonarQube Scanner for MSBuild – Begin Analysis (new) task before the Build solution task.
  11.  Move SonarQube Scanner for MSBuild – End Analysis (new) to be after the Unit test task.sonar26sonar27
  12. We will generate a token to allow the VSTS accessing the SonarQube. Login to the SonarQube portal and click on My Account.sonar28
  13. Click on Security tab.
  14. Type VSTS into the text box and click on Generate.
  15. 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.sonar29
  16. 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.
  17. Enter the connection name e.g. SonarQube
  18. Server Url e.g. http://52.47.123.17/
  19. Token generated from SonarQube Portal.
  20. Click on OK.sonar30
  21. Enter a project key e.g. EpicCalculator. This is a unique key will be used by SonarQube to differentiate your project from another projects.
  22. Enter a project name e.g. Epic Calculator. This is the display name from the project which will show in the SonarQube portal.
  23. Save and queue your build.
  24. 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).sonar31

So this reach us to the end of this post, until next post hope you happy coding 😉

6 thoughts on “Step by step how to create SonarQube VM using MS Azure, and configure it to be integrated with VSTS

      • 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

        Like

      • 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.

        Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.