Skip to content

Commit 9221c6b

Browse files
committed
linkage
1 parent 1ea32ef commit 9221c6b

4 files changed

Lines changed: 162 additions & 102 deletions

File tree

.github/workflows/backend-cd.yml

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
name: CD - Deploy Backend Services to AKS
22

33
on:
4-
workflow_dispatch:
4+
# ----------------------------------------------------------------------
5+
# CRITICAL CHANGE: SWITCH TO WORKFLOW_CALL TO ENABLE AUTO-TRIGGERING
6+
# ----------------------------------------------------------------------
7+
workflow_call: # <--- CRITICAL FIX: Enables calling from backend_ci.yml
58
inputs:
6-
aks_cluster_name:
7-
description: 'Name of the AKS Cluster to deploy to'
8-
required: true
9-
default: 'ishaanAKS'
10-
aks_resource_group:
11-
description: 'Resource Group of the AKS Cluster'
12-
required: true
13-
default: 'deakinuni'
14-
aks_acr_name:
15-
description: 'Name of ACR'
16-
required: true
17-
default: 'ishaan'
9+
aks_cluster_name: { required: true, type: string }
10+
aks_resource_group: { required: true, type: string }
11+
aks_acr_name: { required: true, type: string }
12+
secrets:
13+
azure_credentials: { required: true }
1814

1915
jobs:
2016
deploy_backend:
2117
runs-on: ubuntu-latest
2218
environment: Production
2319

20+
# CRITICAL CHANGE: Update outputs to match the step IDs below
2421
outputs:
25-
PRODUCT_API_IP: ${{ steps.get_product_ip.outputs.external_ip }}
26-
ORDER_API_IP: ${{ steps.get_order_ip.outputs.external_ip }}
22+
PRODUCT_API_IP: ${{ steps.ip_capture.outputs.PRODUCT_IP }} # <--- UPDATED
23+
ORDER_API_IP: ${{ steps.ip_capture.outputs.ORDER_IP }} # <--- UPDATED
2724

2825
steps:
2926
- name: Checkout repository
@@ -32,16 +29,18 @@ jobs:
3229
- name: Log in to Azure
3330
uses: azure/login@v1
3431
with:
35-
creds: ${{ secrets.AZURE_CREDENTIALS }}
32+
creds: ${{ secrets.azure_credentials }} # <--- UPDATED: Using `secrets` from workflow_call
3633
enable-AzPSSession: true
3734

3835
- name: Set Kubernetes context (get AKS credentials)
3936
run: |
40-
az aks get-credentials --resource-group ${{ github.event.inputs.aks_resource_group }} --name ${{ github.event.inputs.aks_cluster_name }} --overwrite-existing
37+
# <--- UPDATED: Using `inputs` from workflow_call
38+
az aks get-credentials --resource-group ${{ inputs.aks_resource_group }} --name ${{ inputs.aks_cluster_name }} --overwrite-existing
4139
4240
- name: Attach ACR
4341
run: |
44-
az aks update --name ${{ github.event.inputs.aks_cluster_name }} --resource-group ${{ github.event.inputs.aks_resource_group }} --attach-acr ${{ github.event.inputs.aks_acr_name }}
42+
# <--- UPDATED: Using `inputs` from workflow_call
43+
az aks update --name ${{ inputs.aks_cluster_name }} --resource-group ${{ inputs.aks_resource_group }} --attach-acr ${{ inputs.aks_acr_name }}
4544
4645
- name: Deploy Backend Infrastructure (Namespace, ConfigMaps, Secrets, Databases)
4746
run: |
@@ -60,6 +59,7 @@ jobs:
6059
kubectl apply -f order-service.yaml
6160
6261
- name: Wait for Backend LoadBalancer IPs
62+
id: ip_capture # <--- CRITICAL FIX: Added ID to make outputs work
6363
run: |
6464
echo "Waiting for Product, Order LoadBalancer IPs to be assigned (up to 5 minutes)..."
6565
PRODUCT_IP=""
@@ -74,6 +74,10 @@ jobs:
7474
echo "All backend LoadBalancer IPs assigned!"
7575
echo "Product Service IP: $PRODUCT_IP"
7676
echo "Order Service IP: $ORDER_IP"
77+
78+
# CRITICAL FIX: Publish the IPs as step outputs
79+
echo "PRODUCT_IP=$PRODUCT_IP" >> "$GITHUB_OUTPUT" # <--- NEW: Set output for the next job
80+
echo "ORDER_IP=$ORDER_IP" >> "$GITHUB_OUTPUT" # <--- NEW: Set output for the next job
7781
break
7882
fi
7983
sleep 5 # Wait 5 seconds before next attempt
@@ -84,18 +88,28 @@ jobs:
8488
exit 1 # Fail the job if IPs are not obtained
8589
fi
8690
87-
# These are environment variables for subsequent steps in the *same job*
88-
# And used to set the job outputs
89-
echo "PRODUCT_IP=$PRODUCT_IP" >> $GITHUB_ENV
90-
echo "ORDER_IP=$ORDER_IP" >> $GITHUB_ENV
91-
92-
- name: Capture Product Service IP for Workflow Output
93-
id: get_product_ip
94-
run: echo "external_ip=${{ env.PRODUCT_IP }}" >> $GITHUB_OUTPUT
95-
96-
- name: Capture Order Service IP for Workflow Output
97-
id: get_order_ip
98-
run: echo "external_ip=${{ env.ORDER_IP }}" >> $GITHUB_OUTPUT
91+
# Removed redundant IP environment setup, now using step outputs.
92+
93+
# Removed redundant 'Capture IP for Workflow Output' steps
9994

