Deploy .NET Core Application on Azure App Service Using Azure DevOps CI/CD

This guide provides a comprehensive, step-by-step process for deploying a .NET Core application to Azure App Service using Azure DevOps for Continuous Integration (CI) and Continuous Deployment (CD). It covers essential steps, starting from creating an Azure App Service, setting up an ASP.NET Core project in Visual Studio, and pushing the code to Azure Repos, to configuring automated build and deployment pipelines in Azure DevOps. By the end of this guide, you'll have a fully automated deployment pipeline that allows for seamless code integration and deployment of your .NET Core application to Azure, enhancing development efficiency and reliability.

Here is the general architecture diagram that we will be deploying.


1. Create Azure App Service

To host your .NET Core application, you first need to create an Azure App Service.

Steps:

  1. Login to Azure Portal:

  2. Create a New App Service:

    • In the left sidebar, click on Create a resource.
    • Search for App Service and click on it.
    • Click Create to start the creation process.
  3. Configure the App Service:

    • Subscription: Select your Azure subscription.
    • Resource Group: Either select an existing resource group or create a new one.
    • Name: Provide a unique name for your App Service (this will be part of the URL).
    • Publish: Select Code (since you are deploying a .NET Core application).
    • Runtime Stack: Select .NET Core (or the specific version you are using).
    • Region: Select the region where you want the app to be hosted.
    • App Service Plan: Choose an existing plan or create a new one. If you are just testing, select a Free or Basic plan.
  4. Review and Create:

    • Review the configurations and click Create. The App Service will be provisioned in a few minutes.
  5. Note down the App Service URL:

    • After deployment, the App Service URL will look like https://your-app-service-name.azurewebsites.net. This will be where your application is hosted.

2. Create an ASP.NET Core Project in Visual Studio

Now, create an ASP.NET Core project in Visual Studio.

Steps:

  1. Open Visual Studio:

    • Launch Visual Studio on your computer.
    • Ensure you have the ASP.NET and web development workload installed.
  2. Create a New Project:

    • Go to File > New > Project.
    • Select ASP.NET Core Web Application.
    • Choose Next.
  3. Configure Project:

    • Project Name: Provide a name for your project (e.g., MyAspNetCoreApp).
    • Location: Choose a folder where the project will be stored.
    • Click Create.
  4. Select Template:

    • Choose Web Application (Model-View-Controller) for a typical MVC application or another template depending on your needs.
    • Click Create to create the project.
  5. Build the Application:

    • Once the project is created, you can build and run it locally to ensure it works properly by pressing Ctrl+F5.

3. Push the ASP.NET Core Project to Azure Repos

Now, push your project to Azure Repos for version control and to integrate with the CI/CD pipeline.

Steps:

  1. Set Up Azure DevOps Repository:

    • Go to Azure DevOps and create a new project (if not already created).
    • In the Repos section, create a new Git repository.
  2. Initialize Local Git Repository:

    • Open Visual Studio.
    • In Solution Explorer, right-click on the solution and select Add Solution to Source Control.
    • Visual Studio will initialize a local Git repository for you.
  3. Connect to Azure Repos:

    • In Team Explorer, click on Home and then Sync.
    • Click Publish to Azure DevOps.
    • Select the Azure DevOps project and Git repository you created earlier.
    • Click Publish.
  4. Commit and Push Changes:

    • In Team Explorer, go to Changes, enter a commit message (e.g., "Initial commit"), and click Commit All.
    • Then, click Push to upload the code to Azure Repos.

    Or, from the command line:

    git add .
    git commit -m "Initial commit"
    git remote add origin https://dev.azure.com/{organization}/{project}/_git/{repository-name}
    git push -u origin master

4. Set Up Continuous Integration (CI) Pipeline in Azure DevOps

The CI pipeline is responsible for building your application, restoring dependencies, and packaging the artifacts for deployment.

