Deploy a .NET Core Application on Azure App Service Using GitHub Actions CI/CD

Here's a step-by-step guide for deploying a .NET Core application to Azure App Service using GitHub Actions for CI/CD:

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



Step 1: Prerequisites

  1. Set Up Azure Resources:

    • Log in to the Azure portal.
    • Create an Azure App Service for hosting the application.
    • Create an Azure Container Registry (ACR) to store your container images.
  2. Set Up Your GitHub Repository:

    • Ensure your .NET Core application is hosted in a GitHub repository.
    • Include a Dockerfile in the root of your project for containerization (example provided in Step 2).
  3. Install Azure CLI (if not already installed):

  4. Login to Azure CLI:

    az login

Step 2: Add a Dockerfile to Your Project

  1. Navigate to the root of your .NET Core application.
  2. Create a Dockerfile for containerizing your application:
    FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
    WORKDIR /app
    EXPOSE 80
    
    FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
    WORKDIR /src
    COPY . .
    RUN dotnet restore
    RUN dotnet publish -c Release -o /app
    
    FROM base AS final
    WORKDIR /app
    COPY --from=build /app .
    ENTRYPOINT ["dotnet", "YourAppName.dll"]
    Replace YourAppName.dll with the name of your .dll file.

Step 3: Push Your Code to GitHub

  1. Commit your changes:
    git add .
    git commit -m "Add Dockerfile for containerization"
    git push origin main

Step 4: Configure Azure Container Registry (ACR)

  1. Log in to Azure ACR:

    az acr login --name <ACR_NAME>
  2. Get ACR Details:

    az acr show --name <ACR_NAME> --query "loginServer" --output tsv

    Note down the ACR loginServer.


Step 5: Create a GitHub Actions Workflow

  1. In your GitHub repository, create a .github/workflows/ci-cd.yml file.
  2. Add the following workflow configuration:
    name: CI/CD Pipeline
    
    on:
      push:
        branches:
          - main
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout code
            uses: actions/checkout@v3
    
          - name: Log in to Azure Container Registry
            uses: azure/docker-login@v1
            with:
              login-server: <ACR_LOGIN_SERVER>
              username: ${{ secrets.AZURE_ACR_USERNAME }}
              password: ${{ secrets.AZURE_ACR_PASSWORD }}
    
          - name: Build and Push Docker Image
            run: |
              docker build -t <ACR_LOGIN_SERVER>/<IMAGE_NAME>:${{ github.sha }} .
              docker push <ACR_LOGIN_SERVER>/<IMAGE_NAME>:${{ github.sha }}
    
      deploy:
        needs: build
        runs-on: ubuntu-latest
        steps:
          - name: Deploy to Azure App Service
            uses: azure/webapps-deploy@v2
            with:
              app-name: <APP_SERVICE_NAME>
              slot-name: Production
              images: |
                <ACR_LOGIN_SERVER>/<IMAGE_NAME>:${{ github.sha }}
    Replace placeholders like <ACR_LOGIN_SERVER>, <IMAGE_NAME>, and <APP_SERVICE_NAME> with your actual values.

Step 6: Set Up GitHub Secrets

  1. In your GitHub repository, go to Settings > Secrets and Variables > Actions.
  2. Add the following secrets:
    • AZURE_ACR_USERNAME: Azure Container Registry username.
    • AZURE_ACR_PASSWORD: Azure Container Registry password.
    • AZURE_APP_SERVICE_PUBLISH_PROFILE: Download the publish profile from Azure App Service and paste its contents here.

Step 7: Test the Workflow

  1. Push a code change to the main branch.
  2. Check the Actions tab in GitHub to monitor the CI/CD pipeline.

Step 8: Quality Assurance (QA)

  1. After the deployment to the DEV environment, test the application.
  2. Ensure all QA validations pass.

Step 9: Deploy to Production

  1. Update the GitHub Actions workflow to include a manual approval step before deployment to production.
  2. Add an environment in GitHub (Settings > Environments) named Production and require approvals.

Step 10: Monitor and Scale the Application

  1. Use the Azure Portal to monitor application performance.
  2. Scale the App Service plan as needed for handling production traffic.

🌟 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