10095
- name: Logout from Azure
10196
run: az logout
97+
98+
# ----------------------------------------------------------------------
99+
# NEW JOB: LINKAGE TO FRONTEND CI
100+
# ----------------------------------------------------------------------
101+
trigger_frontend_ci:
102+
runs-on: ubuntu-latest
103+
needs: deploy_backend # Waits for deployment and IP capture
104+
steps:
105+
- name: "Call Frontend CI Workflow"
106+
uses: ./.github/workflows/frontend_ci.yml # Calls the next file
107+
with:
108+
# Pass the captured IPs from the previous job's outputs
109+
product_api_ip: ${{ needs.deploy_backend.outputs.PRODUCT_API_IP }}
110+
order_api_ip: ${{ needs.deploy_backend.outputs.ORDER_API_IP }}
111+
# Pass cluster details received by this workflow
112+
aks_cluster_name: ${{ inputs.aks_cluster_name }}
113+
aks_resource_group: ${{ inputs.aks_resource_group }}
114+
secrets:
115+
azure_credentials: ${{ secrets.azure_credentials }} # Pass the secret

.github/workflows/backend_ci.yml

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ on:
1212
push:
1313
branches:
1414
- development
15-
# - main
15+
- main # <-- UPDATED: Now triggers when code is merged into main
1616
paths: # Only trigger if changes are in backend directories
1717
- 'backend/**'
1818
- '.github/workflows/backend_ci.yml' # Trigger if this workflow file changes
1919
pull_request:
2020
branches:
21-
- main
21+
- main                            
2222
paths:
2323
- 'backend/**'
2424
- '.github/workflows/backend_ci.yml'
@@ -151,3 +151,24 @@ jobs:
151151
- name: Logout from Azure
152152
run: az logout
153153
if: always()
154+
155+
# ----------------------------------------------------------------------
156+
# NEW JOB: LINKAGE TO THE CD PIPELINE
157+
# ----------------------------------------------------------------------
158+
trigger_backend_cd: # <-- NEW JOB NAME
159+
runs-on: ubuntu-latest
160+
needs: build_and_push_images # <-- Ensure images are built before triggering CD
161+
162+
# CRITICAL GATE: Only execute deployment when merged into 'main'
163+
if: github.ref == 'refs/heads/main' # <-- NEW CONDITION
164+
165+
steps:
166+
- name: "Call Backend CD Workflow" # <-- NEW STEP
167+
# This calls the backend-cd.yml file directly, linking the workflows
168+
uses: ./.github/workflows/backend-cd.yml
169+
with:
170+
# Pass required deployment details to the CD workflow
171+
aks_cluster_name: 'ishaanAKS' # <-- Update with your cluster name
172+
aks_resource_group: 'deakinuni' # <-- Update with your resource group
173+
aks_acr_name: ${{ env.ACR_LOGIN_SERVER }}
174+
azure_credentials: ${{ secrets.AZURE_CREDENTIALS }}

.github/workflows/frontend-cd.yml

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22

33
name: CD - Deploy Frontend to AKS
44

5-
# This workflow can be called by other workflows and takes inputs.
6-
# Or it can be run manually if you provide the IPs.
75
on:
86
workflow_dispatch:
97
inputs:
108
product_api_ip:
119
description: 'External IP of Product Service'
1210
required: true
13-
default: 'http://<ip_address>:8000'
11+
default: '0.0.0.0'
1412
order_api_ip:
15-
description: 'External IP of Order Service (e.g., http://Y.Y.Y.Y:8001)'
13+
description: 'External IP of Order Service'
1614
required: true
17-
default: 'http://<ip_address>:8001'
15+
default: '0.0.0.0'
1816
aks_cluster_name:
1917
description: 'Name of the AKS Cluster to deploy to'
2018
required: true
@@ -24,6 +22,7 @@ on:
2422
required: true
2523
default: 'deakinuni'
2624

