SonarQube Hosted On Azure App Service

Premier Developer

Premier

This post is provided by Premier Field Engineer, Nathan Vanderby, who simplifies the setup of a SonarQube server with one step using Azure App Services.


SonarQube is tool that centralizes static code analysis and unit test coverage. It can be used across multiple languages and for a single project up to enterprise scale.

SQProject

There are various guides on how to setup SonarQube hosted in Azure. From hosting it on a virtual machine (here and here) to a docker image or a Linux container in an Azure App Service. There are also instructions for using IIS as a reverse proxy to allow SSL traffic for additional security.

There is another project out there to try and simplify setting up a SonarQube server that is publicly accessible. The project is SonarQube-AzureAppService. It use the HttpPlatformHandler extension from IIS which can similarly be used to host SonarQube on-prem with IIS directly.

SonarQube-AzureAppService

Project URL: https://github.com/vanderby/SonarQube-AzureAppService

This project simplifies the setup of a SonarQube server to one step. Simply click the Deploy to Azure link on the project homepage and follow the simple walkthrough to have resources deployed out and configured. The initial start time for SonarQube may take up to 10 minutes on slower resources. That’s all! Read below for more in depth details on what’s going on behind the scenes.

Deploying Azure Resources

The Deploy To Azure button on the GitHub repository uses the azuredeploy.json project file to deploy an ARM template. The ARM template defines a simple App Service Plan, Web App and the code from the GitHub repository. The code is pushed to the repository folder in the web app. Within the project a .deployment file defines a deployment script to execute.

DeployToAzure

On a side note, at the time of writing the minimum App Service Plan tier is Basic. The Free and Shared tiers throw an error on startup related to Java memory restrictions with the default SonarQube settings.

Deployment Script

The deployment script is Deploy-SonarQuveAzureAppService.ps1. This script copies the wwwroot folder from the repo, which contains the web.config and HttpPlatformHandlerStartup.ps1 files, to the web app wwwroot folder. I’ll walk through these files later. It also downloads and extracts the latest SonarQube binaries. I’ve removed the logging and error handling lines for brevity in the code block below.

Once the deployment is complete your web app wwwroot folder should have a sonarqube folder and just a few files. You could also manually upload sonarqube binaries.

AzureDeployFiles

Runtime Files

This project was made possible due to how an app service hosts Java applications. It does this using the HttpPlatformHandler extension. This extension will start any executable defined in the web.config and forward any requests it receives to the port defined by the HTTP_PLATFORM_PORT environment variable. This environment variable is randomly set by the HttpPlatformHandler when it invokes the startup executable.

Web.Config

The web.config file shown below is very simple. It adds the HttpPlatformHandler extension to the handlers then defines the behavior of the HttpPlatformHandler. The handler itself is told to run PowerShell and execute the HttpPlatformHandlerStartup.ps1 script. We will go into details of this script later. It also tells the handler to log stdout messages, do not retry the startup and wait 300 seconds before timeout on startup. We want a long startup timeout since SonarQube takes a while to start. Especially the first time if your using an in-memory database.

HttpPlatformHandlerStartup.ps1

This script writes the HTTP_PLATFORM_PORT port to the sonar.properties file, updates the wrapper.conf with the java.exe location and runs StartSonar.bat. The most important thing happening here is setting the HTTP_PLATFORM_PORT as it is randomized each time the application is started. I’ve removed the logging and error handling lines for brevity in the code block below.

On another side note, the script executes the last StartSonar.bat file it finds so that it runs the script from the x64 folder and not the x86 if present.

Hopefully this is helpful if you are looking at setting up a cloud based SonarQube server.

Premier Developer
Premier Developer

Premier Support for Developers

Follow Premier   

No Comments.