Integrating SonarQube with Azure DevOps can be a game-changer for your development team. With this integration, you can leverage Azure DevOps' powerful features to enhance SonarQube's capabilities.
SonarQube Azure DevOps integration allows you to scan your code in real-time, providing instant feedback on code quality. This integration is available in Azure DevOps 2019 and later versions.
To get started with the integration, you need to install the SonarQube plugin in your Azure DevOps project. This plugin is available in the Azure DevOps Marketplace.
The SonarQube Azure DevOps plugin allows you to configure and run SonarQube scans directly from your Azure DevOps pipeline. You can also view SonarQube analysis results directly in your Azure DevOps project.
Integration with Azure DevOps
You can create a Sonar project from Azure DevOps repositories by importing the Azure DevOps pipeline into SonarQube. This helps developers deliver high-quality, efficient code standards that benefit the entire team or organization.
To set the configuration of Azure DevOps repositories, you need to setup global DevOps platform settings which is present in SonarQube. This involves adding a personal access token (PAT) to import repositories.
You can create a global DevOps platform settings by going to your SonarQube UI, clicking on Administration > Configuration > General Settings, and navigating to DevOps Platform Integrations, selecting the Azure DevOps tab, and clicking the Create configuration button.
The configuration involves providing a Configuration Name, Azure DevOps URL, and Personal Access Token (PAT). The Azure DevOps URL is the full Azure DevOps collection URL or organization URL, and the PAT is mandatory to use an Azure DevOps account with Administrator permission.
Here's a list of the required information for creating a configuration:
- Configuration Name: This name will be useful to identify the configuration.
- Azure DevOps URL: Provide your full Azure DevOps collection URL or organization URL.
- Personal Access Token (PAT): It’s mandatory to use an Azure DevOps account with Administrator permission.
Once the configuration is done, you can add a project from Azure DevOps to SonarQube by navigating to the home page of SonarQube and clicking on Add project button present in the top right corner. Select Azure DevOps from the dropdown and provide a PAT with Code (Read & Write) scope.
You can also install SonarQube into the machine by downloading the SonarQube community edition zip file from the official website and following the installation steps. After installation, you can access the SonarQube dashboard by hitting the URL: http://localhost:9000/.
Configuration and Settings
To set up your SonarQube Azure DevOps integration, you'll need to configure the DevOps platform settings.
You'll start by navigating to your SonarQube UI, then clicking on Administration > Configuration > General Settings. From there, you'll select the DevOps Platform Integrations tab and click the Create configuration button.
The first step in creating a configuration is to give it a name, which will help you identify it later. You can choose any name you like, but it's a good idea to make it something descriptive.
You'll also need to enter your Azure DevOps URL, which can be either your Azure DevOps Server collection URL or your Azure DevOps Services organization URL.
To complete the configuration, you'll need to create a Personal Access Token (PAT) from your Azure DevOps account. This token should have Administrator permission and scope authorized for Code > Read & Write for all the repositories you want to analyze.
Here's a summary of the required configuration details:
- Configuration Name: a descriptive name for the configuration
- Azure DevOps URL: your Azure DevOps Server collection URL or Azure DevOps Services organization URL
- Personal Access Token (PAT): a token with Administrator permission and scope authorized for Code > Read & Write for all the repositories
Quality Gates and Automation
You can update a quality gate in SonarQube to see that failing the quality gate fails the build. This is demonstrated in the task where you update the SonarQube Way quality gate to include a Coverage condition with a Warning value of 50 and an Error value of 30.
To automate quality gates, you can integrate SonarQube with Azure DevOps. This integration allows you to set up quality gates in SonarQube and have them trigger builds in Azure DevOps. You can also use the SonarQube Azure DevOps extension to help developers deliver high-quality code.
By automating quality gates, you can ensure that your code meets certain standards before it's deployed. For example, you can set up a quality gate that fails the build if the code coverage is below 50%. This helps catch issues early on and prevents them from making it to production.
Updating a Quality Gate
Updating a Quality Gate is a crucial step in ensuring the quality of your code. You can update a quality gate in SonarQube by clicking on the Quality Gates in the top toolbar.
To add a condition to the quality gate, click on the "Add Condition" dropdown at the bottom of the list of rules and select Coverage. The Warning value is set to 50 and the Error value is set to 30.
In VSTS, queue another MyShuttle2 build to see the updated quality gate in action. When the build completes, it should have a failed result due to the quality gate failing the build.
To change the quality gate to warn rather than fail, go back to SonarQube and edit the Quality Gate. Clear the Error value so that you only get a warning if the coverage < 50% instead of failing the build.
Here's a step-by-step summary of the changes made to the quality gate:
Queue a new build to see that now the build succeeds, but there is a Quality Gate warning.
DevOps Automation Linux Cloud
In DevOps automation, Linux plays a crucial role. You can automate various tasks using tools like Ansible and Terraform on Linux.
To start with DevOps automation on Linux, you'll need to install SonarQube, a tool for code analysis. You can download the SonarQube community edition zip file from the official website: https://www.sonarsource.com/products/sonarqube/downloads/.
Once you've downloaded and extracted the file, navigate to the directory and run the script to start SonarQube. For Windows VM, use bin\windows-x86-64\StartSonar.bat, while for Linux VM, use /bin/linux-x86-64/sonar.sh start.
After starting SonarQube, you can access its dashboard by visiting http://localhost:9000/. From there, you can configure Azure DevOps integration to set up continuous code analysis.
To integrate SonarQube with Azure DevOps, follow these steps:
- Click on Administration / AML Integrations / Azure DevOps Service
- Create a configuration by clicking on add project and selecting Azure DevOps
- Set up the branch and follow the instructions to create a service connection
- In your Azure DevOps project, add an empty job and then add SonarQube tasks to the pipeline
- Trigger the pipeline to run, and once it succeeds, you'll see the full code scan report in SonarQube.
Frequently Asked Questions
What is SonarQube and why is it used?
SonarQube is an open-source platform that analyzes code quality to detect bugs and improve coding standards. It's used for continuous inspection of code to ensure high-quality software development.
Sources
- https://www.devopsuniversity.org/how-to-integrate-sonarqube-with-azure-devops
- https://github.com/SonarSource/sonar-scanner-azdo
- https://blog.knoldus.com/how-to-integrate-sonarqube-with-azure-devops/
- https://azuredevopslabs.com/labs/java/sonarqube/
- https://www.linkedin.com/pulse/sonarqube-integration-azuredevops-pramod-rajput
Featured Images: pexels.com