25+
# CRITICAL: This is how it's called by frontend_ci.yml
2726
workflow_call:
2827
inputs:
2928
product_api_ip:
@@ -38,7 +37,10 @@ on:
3837
aks_resource_group:
3938
required: true
4039
type: string
41-
40+
# CRITICAL FIX: Must define which secrets are required by this workflow
41+
secrets:
42+
azure_credentials: { required: true } # <--- ADDED SECRET HERE
43+
4244
jobs:
4345
deploy_frontend:
4446
runs-on: ubuntu-latest
@@ -48,46 +50,41 @@ jobs:
4850
- name: Checkout repository
4951
uses: actions/checkout@v4
5052

51-
# Azure login using a Service Principal secret
53+
# Azure login using the secret passed via workflow_call
5254
- name: Azure Login
5355
uses: azure/login@v1
5456
with:
55-
creds: ${{ secrets.AZURE_CREDENTIALS }}
56-
57-
# Login to Azure Container Registry (ACR)
58-
- name: Login to Azure Container Registry
59-
run: az acr login --name ${{ secrets.AZURE_CONTAINER_REGISTRY }}
60-
61-
- name: Inject Backend IPs into Frontend main.js
62-
run: |
63-
echo "Injecting IPs into frontend/static/js/main.js"
64-
# Ensure frontend/main.js is directly in the path for sed
65-
sed -i "s|_PRODUCT_API_URL_|${{ inputs.product_api_ip }}|g" frontend/main.js
66-
sed -i "s|_ORDER_API_URL_|${{ inputs.order_api_ip }}|g" frontend/main.js
67-
68-
# Display the modified file content for debugging
69-
echo "--- Modified main.js content ---"
70-
cat frontend/main.js
71-
echo "---------------------------------"
57+
# CRITICAL FIX: Use the secret passed from the caller
58+
creds: ${{ secrets.azure_credentials }}
7259

73-
# Build and Push Docker image for Frontend
74-
- name: Build and Push Frontend Image
75-
run: |
76-
docker build -t ${{ secrets.AZURE_CONTAINER_REGISTRY }}/frontend:latest ./frontend/
77-
docker push ${{ secrets.AZURE_CONTAINER_REGISTRY }}/frontend:latest
60+
# --- OPTIMISATION: REMOVED ACR Login, Inject IPs, and Build/Push steps ---
61+
# These steps were already done by frontend_ci.yml (Step 3 in the chain).
62+
# The IP linkage will now be done with a robust `kubectl` command.
7863

7964
- name: Set Kubernetes context (get AKS credentials)
8065
uses: azure/aks-set-context@v3
8166
with:
8267
resource-group: ${{ inputs.aks_resource_group }}
8368
cluster-name: ${{ inputs.aks_cluster_name }}
8469

85-
- name: Deploy Frontend to AKS
70+
- name: Deploy Frontend to AKS (Apply Base Manifest)
8671
run: |
87-
echo "Deploying frontend with latest tag to AKS cluster: ${{ inputs.aks_cluster_name }}"
72+
echo "Applying frontend base manifest to AKS cluster: ${{ inputs.aks_cluster_name }}"
8873
cd k8s/
89-
# Ensure frontend-service.yaml is configured with your ACR
74+
# Deploy the base frontend service/deployment
9075
kubectl apply -f frontend.yaml
76+
77+
# CRITICAL NEW STEP: Configure Frontend Deployment with Backend IPs
78+
# This is the most reliable way to link the services in AKS
79+
- name: Configure Frontend Deployment with Backend IPs
80+
run: |
81+
echo "Setting backend IP environment variables on frontend deployment..."
82+
# Pass the IPs as environment variables to the live Kubernetes deployment
83+
# Note: Adjust 'frontend-w08e1' if your deployment name is different in frontend.yaml
84+
kubectl set env deployment/frontend-w08e1 \
85+
PRODUCT_API_URL=http://${{ inputs.product_api_ip }}:80 \
86+
ORDER_API_URL=http://${{ inputs.order_api_ip }}:80 \
87+
--overwrite
9188
92-
- name: Logout from Azure (AKS deployment)
93-
run: az logout
89+
- name: Logout from Azure
90+
run: az logout

0 commit comments

Comments
 (0)