Skip to content

Commit 7c6fea0

Browse files
committed
Updated Workflow Letest
1 parent 12bb620 commit 7c6fea0

File tree

3 files changed

+197
-57
lines changed

3 files changed

+197
-57
lines changed

.github/workflows/build-deploy.yml

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,88 +6,126 @@ on:
66

77
env:
88
IMAGE_NAME: python-app
9+
TF_DIR: ./terraform # Update if your terraform path differs
910

1011
jobs:
1112
build-and-deploy:
1213
runs-on: ubuntu-latest
1314

1415
steps:
16+
###############################################################
17+
# Checkout Repository
18+
###############################################################
1519
- name: Checkout repository
1620
uses: actions/checkout@v4
1721

22+
###############################################################
23+
# Configure Git (for Terraform modules hosted on GitHub)
24+
###############################################################
1825
- name: Configure Git for Terraform module downloads
1926
run: |
2027
git config --global url."https://${{ secrets.GH_TOKEN }}:x-oauth-basic@github.com/".insteadOf "https://github.com/"
2128
22-
- name: Log in to Azure
29+
###############################################################
30+
# Login to Azure
31+
###############################################################
32+
- name: Azure Login
2333
uses: azure/login@v2
2434
with:
2535
creds: ${{ secrets.AZURE_CREDENTIALS }}
2636

37+
###############################################################
38+
# Login to Azure Container Registry
39+
###############################################################
2740
- name: Log in to ACR
28-
run: |
29-
az acr login --name ${{ secrets.ACR_NAME }}
41+
run: az acr login --name ${{ secrets.ACR_NAME }}
3042

43+
###############################################################
44+
# Determine Deployment Slot (Blue or Green)
45+
###############################################################
3146
- name: Determine Deployment Slot (Blue/Green)
3247
id: slot
3348
run: |
34-
# Toggle deployment slot
3549
LAST_SLOT_FILE=".last_slot"
3650
if [ -f "$LAST_SLOT_FILE" ]; then
3751
LAST_SLOT=$(cat $LAST_SLOT_FILE)
3852
if [ "$LAST_SLOT" == "blue" ]; then
39-
echo "green" > $LAST_SLOT_FILE
40-
echo "slot=green" >> $GITHUB_OUTPUT
53+
NEW_SLOT="green"
4154
else
42-
echo "blue" > $LAST_SLOT_FILE
43-
echo "slot=blue" >> $GITHUB_OUTPUT
55+
NEW_SLOT="blue"
4456
fi
4557
else
46-
echo "blue" > $LAST_SLOT_FILE
47-
echo "slot=blue" >> $GITHUB_OUTPUT
58+
NEW_SLOT="blue"
4859
fi
60+
echo $NEW_SLOT > $LAST_SLOT_FILE
61+
echo "slot=$NEW_SLOT" >> $GITHUB_OUTPUT
4962
50-
- name: Build and Push Docker image to ACR
63+
###############################################################
64+
# Build & Push Docker Image to ACR
65+
###############################################################
66+
- name: Build and Push Docker image
5167
run: |
5268
IMAGE_TAG=${{ github.run_number }}
53-
az acr build --registry ${{ secrets.ACR_NAME }} \
69+
az acr build \
70+
--registry ${{ secrets.ACR_NAME }} \
5471
--image ${{ env.IMAGE_NAME }}:${IMAGE_TAG} \
55-
--image ${{ env.IMAGE_NAME }}:latest .
72+
--image ${{ env.IMAGE_NAME }}:${{ steps.slot.outputs.slot }} \
73+
.
5674
75+
###############################################################
76+
# Setup Terraform
77+
###############################################################
5778
- name: Setup Terraform
5879
uses: hashicorp/setup-terraform@v3
5980
with:
6081
terraform_version: 1.9.5
6182

83+
###############################################################
84+
# Terraform Init
85+
###############################################################
6286
- name: Terraform Init
63-
working-directory: terraformv3/examples/complete/container_platform
87+
working-directory: ${{ env.TF_DIR }}
6488
env:
6589
ARM_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
6690
ARM_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
6791
ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
6892
ARM_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
6993
run: terraform init -input=false
7094

