Scan a Git Repo in an Azure Pipeline with SonarCloud

Saturday, March 11, 2023

Scan a Git Repo in an Azure Pipeline with SonarCloud

#azure-devops #azure-pipelines #git #sonarcloud-analysis #sonarcloud-scan-code

This article is published at GitHub.You can raise issues, create pull requests or even fork the content...its open source.

In this article, you will learn how to scan a Git repo in an Azure Pipeline with SonarCloud.

Prerequisites

The following prerequisites will be required to complete this tutorial:

Create a Personal Access Token

  1. In Azure DevOps, select User Settings, Personal access tokens in the top right of the screen.

    Azure DevOps User Settings and Personal Access Token Navigation

  2. Select + New Token.

    Azure DevOps Personal Access Tokens + New Token

  3. Enter the following values.

    ParameterValue
    NameSonarCloud
    OrganizationYour Organization
    ScopesCustom defined
    CodeRead & Write

    Azure DevOps SonarCloud Personal Access Token Values

  1. Login to SonarCloud.

  2. Select Import project from Azure, and then enter the following values, and then select Continue.

    ParameterValue
    Azure DevOps organization nameYour Organization name
    Personal Access Token (PAT)PAT created earlier

    SonarCloud Create Organization

  3. Under Import Organization details, enter a Name and Key which will be used to identify your organization in Azure Pipelines, and then select Continue.

    SonarCloud Create Organization Import Organization

  4. Select Free plan and then Create Organization.

    SonarCloud Create Organization Choose Free Plan

Analyze an Azure DevOps project in SonarCloud

  1. In the root of a SonarCloud Organization, select Projects, and then Analyze a new project.

    SonarCloud Analyze a New Project

  2. Select the Project, and then Set Up.

    SonarCloud Select a Repo to Analyze

  3. Select the analysis method With Azure DevOps Pipelines.

    SonarCloud Analyze with Azure DevOps Pipelines

You will now be presented with instructions to setup the analysis which will be outlined in the following sections. Keep the instructions open as you will need information from them.

Add SonarCloud Extension to Azure DevOps

  1. In the root of an Azure DevOps Organization, select Organization Settings in the bottom left of the screen.

    Azure DevOps Organization Settings

  2. In the General section, select Extensions.

    Azure DevOps Organization Settings General Extensions Navigation

  3. Select Browse marketplace.

    Azure DevOps Extensions Navigation Bar

  4. Search for SonarCloud, and then select SonarCloud.

    Azure DevOps Search SonarCloud

  5. Select Get if free, and then select Install.

    Azure DevOps SonarCloud Extension Get It Free

  6. In the Extensions section, SonarCloud is now installed.

    Azure DevOps SonarCloud Extension Installed

Add SonarCloud Service Connection to an Azure DevOps Project

  1. From the root of an Azure DevOps Project, select Project settings in the bottom left of the screen.

    Azure DevOps Projects Settings Link

  2. In the Pipelines section, select Service connections.

    Azure DevOps Project Settings Service Connections Navigation

  3. Select Create service connection.

    Azure DevOps Project Create Service Connection

  4. Search for SonarCloud, then select Next.

    Azure DevOps Service Connection Search SonarCloud

  5. Enter the following values, and then select Verify and Save.

    ParameterValue
    SonarCloud TokenYour SonarCloud Token see below for instructions
    Service connection nameSonarCloud
    Grant access permission to all pipelinesTrue

    Azure DevOps SonarCloud Service Connection Setup

    Copy and paste the SonarCloud Token from the instructions Use this token under the Add a new SonarCloud Service Endpoint section into Azure DevOps Service Connection Setup SonarCloud Token text box. SonarCloud Token

Configure Azure DevOps Pipeline with SonarCloud Code Analysis

  1. In the root of an Azure DevOps Project, select Pipelines.

    Azure DevOps Pipelines Navigation

  2. Select Create Pipeline.

    Azure DevOps Pipelines Create Your First Pipeline

  3. In the Connect tab, Select Azure Repos Git.

    Azure DevOps Pipelines Connect Azure Repos Git

  4. In the Select tab, select ASP.NET Core YAML template.

    Azure DevOps Pipelines Select ASP.NET Core YAML template

  5. Go to the SonarCloud instructions, select .NET build, under the Configure Azure Pipeline section.

    SonarCloud Instructions Select .NET Build

  6. Replace the Contents of the YAML file with the following YAML replacing {Your...} with your organization, project key and project name from the SonarCloud instructions.

    trigger:
    - master
    
    pool:
    vmImage: ubuntu-latest
    
    variables:
    twogsdevProjectVersion: '1.0.0-$(Build.BuildNumber)'
    
    steps:
    - task: SonarCloudPrepare@1
        inputs:
        SonarCloud: 'SonarCloud'
        organization: 'twogsdev'
        scannerMode: 'MSBuild'
        projectKey: 'twogsdev_CoreDeployTest'
        projectName: 'CoreDeployTest'
        projectVersion: '$(twogsdevProjectVersion)'
    - task: DotNetCoreCLI@2
        inputs:
        command: 'build'
    - task: SonarCloudAnalyze@1
    - task: SonarCloudPublish@1
        inputs:
        pollingTimeoutSec: '300'
    
  7. Rename the pipelines YAML file to sonarcloud-az-pipeline.yml and then select Save and run.

    Azure DevOps Pipelines Rename the Pipelines Yaml File, Save and Run

  8. Add a commit message and then select Save and run.

    Azure DevOps Pipelines Connect Azure Repos Git

  9. Once the build has completed, in the Build Summary, select Extensions, and then Detailed SonarCloud Report.

    Azure DevOps Pipelines SonarCloud Report

In order to see the Quality Gate result in Azure DevOps Pipelines. A Code Definition needs configuring in SonarCloud. Follow the next steps to set this up.

Setup Quality Gate in SonarCloud

  1. In the SonarCloud Project Overview, select Set New Code definition.

    SonarCloud Report Quality Gate Set New Code Definition

  2. Select Previous Version, this corresponds to the projectVersion added to the pipeline YAML earlier.

    SonarCloud Report Quality Gate Set New Code Definition

  3. Once configured, in Azure DevOps Pipelines, select Run new. Once the Azure Pipeline has finished building and analyzing, select Detailed SonarCloud Report.

    Azure DevOps Pipelines Run New Job

  4. In SonarCloud select See Full Analysis.

    SonarCloud Report Quality Gate See Full Analysis

  5. The Detailed Report will be opened.

    SonarCloud Detailed Report

Analyze SonarCloud Report

  1. In the SonarCloud Dashboard, select the Your Project > See Full Analysis, then Overall Code.

    SonarCloud Detailed Report OverallCode Navigation

  2. Under Reliability, you will see a bug has been found. Select the Number next to Bugs.

    SonarCloud Detailed Report Overall Code Navigation

  3. The details of the Bug will be opened.

    SonarCloud Detailed Report Bug Details

  4. Select the Bug to see the code.

    SonarCloud Bug Code

Got a comment?

All my articles are written and managed as Markdown files on GitHub.

Please add an issue or submit a pull request if something is not right on this article or you have a comment.

If you'd like to simply say "thanks", then please send me a so the rest of Twitter can see how awesome my work is.

An unhandled error has occurred. Reload