Introduction

As we all know, it’s very important for developers to write better and maintainable code, so checking or scanning code for vulnerabilities is a very important task as it improves code quality and reduces overall maintenance costs when implemented as part of a continuous build and deploy process.
In this article, I will explain about a tool called SonarCloud (entirely free for open source projects) using which you can highlight the issues, bugs etc. in your code and can fix leaks and therefore improve code quality systematically. Also, you will see how you can use this tool with Azure DevOps Pipeline in continuous build process.
Let’s get started!
For using SonarCloud in Azure DevOps Pipeline, you need to implement the below steps in the given order,

Install the SonarCloud Marketplace extension

Create a SonarCloud service connection

Azure pipeline tasks require access to SonarCloud, so you need to create a service connection from the Microsoft Azure DevOps portal. A service connection provides secure access to an external service, such as SonarCloud.
Scan SPFx Code For Vulnerabilities Using SonarCloud In Azure DevOps
NOTE
Azure DevOps performs a test connection to verify that it can connect to Sonar Cloud.
Now the question is what is “SonarCloud Token” and how to generate it?

Add variables to Azure Pipelines

Let's add a few variables to the pipeline.
These variables will be used while configuring the settings for SonarCloud tasks in Azure DevOps Pipeline.

Add scan tasks to your Azure Pipelines configuration

SonarCloud Marketplace extension provides built-in task types that performs the scan.
You can add three different type of tasks in Azure DevOps pipeline to scan the code. These 3 tasks are,

Analyze the results on SonarCloud

Once your pipeline runs successfully, analyze the results by navigating to the SonarCloud portal, refresh the page, and then examine the results. Below is the screen shot showing analysis of one the pipeline run using SonarCloud extension,
Scan SPFx Code For Vulnerabilities Using SonarCloud In Azure DevOps
Important NOTE
There is one more freely available extension which you can use from Marketplace for scanning your code with Azure DevOps called – SonarQube. This extension also provides continuous inspection of your code quality and hence empowers the development teams.

Summary

In this article, I explained about the SonarCloud extension which can be used in Azure DevOps pipeline to scan issues/bugs and other vulnerabilities in your code and can give you results before the build process which could help in improving the overall quality of your code.