95+
###############################################################
96+
# Terraform Plan
97+
###############################################################
7198
- name: Terraform Plan
72-
working-directory: terraformv3/examples/complete/container_platform
99+
working-directory: ${{ env.TF_DIR }}
73100
env:
74101
ARM_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
75102
ARM_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
76103
ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
77104
ARM_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
78105
run: |
79106
terraform plan \
80-
-var="image_tag=${{ github.run_number }}" \
107+
-var="docker_image_name=${{ env.IMAGE_NAME }}" \
108+
-var="docker_registry_url=${{ secrets.ACR_LOGIN_SERVER }}" \
81109
-var="slot_name=${{ steps.slot.outputs.slot }}" \
82-
-var="acr_login_server=${{ secrets.ACR_LOGIN_SERVER }}" \
83-
-var="image_name=${{ env.IMAGE_NAME }}" \
110+
-var="image_tag=${{ github.run_number }}" \
84111
-out=tfplan
85112
113+
###############################################################
114+
# Terraform Apply (Deploy Selected Slot)
115+
###############################################################
86116
- name: Terraform Apply
87-
working-directory: terraformv3/examples/complete/container_platform
117+
working-directory: ${{ env.TF_DIR }}
88118
env:
89119
ARM_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
90120
ARM_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
91121
ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
92122
ARM_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
93123
run: terraform apply -auto-approve tfplan
124+
125+
###############################################################
126+
# Optional: Swap Slots (Green <-> Blue)
127+
###############################################################
128+
- name: Swap Slots (Post Deployment)
129+
if: success()
130+
working-directory: ${{ env.TF_DIR }}
131+
run: terraform apply -auto-approve -target=azurerm_web_app_slot_swap.swap
Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1+
###############################################################
2+
# Provider
3+
###############################################################
14
provider "azurerm" {
25
features {}
3-
# subscription_id = "1ac2caa4-336e-4daa-b8f1-0fbabe2d4b11"
46
}
57

6-
##-----------------------------------------------------------------------------
7-
## Local declaration
8-
##-----------------------------------------------------------------------------
8+
###############################################################
9+
# Local declaration
10+
###############################################################
911
locals {
1012
name = "ayush-test"
1113
environment = "dev"
1214
label_order = ["name", "environment"]
1315
location = "Canada Central"
1416
}
1517

