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:
- Create a Terraform configuration for Azure App Service
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | # 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
:
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 CLIaz 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:
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:
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.