A previous article guided you through setting up your repository. You did this by creating multiple branches that reflect the GitFlow model and protecting those branch use policies. Then, I gave you an overview of how pull requests work. This article - the final installment in the Azure series - will show you how to implement Azure’s continuous testing, integration, and deployment mechanisms.

How Azure can help your company expand in multiple regions

Continuous Integration

Continuous Integration (CI) is a practice that allows multiple developers to frequently merge code changes into a remote repository by automating the build, test, and run processes. To implement your own CI pipeline in Azure, follow these steps:

Continuous Testing

As mentioned before, the continuous integration pipeline can perform both static and dynamic tests automatically. Static testing is a technique to improve the quality of your application by detecting bugs, vulnerabilities, and code smells without executing your application’s code. Dynamic testing, on the other hand, focuses on the behavior of your application; it requires the execution of your application’s code.

This tutorial will teach you how to perform static testing using SonarCloud.

Install the SonarCube Extension

To be able to use the SonarCloud functionalities, you must install the SonarCloud extension in your organization using the following steps:

Link the Project to SonarCloud

It is now time to connect your Azure DevOps project to SonarCloud.

How Azure can help your company expand in multiple regions

Fill in the parameters for the service connection, allowing all pipelines to use this connection option.

How Azure can help your company expand in multiple regions

Click Verify and Save to create the connection.

Create a New Project on SonarCloud

Before you continue with the configuration of the CI pipeline, you must create a project on SonarCloud that will be linked to the repository in the next steps.

How Azure can help your company expand in multiple regions

Select your organization, check your project and click Set Up.

How Azure can help your company expand in multiple regions

Add Task into the Continuous Integration Pipeline

Now that you have successfully connected your SonarCloud account to your project, you can move forward and add the SonarCloud-specific tasks installed by the extension into your continuous integration pipeline.

How Azure can help your company expand in multiple regions

The order in which these tasks should be executed is,

  1. Prepare Analysis Configuration: configures the settings before executing the build
  2. Run Code Analysis: analyzes the source code
  3. Publish Quality Gate Result (optional): shows if the code meets the quality standards in the build summary

Let us start by adding a Prepare Analysis Configuration task before the build task. Here you must select the service connection, the organization, how the analysis is done (MSBuild in this tutorial because the example application was developed using the .NET 5 framework), the project key, and the project name.

How Azure can help your company expand in multiple regions

The project key is the values of the parameter ID in the URL of your SonarCloud project.

How Azure can help your company expand in multiple regions

How Azure can help your company expand in multiple regions

Lastly, add the Publish Build Artifact task.

How Azure can help your company expand in multiple regions

Save your pipeline.

The pipeline will look something like this,

trigger:
- main
pool:
  vmImage: ubuntu-latest
variables:
  buildConfiguration: 'Release'
steps:
- task: SonarCloudPrepare@1
  inputs:
    SonarCloud: 'SonarCloud'
    organization: 'gtrekter'
    scannerMode: 'MSBuild'
    projectKey: 'GTRekter_Training'
    projectName: 'Training'
- script: dotnet build --configuration $(buildConfiguration)
  displayName: 'dotnet build $(buildConfiguration)'
- task: SonarCloudAnalyze@1
- task: SonarCloudPublish@1
  inputs:
    pollingTimeoutSec: '300'
- task: DotNetCoreCLI@2
  inputs:
    command: 'publish'
    publishWebProjects: true
    zipAfterPublish: true
    arguments: '--configuration $(buildConfiguration) --output  $(Build.ArtifactStagingDirectory)'
- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

Continuous Deployment

Now you can create your continuous deployment (CD) pipeline. This pipeline will automatically deploy the changes to the app services you made based on the previous tutorial.

Create a Connection to Azure Resource Manager

To be able to create, update, and delete resources in your Azure account, you must create a connection to Azure Resource Manager. Azure Resource Manager is the deployment and management service for Azure. To create the connection, perform the following steps:

How Azure can help your company expand in multiple regions

Select your scope level, filling in the parameters for the service connection, and allowing all pipelines to use this connection option.

How Azure can help your company expand in multiple regions

Click Save to create the connection.

Create a Release Pipeline

How Azure can help your company expand in multiple regions

Select Build as the source type, then select the building pipeline from within the Source (Build Pipeline) dropdown.

How Azure can help your company expand in multiple regions

To enable deployment each time a new build is available, click the lightning icon in the top right-hand corner of your artifact. Enable the Continuous Deployment Trigger.

How Azure can help your company expand in multiple regions

Click on Add a Stage.

How Azure can help your company expand in multiple regions

How Azure can help your company expand in multiple regions

Select the service connection to the Azure Resource Manager that you have previously created; target the App Service. The example shows the deployment of the application in the App Service hosted in a server in the Central United States.

How Azure can help your company expand in multiple regions

How Azure can help your company expand in multiple regions

Change the App Service of the cloned stage to point to the App Service hosted on a server in Northern Europe.

How Azure can help your company expand in multiple regions

Click the Save button at the top right-hand side of the page.

Congratulations! You have finished setting up the proposed architecture.

Testing the Architecture

Now that the entire architecture is finally ready, you can test the entire workflow.

On your local machine, change your application’s response to an HTTP request at the root by pasting the following code into the Configure method of your Startup file.

app.UseEndpoints(endpoints =>
{
    endpoints.MapGet("/", async context =>
    {
        await context.Response.WriteAsync("How Azure can help your company expand in multiple regions - Part 5");
    });
});

Stage, commit and push the changes on the feature-amazingfeature branch to the remote repository.

$ git add .
$ git commit -m "Change the response"
[feature-amazingfeature 369b799] Change the response
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 384 bytes | 192.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Analyzing objects... (3/3) (20 ms)
remote: Storing packfile... done (109 ms)
remote: Storing index... done (116 ms)
To https://dev.azure.com/GTRekter/Training/_git/Training
   d7b19ea..369b799  feature-amazingfeature -> feature-amazingfeature

Navigate to your project on Azure DevOps, select the Pull Request option in the Repos tab, and click New Pull Request.

How Azure can help your company expand in multiple regions

Select the branch feature-amazingfeature as the source, the branch develop as the target, create a title, create a description, and click Create.

How Azure can help your company expand in multiple regions

Click approve. Then click Complete to conclude the pull request.

How Azure can help your company expand in multiple regions

To avoid hundreds of unused branches, check the Delete feature-amazingfeature After Merging option, then click Complete Merge.

How Azure can help your company expand in multiple regions

Repeat the same procedure for the other branches,

Once the merge into the main is complete, the build pipeline will start automatically.

How Azure can help your company expand in multiple regions

Once your build pipeline is completed, you can view the results of the analysis performed by SonarCloud by navigating to https://sonarcloud.io/ in your browser and selecting the appropriate SonarCloud project.

How Azure can help your company expand in multiple regions

If your pipeline successfully completes, your release pipeline will automatically start. This will deploy the application on the two App Services located in different regions.

How Azure can help your company expand in multiple regions

If you navigate to the URL defined in your Azure Front Door resource, your HTTP request will be forwarded to the geographically closest App Service in the backend pool. In my case, because I am currently in Philadelphia, Pennsylvania, Azure Front Door will forward my request to the App Service located in the Central United States.

How Azure can help your company expand in multiple regions