AWS/Azure/OpenShift/Kubernetes Version Control System (VSC)

How to build your first CI/CD Pipeline in Azure DevOps using ASP.Net Core Application

Azure-CI-CD-Pipeline
Azure DevOps CI/CD

In times past, software releases were once scheduled in terms of weeks or even years before they’re released to production.  With the transformation agenda of DevOps, it is easier for software teams to release features in smaller batches that are often scheduled in days or weeks. Some teams even deliver software updates continuously and sometimes with multiple releases within the same day. Before I delve into Azure DevOps CI/CD Pipeline, let me briefly look at what DevOps itself is all about.

What is DevOps? The DevOps concept has been around for a while now with many particularly those from non-tech backgrounds not really understanding the idea behind it.  The concept is the combination of "Dev" which means "Development" and "Ops" meaning  "Operations".  Any software development process must involve people, goes through a process to produce a software product that will be delivered to end-users continuously.  Therefore:

DevOps is the union of people, processes, and products to enable continuous delivery of value to our end users.

It refers to replacing siloed Development and Operations to create multidisciplinary teams that now work together with shared and efficient practices and tools. Essential DevOps practices include agile planning, continuous integration, continuous delivery, and monitoring of applications. DevOps is a continuous journey.

Having looked at what DevOps is, let me now take a look at Azure DevOps.

Azure-DevOps-Image
Azure DevOps

Azure DevOps is a Software as a service (SaaS) platform from Microsoft that provides an end-to-end DevOps toolchain for developing and deploying software. It also integrates with most leading tools on the market and is a great option for orchestrating a DevOps toolchain.

What can Azure DevOps do?

Azure DevOps comprises a range of services covering the full development life-cycle.

  • Azure Boards: agile planning, work item tracking, visualization, and reporting tool.
  • Azure Pipelines: a language, platform, and cloud-agnostic CI/CD platform with support for containers or Kubernetes
  • Azure Repos: provides cloud-hosted private git repos.
  • Azure Artifacts: provides integrated package management with support for Maven, npm, Python and NuGet package feeds from public or private sources.
  • Azure Test Plans: provides an integrated planned and exploratory testing solution.
  • Azure DevOps can also be used to orchestrate third-party tools.

Next is to look at what Azure DevOps CI/CD Pipeline is all about.
Azure Pipelines is used to automatically build and test your software projects to make them available to others. It works with just about any language (ASP.Net Core, Python, Java, name it) or project type. Azure Pipelines combines continuous integration (CI) and continuous delivery (CD) to test and build your code and push it to another level in the Software Development Life-Cycle (SDLC). Continuous Integration (CI) is the practice used by development teams in automating, merging, and testing code. Implementing CI helps to catch bugs early in the development cycle, which makes them less expensive to fix.

The word pipeline may sound so unfamiliar, but it is not too unfamiliar if you’re in the DevOps field. In a simple context, Pipeline is a runnable specification of steps that reduce developers’ manual work by delivering a new version of software productively to save time.

I am building my Azure CI/CD Pipeline from a simple static web code of ASP.Net Core project initiated within my local system. Building Azure CI/CD Pipeline is not only limited to an ASP.Net Core project, you can also build it from a Docker Image or any other language with your code being hosted remotely on GitHub.

Prerequisites for creating Azure CI/CD Pipeline

To create Azure CI Pipeline on Azure DevOps, the following things must be carried out:

  • An Azure subscription. You can get one free through Visual Studio Dev Essentials.
  • After setting up your subscription, visit the Azure DevOps portal to create your first Azure DevOps organization. The steps to create Azure DevOps organizations is in the next session of this article.
  • You must create Azure DevOps Project
  • Access to a GitHub or external Git repo that contains .NET, Java, PHP, Node.js, Python, or static web code.

Some important concepts to know about when it comes to Azure DevOps:

  • Azure DevOps Organization – An Organization in Azure DevOps is a tool for organizing and connecting groups of related projects. Learn more about Azure DevOps Organization
  • Azure DevOps Project – Azure DevOps Project helps in simplifying the setup of an entire continuous integration (CI) and continuous delivery (CD) pipeline to Azure with Azure DevOps. It provides a repository for source code and a place for users to plan, track progress, and collaborate on building software solutions. A project represents a fundamental container where data is stored when added to Azure DevOps. Azure DevOps project can either be public or private. It is public when it’s open to every members of the organization and it is private when access is only provided to a certain members of a project team within the organization.

What we get with free Azure DevOps Organization:

  • Five free users with Basic access
  • Unlimited Visual Studio subscribers and Stakeholders
  • Free monthly amounts of additional services such as build and deployment 

Prerequisite for creating an Azure DevOps organization or project collection:

  • Read and understand how to Plan your organizational structure.
  • Setup an Azure AD. Without Azure AD you’re solely responsible for controlling organization access.
  • Organization names must start with a letter or number, followed by letters, numbers or hyphens, and must end with a letter or number.

