To automate the deployment of your .NET backend API using Azure App Service with Terraform and set up a CI/CD pipeline, you need to follow these steps:

  1. Create a Terraform configuration for Azure App Service
  2. Set up GitHub Actions for CI/CD

Here’s a detailed guide on how to achieve this:

Step 1: Create Terraform Configuration

Create a Terraform configuration file (main.tf) to set up your Azure App Service.

# main.tf

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "rg" {
  name     = "myResourceGroup"
  location = "East US"
}

resource "azurerm_app_service_plan" "asp" {
  name                = "myAppServicePlan"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  sku {
    tier = "Basic"
    size = "B1"
  }
}

resource "azurerm_app_service" "app" {
  name                = "myAppService"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  app_service_plan_id = azurerm_app_service_plan.asp.id

  app_settings = {
    "WEBSITES_PORT" = "8080"  # Replace with the port your app listens on
  }

  site_config {
    app_command_line = "dotnet your_app.dll"
  }

  lifecycle {
    ignore_changes = [
      app_settings["WEBSITES_PORT"]
    ]
  }
}

resource "azurerm_container_registry" "acr" {
  name                = "myContainerRegistry"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  sku                 = "Basic"
  admin_enabled       = true
}

resource "azurerm_app_service_source_control" "sourcecontrol" {
  app_id     = azurerm_app_service.app.id
  branch     = "main"
  repo_url   = "https://github.com/yourusername/yourrepo"
  scm_type   = "GitHub"

  github_action_configuration {
    repo_url  = "https://github.com/yourusername/yourrepo"
    branch    = "main"
    workflow  = <<-EOF
    name: Deploy to Azure

    on:
      push:
        branches:
          - main

    jobs:
      build-and-deploy:
        runs-on: ubuntu-latest

        steps:
        - name: 'Checkout GitHub Action'
          uses: actions/checkout@v2

        - name: Set up Docker Buildx
          uses: docker/setup-buildx-action@v1

        - name: Login to Docker Hub
          uses: docker/login-action@v1
          with:
            username: ${{ secrets.DOCKER_USERNAME }}
            password: ${{ secrets.DOCKER_PASSWORD }}

        - name: Build and push Docker image
          uses: docker/build-push-action@v2
          with:
            context: .
            file: ./Dockerfile
            push: true
            tags: yourdockerhubusername/yourimagename:latest

        - name: Azure Login
          uses: azure/login@v1
          with:
            creds: ${{ secrets.AZURE_CREDENTIALS }}

        - name: 'Deploy to Azure Web App'
          id: deploy
          uses: azure/webapps-deploy@v2
          with:
            app-name: 'myAppService'
            slot-name: 'production'
            images: 'yourdockerhubusername/yourimagename:latest'
    EOF
  }
}

Step 2: Set up GitHub Actions for CI/CD

Create a GitHub Actions workflow file in your repository at .github/workflows/deploy.yml:

name: Deploy to Azure

on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    steps:
    - name: 'Checkout GitHub Action'
      uses: actions/checkout@v2

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v1

    - name: Login to Docker Hub
      uses: docker/login-action@v1
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}

    - name: Build and push Docker image
      uses: docker/build-push-action@v2
      with:
        context: .
        file: ./Dockerfile
        push: true
        tags: yourdockerhubusername/yourimagename:latest

    - name: Azure Login
      uses: azure/login@v1
      with:
        creds: ${{ secrets.AZURE_CREDENTIALS }}

    - name: 'Deploy to Azure Web App'
      id: deploy
      uses: azure/webapps-deploy@v2
      with:
        app-name: 'myAppService'
        slot-name: 'production'
        images: 'yourdockerhubusername/yourimagename:latest'

Step 3: Configure Secrets in GitHub

In your GitHub repository, go to Settings -> Secrets and add the following secrets:

  • DOCKER_USERNAME: Your Docker Hub username.
  • DOCKER_PASSWORD: Your Docker Hub password.
  • AZURE_CREDENTIALS: JSON output from the Azure CLI az ad sp create-for-rbac --name "myAppServicePrincipal" --role contributor --scopes /subscriptions/{subscription-id} --sdk-auth.

Step 4: Initialize and Apply Terraform Configuration

Run the following commands to initialize and apply your Terraform configuration:

terraform init
terraform apply

This will create the necessary Azure resources, including the App Service and Container Registry.

Step 5: Tear Down the Infrastructure

To tear down the infrastructure, you can use the terraform destroy command:

terraform destroy

This will delete all the resources created by your Terraform configuration.

By following these steps, you'll have a fully automated CI/CD process that builds your Docker image, pushes it to Docker Hub, and deploys it to Azure App Service using GitHub Actions and Terraform.

Edit

Pub: 11 Jun 2024 04:41 UTC

Views: 63