Build and Test Pipelines with Azure
When talking about software development, identifying errors in the delivering code and doing necessary tests to the code base is a crucial step to avoid disasters in the deployments. Because of that, most of the Continuous Integration and Continuous Delivery solutions have to provide some features to enable developers to create and run, Test and Build pipelines to their code. In this article, We are discussing adding Build and Test pipeline to your Azure DevOps pipeline and configure it to work with your Pull Requests.
So in here, I am talking about creating a pipeline with Azure DevOps with an Azure Git Repos. But you also can develop pipelines with Github Repos, Bitbucket Repos, Subversion and any other Git repo. The process will be the same as them.
So first you need an Azure DevOps account. You can create an Azure DevOps account for free and use it for your hobby projects because they provide some flexible package for free. You can view their free plan benefits by clicking this link below.
So also you can create a free account with Azure DevOps by the link below.
So after creating the account, you will ask to make a tenant and project. So I will not be going to explain it. You only need to follow the instructions. 😉
So let’s move on to the tedious part.
So for demonstrating our application, I have a sample Dotnet core application. It’s the sample application have generated with Visual Studio for Dotnet core Web API (Weather Forecast API as you know 😉)
For implement the Test part. I have written so simple unit test with the xuint and Moq.
So now we have everything to start our application. Now we need to create a repository in Azure DevOps. For that, you can navigate to the Repos Section in Azure DevOps or use the same repo created when the project started. To create a new Repo, click on the project name on the top bar just like the image below.
Then click on the +New Repository
Then
Fill this with necessary details and click Create button.
So It will create a new repo shortly. Now we need to add our sample application to the repo. I will not be going to explain it; also, it is the same procedure that follows to push the code into the Github.😉
Then we have a repo. So now let’s look into how to create the Test and Build pipeline.
Let’s navigate to the Pipelines section in the Azure DevOps.
And click on the Create Pipeline button.
Now you need to select which source you are going to add the repo to start the build and test.
So in here you can use these sources and connect it to the Azure Pipelines. I’m using the Azure Repos. So I’m clicking the first option.
Then select the Repository that you need to create the pipeline.
Then need to select the platform that you are using to build the application. In here, I have used the Dotnet Core. So I’m using the second option in this list. Then select the Repository that you need to create the pipeline.
Then it creates a template YAML(YAML Ain’t Markup Language) for the build and test pipelines. These all pipelines are written in data serialization language called Yaml. It is easy to read and maintain. In here click on Save and run. It will ask to push this file direct to the repo. So click on Save and Run.
You can see it is running our pipeline. So click on the Job.
You can see step by step our pipeline is running.
After a successful run, you can see this. If it fails, that green icon shows as red.
Now we need to change this YAML pipeline a little bit. Open the azure-pipeline.yml
on your editor. I’m preferred to use VS Code. Then replace this code with existing code. I will explain this one by one.
Now let’s we look into what is this YAML File means.
The first line in the YAML pipeline means the trigger event to the pipeline. In here it means the trigger the pipeline for every commit merge to the master.
Second-line variables mean we can set different variables to the pipeline to access various stages.
After this lines, it starts the pipeline tasks.
As first task, it installs the Dotnet core SDK to the environment.
Then it restore the NuGet packages mentioned in all the .csproj files (This means for both API and Test projects).
Then pipeline starts release type build sequences for both API and Test project.
Then it runs the unit tests inside the Test project.
Then it outputs the build artifacts that can be used for further deployments or if there are another release pipelines.
Now add this file also into your repo, and it will run the pipeline automatically because we have set the trigger point as the master.
After a successful run. It can be seen as like this.
Adding PR Build to the Pipeline
Add this code to the pipeline after the first two lines.
After the changes your pipeline can be seen as this.
This will allows triggering the build pipeline for every PR raised inside the repo. You can add restrictions to the branches adding branch names after “include” and “-” sign.
Installing Entity Framework inside the Pipeline
Simple add this code block after the Dotnet install task.
Also, Code quality and coverage calculations can be done in the Build/Test pipelines. I will explain it with a different article.
I hope this article will be helpful to you. Add your concerns as comments here. Goodbye and Stay safe until next.