Prerequisite for creating Azure DevOps Project:

  • You need an organization before you can create a project.
  • You must be a member of the Project Collection Administrators group or have the Create new projects permission set to Allow.

Steps to create Azure DevOps Organization

  • Sign in to Azure DevOps
  • Select New organization
  • Confirm information, and then select Continue
  • Congratulations, you’re now an organization owner!
  • Sign in to your organization at any   time through the organization unique URL which will appear like this https://dev.azure.com/{yourorganization}.
Azure-DevOps-Interface
Azure DevOps Organization

As you can see from the screenshot above I have already had an existing organization and the name of my organization is “imohetuk”. Therefore, my organization’s URL will be https://dev.azure.com/imohetuk

Creating Azure DevOps Project

  • Select your desired Azure DevOps organization to open the project creation page
  • Select New project
  • Enter information into the form provided and decide if you want to make your project public or private. Under the “Advanced” tab, select either Git or Team Foundation type of version control).
  • Click Create. The welcome page appears
Project-creation-interface
Azure DevOps Project

In the screenshot shown above, I named my project “demoproject”. I made it a private project and selected Git as my preferred Version Control.

Adding Users to Azure DevOps Project
– You can add users to a project or team, add projects to organizations, and add teams to projects

  • You must have an organization and project
  • To add users to an organization, you must be a member of the Project Collection Administrators group
  • To add users to a team, you must be a Team Administrator, or you must be a member of one of the administrative groups. Select Create.

Building your First Azure CI/CD Pipeline on Azure DevOps

Having created my project and set to build my first Azure CI/CD Pipeline on Azure DevOps, I will like to show you how the Azure DevOps Repository looks like before pushing the code project into it. See the screenshot below:

Cloning-a-project
Azure DevOps Repo

From the above, Azure DevOps provides you with several options on how you can get your code into your Azure DevOps repository.

  • The first option is if you want to clone an already existing code into Visual Studio Code (VSCode) IDE and start working on your project and then push it back to Azure repo.
  • While the second option is if you have an existing repository in your command line just as the method I am using here you can easily push it to the remote origin from your local system and have the code stored on Azure Repo. Or
  • If you want to import your code hosted remotely in GitHub you can use the “Import” option. Azure DevOps is so flexible and friendly that there is hardly anything you can’t do with it when it comes to managing your software code.

It’s time to get to work!

As stated in the previous section, I built my Azure CI/CD Pipeline from my local system using an ASP.Net Core Project. To follow along, create a folder on the desktop of your local system name it whatever name you desired. From the screenshot below, I named mine “Azure DevOps DemoProject”. As you can see the folder is empty.

Local-project-folder-1
Local Project Folder

Now, let’s get started by running the.Net code project.

Launch the Command Prompt in your local system. To do this, open the project folder and type “cmd”. See the below screenshot and type cmd where the arrow is directly pointing to.

Local-project-folder-2

Typing cmd will open up the command prompt interface within the project folder as shown below:

CMD2

Proceed to run the code below to create the ASP.Net Core project as shown in the screenshot below:

dotnet new mvc
Project-created-in-CMD
ASP.Net Core Project

From the screenshot above, you can see that the project files have been created successfully. Now, you will need to initialize the local repository by running the git init command in your command prompt.

Having initialized the code project, the next thing to do is to add all the files to my repo by running the git add –all command. See the below screenshot to see that all the files have been added.

Files-added
Project files Added

The next thing is to commit all the project files. I do this by running the git commit -m followed by a commit message. Having committed the files, it’s time to push it to the remote origin by running the git remote add origin followed by the Azure Repo URL. Head to the Azure DevOps project and locate the Repo tab and copy the repo URL and paste the same in your command prompt. Doing this will automatically issue the git push -u origin --allcommand, just press the Enter key on your keyboard to push the code to the Azure repo.

Code-pushed-to-azure-devops
Code pushed to Azure Repo

From the above screenshot, all the project files have been successfully pushed to the master branch of the Azure Repository. After pushing the codes, head to the Azure DevOps Repository to inspect if your codes have been pushed there. Mine has been pushed, see the screenshot below:

Set-up-build-1
Confirmation of your codes in Azure Repo

The screenshot above is not only confirming to us that the codes have been pushed to Azure Repo, but it also shows that we can set up the build pipeline directly from the Azure repo but in this case, I am going to set up my build pipeline using the Pipeline tab. Now click on the Pipeline tab and click on “Create Pipeline”

Pipeline-button-in-pipeline
Creating Azure Pipeline

The next phase is for you to locate the appropriate location that you have your code hosted. Azure DevOps will ask you boldly ” Where is your code?” See the screenshot below:

Where-is-your-code-screen
Locate your code

I have my code already pushed to Azure Repos hence, why I am selecting the first option. Your code could be in other locations so please go ahead and select the right location. Now is the time to select my type of project. Of course, my project is an ASP.Net Core so I’m going to jump and select it, yours could be different.