Steps:

  1. Go to Pipelines in Azure DevOps:

    • In Azure DevOps, navigate to Pipelines and click Create Pipeline.
  2. Select the Repository:

    • Choose Azure Repos Git (if using Azure Repos) or GitHub (if using GitHub) as the source of your code.
  3. Choose Pipeline Template:

    • Select the .NET Core template from the list of available templates.
  4. Modify the YAML File:

    • In the generated YAML file, modify it according to your needs. Below is a basic setup for building and publishing your .NET Core application:

      trigger:
        branches:
          include:
            - main  # or master, depending on your branch
      
      pool:
        vmImage: 'windows-latest'
      
      steps:
      - task: UseDotNet@2
        inputs:
          packageType: 'sdk'
          version: '6.x'  # Replace with your .NET Core version
          installationPath: $(Agent.ToolsDirectory)/dotnet
      
      - task: DotNetCoreCLI@2
        inputs:
          command: 'restore'
          projects: '**/*.csproj'
      
      - task: DotNetCoreCLI@2
        inputs:
          command: 'build'
          projects: '**/*.csproj'
      
      - task: DotNetCoreCLI@2
        inputs:
          command: 'publish'
          publishWebProjects: true
          arguments: '--configuration Release --output $(Build.ArtifactStagingDirectory)'
      - task: PublishBuildArtifacts@1
        inputs:
          pathToPublish: $(Build.ArtifactStagingDirectory)
          artifactName: drop
  5. Save and Run the Pipeline:

    • After configuring the pipeline, click Save and then Run to execute the pipeline. This will compile your .NET Core application and create build artifacts.

5. Set Up Continuous Deployment (CD) Pipeline

Once the build artifacts are created, you need to deploy them to your Azure App Service using the Release Pipeline.

Steps:

  1. Create a Release Pipeline:

    • In Azure DevOps, go to Pipelines > Releases and click New pipeline.
    • Select the Artifact produced by the CI pipeline (from the build step).
  2. Add a Stage for Deployment:

    • Add a Stage in the release pipeline (e.g., "Production" or "Staging").
    • Click on the Stage to configure it.
  3. Configure Azure App Service Deployment:

    • In the Stage, click Add an Azure App Service deployment task.
    • Configure the task:
      • Azure Subscription: Select your Azure subscription (you may need to authenticate it).
      • App Service Name: Enter the name of the App Service you created earlier.
      • Package or Folder: Select the path to the build artifact from the previous pipeline (usually something like $(System.ArtifactsDirectory)/drop/**/*.zip).
    • This task will deploy the .zip package to the App Service.
  4. Set Up Continuous Deployment Trigger:

    • Enable a trigger to start the release automatically when a new build artifact is available.
    • Go to the Triggers tab in the release pipeline and enable the Continuous Deployment Trigger.
  5. Save and Deploy:

    • Save your release pipeline.
    • You can manually trigger the release by clicking Create Release or rely on the automatic deployment when the CI pipeline is successful.

6. Test the Deployment

Once the pipeline runs successfully, your .NET Core application will be deployed to the Azure App Service.

  1. Open a browser and navigate to your App Service URL (e.g., https://your-app-service-name.azurewebsites.net).
  2. Verify that the application is up and running.

By following these steps, you have successfully:

  • Created an Azure App Service to host your .NET Core application.
  • Developed an ASP.NET Core project in Visual Studio and pushed it to Azure Repos.
  • Set up Continuous Integration (CI) and Continuous Deployment (CD) pipelines in Azure DevOps to automate the build and deployment process.

This end-to-end guide ensures that your .NET Core application is continuously integrated and deployed to Azure App Service, providing a streamlined and automated workflow.

🌟 Master Microsoft Azure with Microsoft Azure in Action! ðŸŒŸ

Dive into the world of cloud computing and supercharge your skills! This practical guide is packed with step-by-step tutorials, real-world use cases, and the latest Azure features to help you build, deploy, and manage scalable cloud apps like a pro. 🚀

🔥 Exclusive Deal Alert! Unlock amazing savings of 34.04% today! 🎉 Don’t miss this chance to learn Azure while saving big.

👇 Click now to claim your discount and start your Azure journey! ðŸ‘‡
👉 Grab Your 34% Discount Now!

Hurry—this offer won't last forever! ⏳

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete