Building a CI/CD pipeline with Azure DevOps

Continuous Integration and Continuous Delivery (CI/CD) helps to automate the development process to make it more efficient and predictable. CI/CD pipelines are an important part of modern software development lifecycle. Azure DevOps provides a complete set of tools to build, deploy and test software. In this tutorial, we will walk through the steps required to build a CI/CD pipeline using Azure DevOps.

Prerequisites

Before we begin the tutorial, we need to ensure that we have the following prerequisites:

  • An Azure DevOps account
  • A GitHub account
  • A sample application to build and deploy

Creating a New Project

To get started, we need to create a new project in Azure DevOps. If you don’t have a project already, follow these steps:

  1. Login to your Azure DevOps account
  2. Click on “Create Project”
  3. Provide a name for the project and select the version control system you want to use. For this tutorial, we will be using Git.

Setting up the Build Pipeline

The first step in creating a CI/CD pipeline is to setup the build pipeline. The build pipeline is responsible for building the application and creating an artifact that can be deployed. To setup the build pipeline, follow these steps:

  1. Click on “Pipelines” in the left menu and then click on “Builds”
  2. Click on “New Pipeline” and select the source where your application code is hosted. For this tutorial, we will be using GitHub.
  3. Select the repository where your application code is located and click on “Continue”
  4. Azure DevOps will analyze your code and suggest a template for your pipeline. For this tutorial, select “ASP.NET Core” template.
  5. Azure DevOps will create a YAML file for the pipeline.

This pipeline will build and publish the application to an artifact repository. We can customize this pipeline YAML file to add additional steps like running tests or performing code analysis.

Customizing the Build Pipeline

The YAML file for the build pipeline can be modified to fit specific needs. In this tutorial, we will add a step to run unit tests on the application after it is built.

  1. Click on “Edit” on the azure-pipelines.yml file generated by the pipeline creation wizard.
  2. Locate the “- task: DotNetCoreCLI@2” section in the file
  3. Add the following section after it:
  - task: DotNetCoreCLI@2
    displayName: Test
    inputs:
      command: test
      projects: '**/*Tests.csproj'

This section will run the unit tests in the application after it is built.

Setting up the Release Pipeline

The next step is to create the release pipeline. The release pipeline is responsible for deploying the application to a specific environment. To set up the release pipeline, follow the below steps:

  1. Click on “Releases” in the left

menu and then click on “New Pipeline”
2. Select the “Empty job” template
3. Click on “Add an artifact” and select the artifact that was generated by the build pipeline
4. Click on “Add a stage” and provide a name for the stage. For this tutorial, we will create a “Dev” environment
5. Drag and drop a “Deploy Azure App Service” task to the stage
6. Click on “Azure subscription” and select the subscription where the app service that will host this application is running
7. Click on “App Service name” and select the app service where the application will be deployed
8. Save the release pipeline.

Customizing the Release Pipeline

The YAML file for the release pipeline can also be modified to fit specific needs. In this tutorial, we will add a step to deploy the application only if all the tests pass.

  1. Click on “Edit” on the azure-pipelines.yml file generated by the pipeline creation wizard.
  2. Add the following section after the “steps:” section:
- task: VSTest@2
  displayName: Test Assemblies
  inputs:
    testAssemblyVer2: |
      **$(BuildConfiguration)*Tests*.dll
      !**obj**
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

This section will run all the tests in the application before it is deployed.

Triggering the Pipeline on Code Changes

The pipeline can be triggered on code changes to ensure that the application is built and deployed whenever there are changes made to the code. To trigger the pipeline on code changes, follow these steps:

  1. Click on “Pipelines” in the left menu and then click on “Builds”
  2. Click on the “…” next to the pipeline name and select “Edit”
  3. Click on the “Triggers” tab and enable “Continuous integration”.
  4. Save the pipeline.

Conclusion

In this tutorial, we have seen how to set up a CI/CD pipeline using Azure DevOps. We started by creating a new project and creating the build pipeline. We then customized the pipeline to run unit tests. Next, we created a release pipeline and customized it to run tests before deploying the application. Finally, we set up the pipeline to trigger on code changes to ensure that the application is always up-to-date. Azure DevOps provides a powerful set of tools to automate the development process and make it more efficient.

Related Post