Deploying ASP.NET MVC Application to Azure Cloud Using Azure DevOps from GitHub

Introduction

In this article, we will explore the process of deploying an ASP.NET MVC application to Azure Cloud using Azure DevOps and GitHub. This approach ensures a seamless continuous integration and continuous deployment (CI/CD) pipeline, facilitating an efficient and robust development workflow. By the end of this chapter, you will have a comprehensive understanding of setting up an end-to-end deployment pipeline from GitHub to Azure using Azure DevOps Classic Editor.

Step 1. Prerequisites

Before we dive into the steps, let's ensure you have the necessary prerequisites.

  1. Azure Subscription: An active Azure subscription is required to deploy your application.
  2. Azure DevOps Account: You need an Azure DevOps account to create and manage the CI/CD pipeline.
  3. GitHub Repository: Your ASP.NET MVC application should be hosted on GitHub.
  4. Basic Knowledge of ASP.NET MVC: Familiarity with ASP.NET MVC will help you follow along with the steps.
  5. Azure App Service: Set up an Azure App Service where your application will be deployed.

Step 2. Creating an Azure App Service

Azure App Service is a fully managed platform for building, deploying, and scaling web apps. Here's how to create one:

  1. Log in to Azure Portal: Navigate to Azure Portal.
  2. Create a New Resource: Click on "Create a resource" in the left-hand menu and select "Web App" under the "Compute" section.
  3. Configure Web App
    1. Subscription: Select your Azure subscription.
    2. Resource Group: Create a new resource group or use an existing one.
    3. Name: Provide a unique name for your web app, which will be part of its URL (e.g., yourappname.azurewebsites.net).
    4. Publish: Choose "Code" since you are deploying an ASP.NET MVC application.
    5. Runtime stack: Select ".NET Core" and the appropriate version for your application.
    6. Operating System: Choose "Windows".
    7. Region: Select a region close to your user base to reduce latency.
    8. App Service Plan: Create a new plan or select an existing one. The app service plan determines pricing and available features.
  4. Review and Create: Click "Review + Create" to review your settings and then "Create" to create the web app.

Create the Azure Web app and fill in all the above details.

Azure Web

Fill in the details and then click next.

 Details

Web App

Step 3. Setting Up an Azure DevOps Project

Azure DevOps is a comprehensive suite of development tools that facilitate CI/CD. Follow these steps to set up your project:

  1. Log in to Azure DevOps: Navigate to Azure DevOps.
  2. Create a new project
    • Click on the "New Project" button.
    • Project Name: Enter a name for your project.
    • Description: Optionally, add a description to describe the project's purpose.
    • Visibility: Choose either public or private based on your preference.
  3. Create Project: Click "Create" to finalize the project setup.
    Create

Step 4. Connecting Azure DevOps to GitHub

To link your Azure DevOps project with your GitHub repository.

  1. Go to Repos: In your Azure DevOps project, navigate to "Repos" from the left-hand menu.
  2. Import Repository
    • Click on "Import a repository" at the top right.
    • Clone URL: Provide the URL of your GitHub repository.
    • Authentication: Enter your GitHub credentials if necessary to access the repository.
  3. Import: Click "Import" to create a copy of your GitHub repository in Azure DevOps.
    GitHub repository

Step 5. Creating a Build Pipeline

A build pipeline automates the process of building and testing your application. Here’s how to create one.

  1. Go to Pipelines: In Azure DevOps, navigate to "Pipelines" and then "Builds".
    Azure DevOps
  2. New Pipeline: Click on "New pipeline" and select "Use the classic editor".
  3. Select Repository: Choose your GitHub repository.
    • Source: Select "GitHub".
    • Repository: Choose your repository from the list or provide the URL.
      URL
  4. Select a Template: Choose the ASP.NET Core template, which includes tasks for restoring, building, and publishing your application.
  5. Configure Build Pipeline
    • Agent Pool: Choose "Azure Pipelines" for Microsoft's hosted agents.
    • Restore: Use the "NuGet restore" task to restore NuGet packages.
    • Task Name: NuGet restore
    • Path to the solution: Provide the path to your solution file (e.g., **/*.sln).
       Build Pipeline
  6. Build: Add a build task to compile your solution.
    • Task Name: .NET Core
    • Command: Build
    • Projects: Provide the path to your project file (e.g., **/*.csproj).
      Project file
  7. Test: Optionally, add a task to run tests.
    • Task Name: .NET Core
    • Command: Test
    • Projects: Provide the path to your test project file (e.g., **/*Tests/*.csproj).
  8. Publish: Use the "Publish Build Artifacts" task to publish the build outputs.
    • Task Name: Publish and build artifacts
    • Path to Publish: Specify the path to the directory containing your build outputs (e.g., $(Build.ArtifactStagingDirectory)).
    • Artifact Name: Name the artifact (e.g., drop).
    • Save and Queue: Save the pipeline and queue a new build to test the setup.
       Build Artifacts

Step 6. Creating a Release Pipeline

A release pipeline automates the process of deploying your application to various environments. Here’s how to set it up.

  1. Go to Releases: In Azure DevOps, navigate to "Pipelines" and then "Releases".
  2. New Pipeline: Click on "New pipeline" and select "Empty job".
    Empty job
  3. Add Artifacts: Link the artifacts from the build pipeline.
    • Source Type: Build
    • Project: Select your project.
    • Source: Choose the build pipeline created earlier.
    • Default Version: Select "Latest".
      Pipeline
  4. Add a Stage: Add a new stage and name it "Deployment".
    • Stage Name: Provide a name for the stage (e.g., Dev).
      Satge name
  5. Add Deployment Task
    • Click on "1 job, 0 task" under your stage.
    • Click the "+" icon to add a task and search for "Azure App Service deploy".
    • Azure Subscription: Authorize Azure DevOps to connect to your Azure subscription.
    • App Service Name: Select the web app you created earlier.
    • Package or Folder: Point to the build artifacts (e.g., $(System.DefaultWorkingDirectory)/_your_build_artifact_name/drop).
       Deployment Task
  6. Configure Continuous Deployment: Enable continuous deployment triggers to automatically deploy on successful builds.
    • Click on "Continuous deployment trigger" and enable it.
      Deployment trigger
  7. Save and Create Release: Save the pipeline and create a release to test the deployment.
    • Click "Create release" and select the appropriate stages.
    • Click "Create" to initiate the release process.
      Create release

Step 7. Monitoring and Verifying Deployment

Once the release pipeline is set up, it’s important to monitor and verify the deployment:

  1. Monitor Release: Go to "Releases" to track the deployment process.
    • Check logs and deployment status to ensure the process is completed successfully.
  2. Verify Deployment: After deployment, visit the Azure Portal and navigate to your web app.
    • Access the URL of your web app (e.g., https://yourappname.azurewebsites.net) to ensure it is running correctly.
      Web app
  3. Monitor the release pipeline
    Release pipeline 
    Test

Conclusion

In this article, you have learned how to set up an end-to-end CI/CD pipeline for an ASP.NET MVC application using Azure DevOps and GitHub. By following these steps, you can automate the process of building, testing, and deploying your application to Azure App Service, ensuring a streamlined and efficient development workflow. This setup not only enhances productivity but also ensures that your application is always up-to-date with the latest changes, providing a seamless experience for both developers and users.


Similar Articles