ASP-dotnet-app
Selection of project type

I will configure my Pipeline. I have done that through the screenshot below:

Pipeline-is-being-configured
Pipeline configuration screen

This stage will take me to my Pipeline review stage as shown in the screenshot below:

YAML-File-steps
Pipeline review screen

From the screenshot above, take your time to study the YAML file. In my YAML file, the comments are well stated in green color and the build will trigger from the master branch. The pool option simply identifies the type of Virtual Machine image which is ubuntu-latest, the lastest added to it means that the vmImage is up to date. The build variable is marked as “Release” which defines the stage where the pipeline is delivered to the repository. The last steps are what will be run after all other steps have been run and completed. From this stage, go ahead and click on the “save and run” button. Doing so will take you the screenshot shown below:

Commit-message
Adding a commit message

I supplied my commit message as “Set up CI with Azure Pipelines. I left the optional extended description column blank and committed it directly to the master branch and click on save and run. My Pipeline is on queue getting ready to start running and give me a successful email notification.

Build-is-in-queue
Build Pipeline in queue

The next screenshot shows the build status has changed from the queue status to the running status. Take a look at it;

Build-status-changed-to-running
Build Pipeline Running

Wow! the build status finally changed to “success”. See the screenshot below:

Build-success
Build Pipeline is successful

I will go to the Repo section to confirm that my build is successful. Do the same thing so we will be on the same page. See my screen below:

Comfirming-the-build-pipeline-in-the-repo-section-1
Build Pipeline success status confirmed

The next thing to do is to pull the YAML file to the local repository in our local system. Run the git pull command on your command prompt.

YAML-File-added-to-the-local-repo
YAML file added to the local repo

The YAML file has been pulled to the local repo in the local machine. There is no limit to the number of times you can run your build Pipeline from your code.

How to fix a bug in a failed build Pipeline – Let’s say you’re working in a real production environment and your build pipeline failed to run. What will you do, give up on it? Of course, not! Now run code . within the command prompt in your local machine to open up VSCode. See the screenshot below:

Files-opened-with-VSCode
Project files opened in VSCode

Now take time to observe the YAML files to see that it’s the same as what you have in Azure Repo. See the screen below:

YAML-File-present-in-VSCode
YAML File

Now make any erroneous alteration to any code within the project files and commit the same to the Azure Repo and watch the pipeline failed to build. The CI Pipeline is meant to run automatically whenever any commit is made to the code. I am altering the HomeController.cs files by deleting one of the curly braces. See the screenshot below:

Curly-bracket-deleted
Code altered

Now I’ll add a commit message “Updated controller” and push it to the Azure Repo to automatically build my pipeline.

Marked-failed
Build Pipeline failed

The above screen shows that the pipeline has failed to build and run successfully because of the error.

The next screenshot shows us what the problem is. The error can be fixed by clicking on the “troubleshooting failed runs” button as indicated on the screen.

Failed-build-status-1
Build Pipeline Failed Status

Now is the time to troubleshoot the error. To so do, click on the failed pipeline to locate the error. Azure Pipeline has indicated the error by pointing to the curly brace that was deleted from the below screen.

Failed-stage
Error troubleshooting screen

Now, I will return to my VSCode screen to fix the error, commit and push it back to the Azure Repo. See the screen below:

Fixed-Error
Fixed error

The commit message which will serve as the title of the build pipeline is “Fixed error”.

Fixed-error-succeeded
Build succeeded

As you can see from the screenshot, the error has been fixed the build is successful.

line24-azure-devops
side-by-side view of the error and fix

The above screen simply compares the error and the fix side-by-side to give you more insights into how it is easy to fix any error in Azure Pipelines.

The last thing I will do is to view our project on a web browser. Run dotnet run command in your command prompt or your VSCode terminal within the project directory to view the web page of your build project.

App-URL
App URL

From the screenshot, the URL to access the app on a web browser is provided as http://localhost:5000, where 5000 is the port number that the application will run through in my localhost. Running this http://localhost:5000, gives me the web page below:

First-web-page
App web page

Cleaning up your Azure DevOps Environment – This is very essential as it will help to control spending on your Azure Subscription. To do so, follow these steps:

  • From project settings, scroll to the bottom of the page
  • Locate the delete button in “Red
  • You will be asked to type the name of the project you wish to delete, supply the project name
  • Click delete and you’re done.

Conclusion: Congratulations!! You have successfully build your first Azure CI/CD Pipeline on Azure DevOps and learned about setting up and configuring Pipeline with a sample ASP.NET Core Azure DevOps project as well as fixing the bug in your build Pipeline.
Azure DevOps helps organizations to plan smarter, collaborate faster, and ship better within the set of development services. The automated Pipeline setup also provides the test, build, and deployment of the application in an easy manner. It is a good collaboration tool to get started with!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x