If you are using Azure DevOps for building and deploying your .NET core applications, then you should consider the following.
- Azure Pipelines now supports composing both the build and release stages as code. You can now combine your CI and CD pipeline definitions into a single pipeline definition which lives within the same repository as the application code.
- Turn on binary logging in MSBuild so that you receive exhaustive structured logs from the build process. You can visualize these logs using MSBuild Structured Log Viewer to inspect the build process in great detail.
- Add Coverlet, which is a cross-platform code coverage framework to collect code coverage data, and generate neat code coverage reports with Report Generator.
To demonstrate the concepts that we discussed, we will build an integrated CI/CD Azure Pipeline which will build and deploy an Azure Function to Azure. Let's first briefly discuss the utilities that I mentioned above.
MSBuild Binary Log and Viewer
For most of us, the MSBuild process is a black box that chews source code and spits out application binaries. To address concerns with the opaqueness of build logs, MSBuild started supporting structured logging since version 15.3. Structured Logs are a complimentary feature to the file and the console loggers, which MSBuild has been supporting for a while now. You can enable binary logging on MSBuild by setting the /bl switch and optionally specifying the name of the log file, which is build.binlog in the following command.
$ msbuild.exe MySolution.sln /bl:build.binlog
The binary log file generated by executing the previous command is not human readable. The MSBuild Structured Log Viewer utility helps you visualize the logs and makes navigation through the logs a breeze.
Coverlet and Report Generator
Coverlet is a cross-platform tool written in .NET core that measures coverage of unit tests in .NET and .NET core projects. The easiest way to use Coverlet is to include the Coverlet NuGet package in your test projects and use MSBuild to execute the tests. You can use the following commands to perform the two operations.
$ dotnet add package coverlet.msbuild
$ dotnet test /p:CollectCoverage=true
Report Generator is a visualization tool that takes a code coverage file in a standard format such as Cobertura and Clover and converts it to other useful formats such as HTML. Due to its integration with Azure Pipelines, you will be able to see the code coverage results alongside build results in the same pane.
Integrated CI/CD Pipeline
Azure DevOps now supports writing Release pipelines as code. You can now compose a single definition that includes both the build and release stages and keep it in the source code. Shortly, we will build a YAML definition that includes both the build and release stages of our application.
The Sample Application
I created a simple Azure function named GetLoLz that returns a string with as many occurrences of the text “LoL“ as requested by setting the query parameter named count of the HTTP request as follows.
- [GET] http://<hostname>.<domain name>/api/GetLoLz?count=<int>
Let’s get started.
Source Code
You can download the source code of the sample application from my GitHub repository here. I used .NET Core 3.0, Visual Studio 2019, and Azure Function v2 to build the application.
The LoL Function
In Visual Studio (or VSCode), create a new Function app project named AzFunction.LoL. Create a new class file and add the following HTTP function to your project.
- [FunctionName(nameof(GetLoLz))]
- public static IActionResult GetLoLz(
- [HttpTrigger(AuthorizationLevel.Anonymous, "get")]
- HttpRequest request,
- ILogger log)
- {
- log.LogInformation("C# HTTP trigger function processed a request.");
- return int.TryParse(request.Query["count"], out var count)
- ? (ActionResult)new OkObjectResult($"{string.Join(string.Empty, Enumerable.Repeat("LoL.", count))}")
- : new BadRequestObjectResult("Please pass a count on the query string");
- }
Launch the application now and visit the following URL from your browser or send a GET request from an API client such as Postman. Since your application might be available at a different port, check the Azure Function Tool CLI console for the address of your HTTP function.
http://localhost:7071/api/GetLoLz?count=1000
Let’s now add a few tests for the function that you just created. Create a xUnit test project in your solution and add the coverlet.msbuild and coverlet.collector NuGet packages to it. Now, add a few tests to the project. For details, check out the AzFunction.LoL.Tests project in the GitHub repository of this application.
Provision Azure Resources
Let’s use the AZ CLI utility to create a Resource Group and an Azure Function App for our project. The following commands will create a Resource Group named FxDemo, a storage account named lolfxstorage, and an Azure Function named LoLFx. You can choose other names for your resources and substitute them in the commands below. Remember to use the command az login to log in before executing the following commands.
$ az group create -l australiaeast -n FxDemo
$ az storage account create -n lolfxstorage -g FxDemo -l australiaeast --sku Standard_LRS
$ az functionapp create --resource-group FxDemo --consumption-plan-location australiaeast --name LoLFx --storage-account lolfxstorage --runtime dotnet --os-type Linux
Log in to the Azure portal and verify the status of all the resources. If all of them are up and running, then you are good to go.
Composing CI/CD Pipeline - Build Stage
Azure Pipelines automatically picks the build and release definition from the azure-pipelines.yml file at the root of your repository. You can store your code in any popular version control system such as GitHub and use Azure Pipelines to build, test, and deploy the application. Use this guide to connect your version control system to Azure DevOps. We will now define our integrated build and release pipeline.
Create a file named azure-pipelines.yml at the root of the repository and add the following code to it.
- trigger:
- branches:
- include:
- - '*'
- stages:
- - stage: Build
- jobs:
- - job: Build_Linux
- pool:
- vmImage: ubuntu-18.04
- variables:
- buildConfiguration: Release
- steps:
- - template: .azure-pipelines/restore-packages.yml
- - template: .azure-pipelines/build.yml
- - template: .azure-pipelines/test.yml
- - template: .azure-pipelines/publish.yml
- - stage: Deploy
- jobs:
- - job: Deploy_Fx
- pool:
- vmImage: ubuntu-18.04
- variables:
- azureSubscription: LoLFxConnection
- appName: LolFx
- steps:
- - template: .azure-pipelines/download-artifacts.yml
- - template: .azure-pipelines/deploy.yml
I like to adopt the Single Responsibility Principle (SRP) everywhere in my application code. Azure Pipelines extends SRP to DevOps by giving you the flexibility to write the definition of individual steps that make up your pipeline in independent files. You can reference the files that make up the pipeline in the azure-pipelines.yml file to compose the complete pipeline.
You can see in the previous definition that this pipeline will be triggered whenever Azure Pipelines detects a commit on any branch of the repository. Next, we defined two stages in the pipeline named Build and Deploy that will kick-off to build and deploy the application respectively. We used the same type of agent (VM with Ubuntu image) to carry out the tasks of building and deploying our application. Even if you are using an XPlat framework such as .NET core, you should always use agents that have the same OS platform as your application servers. A common platform ensures that tests can verify the behavior of your application on the actual platform, and any platform-related issues get surfaced immediately.
As you can see that I have stored all the templates in a folder named .azure-pipelines at the root of the repository. Let’s go through the templates that we used for the build stage. The first step that will execute is restore-packages, which will restore the NuGet packages of the solution.
- steps:
- - task: DotNetCoreCLI@2
- displayName: Restore Nuget
- inputs:
- command: restore








Join the conversation! Your thoughts help the community grow.