16-
##-----------------------------------------------------------------------------
17-
## Resource group
18-
##-----------------------------------------------------------------------------
18+
###############################################################
19+
# Resource Group
20+
###############################################################
1921
module "resource_group" {
2022
source = "clouddrove/resource-group/azure"
2123
version = "1.0.2"
@@ -25,9 +27,9 @@ module "resource_group" {
2527
location = local.location
2628
}
2729

28-
##-----------------------------------------------------------------------------
29-
## Log Analytics
30-
##-----------------------------------------------------------------------------
30+
###############################################################
31+
# Log Analytics
32+
###############################################################
3133
module "log-analytics" {
3234
source = "clouddrove/log-analytics/azure"
3335
version = "1.1.0"
@@ -41,9 +43,9 @@ module "log-analytics" {
4143
log_analytics_workspace_id = module.log-analytics.workspace_id
4244
}
4345

44-
##-----------------------------------------------------------------------------
45-
## App service with container runtime
46-
##-----------------------------------------------------------------------------
46+
###############################################################
47+
# Primary App Service (Production Slot)
48+
###############################################################
4749
module "app-container" {
4850
source = "../../.."
4951
name = local.name
@@ -54,27 +56,19 @@ module "app-container" {
5456
os_type = "Linux"
5557
sku_name = "S1"
5658

57-
##-----------------------------------------------------------------------------
58-
## To Deploy Container
59-
##-----------------------------------------------------------------------------
6059
use_docker = true
6160
docker_image_name = "python-app"
6261
docker_registry_url = "ayushacr123.azurecr.io"
63-
# docker_registry_username = "<registryname>"
64-
# docker_registry_password = "<docker_registry_password>"
6562
acr_id = "/subscriptions/1ac2caa4-336e-4daa-b8f1-0fbabe2d4b11/resourceGroups/ayush-rg/providers/Microsoft.ContainerRegistry/registries/ayushacr123"
6663

6764
site_config = {
6865
container_registry_use_managed_identity = true
6966
}
67+
7068
app_settings = {
7169
foo = "bar"
7270
}
7371

74-
##-----------------------------------------------------------------------------
75-
## App Service logs
76-
##-----------------------------------------------------------------------------
77-
7872
app_service_logs = {
7973
detailed_error_messages = false
8074
failed_request_tracing = false
@@ -89,8 +83,62 @@ module "app-container" {
8983
}
9084
}
9185

92-
##-----------------------------------------------------------------------------
93-
## log analytics
94-
##-----------------------------------------------------------------------------
9586
log_analytics_workspace_id = module.log-analytics.workspace_id
96-
}
87+
}
88+
89+
###############################################################
90+
# Blue-Green Deployment Configuration
91+
###############################################################
92+
93+
# --- Blue Slot (Active Production) ---
94+
resource "azurerm_linux_web_app_slot" "blue" {
95+
name = "blue"
96+
app_service_id = module.app-container.app_service_id # ✅ FIXED
97+
service_plan_id = module.app-container.app_service_plan_id
98+
99+
site_config {
100+
linux_fx_version = "DOCKER|ayushacr123.azurecr.io/python-app:blue"
101+
container_registry_use_managed_identity = true
102+
}
103+
104+
app_settings = {
105+
SLOT_NAME = "blue"
106+
}
107+
108+
identity {
109+
type = "SystemAssigned"
110+
}
111+
}
112+
113+
# --- Green Slot (Staging Slot) ---
114+
resource "azurerm_linux_web_app_slot" "green" {
115+
name = "green"
116+
app_service_id = module.app-container.app_service_id # ✅ FIXED
117+
service_plan_id = module.app-container.app_service_plan_id
118+
119+
120+
site_config {
121+
linux_fx_version = "DOCKER|ayushacr123.azurecr.io/python-app:green"
122+
container_registry_use_managed_identity = true
123+
}
124+
app_settings = {
125+
SLOT_NAME = "green"
126+
}
127+
128+
identity {
129+
type = "SystemAssigned"
130+
}
131+
}
132+
133+
###############################################################
134+
# Optional: Blue-Green Swap (Manual or Pipeline Triggered)
135+
###############################################################
136+
# This resource defines slot swap (to switch traffic)
137+
# You can trigger it manually or via CI/CD (e.g., GitHub Actions)
138+
139+
resource "azurerm_web_app_slot_swap" "swap" {
140+
resource_group_name = module.resource_group.resource_group_name
141+
web_app_name = module.app-container.app_service_name
142+
slot_name = azurerm_linux_web_app_slot.green.name # New version (staging)
143+
target_slot_name = azurerm_linux_web_app_slot.blue.name # Current production
144+
}
Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,74 @@
1-
variable "image_tag" {
2-
description = "Tag of the Docker image to deploy"
1+
###############################################################
2+
# Variables
3+
###############################################################
4+
5+
variable "subscription_id" {
6+
description = "Azure Subscription ID"
37
type = string
4-
default = "latest"
58
}
6-
# EnVirement ( dev )
7-
variable "env" {
8-
description = "Environment name (dev, test, prod)"
9+
10+
variable "location" {
11+
description = "Azure region for deployment"
912
type = string
10-
default = "dev"
13+
default = "Canada Central"
14+
}
15+
16+
variable "name" {
17+
description = "Base name for all resources"
18+
type = string
19+
default = "ayush-test"
1120
}
12-
variable "slot_name" {
21+
22+
variable "environment" {
23+
description = "Environment name (e.g. dev, staging, prod)"
1324
type = string
14-
description = "Deployment slot name (blue or green)"
15-
default = "blue"
25+
default = "dev"
1626
}
27+
1728
variable "docker_registry_url" {
29+
description = "Azure Container Registry login server URL"
30+
type = string
31+
default = "ayushacr123.azurecr.io"
32+
}
33+
34+
variable "docker_image_name" {
35+
description = "Docker image name for deployment"
36+
type = string
37+
default = "python-app"
38+
}
39+
40+
variable "acr_id" {
41+
description = "Resource ID of Azure Container Registry"
1842
type = string
19-
description = "ACR login server URL"
20-
}
43+
default = "/subscriptions/1ac2caa4-336e-4daa-b8f1-0fbabe2d4b11/resourceGroups/ayush-rg/providers/Microsoft.ContainerRegistry/registries/ayushacr123"
44+
}
45+
46+
variable "sku_name" {
47+
description = "App Service plan SKU (S1, P1v2, etc.)"
48+
type = string
49+
default = "S1"
50+
}
51+
52+
variable "os_type" {
53+
description = "Operating system type"
54+
type = string
55+
default = "Linux"
56+
}
57+
58+
variable "enable_logs" {
59+
description = "Enable or disable App Service logs"
60+
type = bool
61+
default = true
62+
}
63+
64+
variable "log_retention_days" {
65+
description = "Days to retain logs"
66+
type = number
67+
default = 7
68+
}
69+
70+
variable "log_retention_mb" {
71+
description = "Retention size (MB) for logs"
72+
type = number
73+
default = 35
74+
}

0 commit comments

Comments
 (0)