NashTech Blog

Automating Infrastructure with Azure DevOps: CI/CD Pipelines for .NET Applications

Table of Contents

Introduction

In modern software development, efficiency and agility are key. Continuous Integration (CI) and Continuous Deployment (CD) are methodologies that automate the process of integrating code changes and deploying applications, ensuring faster and more reliable delivery.

Azure DevOps is a comprehensive platform from Microsoft that facilitates CI/CD for applications, including those built with .NET. It integrates various tools to streamline the development lifecycle, including source control, build automation, testing, and deployment.

CI/CD Pipelines Overview

  • Continuous Integration (CI) involves regularly merging code changes into a shared repository. Automated builds and tests are triggered to catch issues early, ensuring that new code integrates smoothly with the existing codebase.
  • Continuous Deployment (CD) extends CI by automating the release process. Once code passes build and test stages, it is automatically deployed to production or other environments, reducing manual intervention and accelerating delivery.

Azure DevOps for .NET Applications

  1. Source Control: Azure Repos provides Git or TFVC for managing your code. Commits to the repository can trigger automated pipelines.
  2. Build Automation: Azure Pipelines automates the build process. It compiles the .NET application, restores dependencies, and runs unit tests. Successful builds produce artifacts ready for deployment.
  3. Testing: Automated testing, including unit and integration tests, ensures code quality. Azure Pipelines integrates with various testing frameworks to validate functionality.
  4. Deployment: Azure Pipelines also automates deployment to environments like Azure App Service or virtual machines. Deployments are triggered automatically once builds pass all tests.
  5. Monitoring and Feedback: Azure DevOps provides tools like Application Insights for monitoring application performance and dashboards for tracking pipeline metrics.

Steps for Implementing CI/CD with Azure DevOps

  1. Creating a New Project in Azure DevOps
  2. Setting Up a Git Repository
  3. Configuring a Build Pipeline
  4. Setting Up a Release Pipeline
  5. Deploying to Azure App Service
  6. Testing and Validation

1: Creating a New Project in Azure DevOps

  1. Log in to your Azure DevOps account.
  2. Create a new organization if you don’t have one.
  3. Inside your organization, click on “New Project”.
  4. Enter a Project Name, select Visibility (Private or Public), and click “Create”.

2: Setting Up a Git Repository

  1. Navigate to your newly created project.
  2. Go to the “Repos” section in the sidebar.
  3. Click “Initialize” to create a new repository.
  4. Clone the repository to your local machine using the URL provided.
git clone https://dev.azure.com/{organization}/{project}/_git/{repo} 

Add your .NET application code to this repository and push it.

cd {repo}
git add .
git commit -m "Initial commit"
git push origin main 

3: Configuring a Build Pipeline

  1. Navigate to the “Pipelines” section in your Azure DevOps project.
  2. Click on “New Pipeline”.
  3. Choose “Azure Repos Git” as the source and select your repository.
  4. Choose “YAML” for the pipeline configuration.
  5. You’ll be prompted to create a azure-pipelines.yml file. Here’s a sample configuration for a .NET Core application:
trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: UseDotNet@2
  inputs:
    packageType: 'sdk'
    version: '6.x' # Update this based on your .NET version
    installationPath: $(Agent.ToolsDirectory)/dotnet

- task: DotNetCoreCLI@2
  inputs:
    command: 'restore'
    projects: '**/*.csproj'

- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    projects: '**/*.csproj'
    arguments: '--configuration Release'

- task: DotNetCoreCLI@2
  inputs:
    command: 'test'
    projects: '**/*.csproj' 

Save and run the pipeline. This will build your .NET application and run tests.

4: Setting Up a Release Pipeline

  1. Go to the “Pipelines” section and click on “Releases”.
  2. Click “New Pipeline”.
  3. Select “Empty Job” to start with a blank template.

Define Stages

  1. Click on the “Add” button under “Stages”.
  2. Choose “Azure App Service Deployment” if you’re deploying to an Azure App Service.

Configure the Stage

  1. Name your stage and click “Tasks”.
  2. Add the “Azure App Service Deploy” task.
- task: AzureRmWebAppDeployment@4
  inputs:
    azureSubscription: '<Your Azure Service Connection>'
    appType: 'webApp'
    WebAppName: '<Your Web App Name>'
    packageForLinux: '$(System.DefaultWorkingDirectory)/**/*.zip' 
  1. Configure the Azure subscription. You need to authorize Azure DevOps to deploy to your Azure account by setting up a service connection.

Configure Artifacts

  1. In the “Artifacts” section, click “Add”.
  2. Select “Build” and choose the build pipeline you created earlier.

5: Deploying to Azure App Service

  1. Go to Azure Portal and create an Azure App Service if you don’t have one.
  2. Once created, go back to your Azure DevOps pipeline and configure the deployment details:
    • Subscription: Select your Azure subscription.
    • App Service name: Enter the name of your Azure App Service.
    • Package: Select the artifact produced by your build pipeline.

6: Testing and Validation

  1. Run the release pipeline manually to deploy your application.
  2. Monitor the deployment status in Azure DevOps to ensure everything is working correctly.
  3. Access your Azure App Service URL to verify that your application is running as expected.

Conclusion

Setting up CI/CD pipelines with Azure DevOps for your .NET applications automates your build and deployment processes, ensuring faster and more reliable releases. By following the steps outlined above, you can efficiently manage your deployment pipelines and maintain high-quality software delivery practices.

Picture of akshaychirde

akshaychirde

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

Scroll to Top