CD - Deploy Frontend to AKS #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # week08/.github/workflows/frontend-cd.yml | |
| name: CD - Deploy Frontend to AKS | |
| # This workflow can be called by other workflows and takes inputs. | |
| # Or it can be run manually if you provide the IPs. | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| product_api_ip: | |
| description: 'External IP of Product Service' | |
| required: true | |
| default: 'http://<ip_address>:8000' | |
| order_api_ip: | |
| description: 'External IP of Order Service (e.g., http://Y.Y.Y.Y:8001)' | |
| required: true | |
| default: 'http://<ip_address>:8001' | |
| aks_cluster_name: | |
| description: 'Name of the AKS Cluster to deploy to' | |
| required: true | |
| default: '<aks_name>' | |
| aks_resource_group: | |
| description: 'Resource Group of the AKS Cluster' | |
| required: true | |
| default: '<<resource_group_name>' | |
| workflow_call: | |
| inputs: | |
| product_api_ip: | |
| required: true | |
| type: string | |
| order_api_ip: | |
| required: true | |
| type: string | |
| aks_cluster_name: | |
| required: true | |
| type: string | |
| aks_resource_group: | |
| required: true | |
| type: string | |
| jobs: | |
| deploy_frontend: | |
| runs-on: ubuntu-latest | |
| environment: Production | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| # Azure login using a Service Principal secret | |
| - name: Azure Login | |
| uses: azure/login@v1 | |
| with: | |
| creds: ${{ secrets.AZURE_CREDENTIALS }} | |
| # Login to Azure Container Registry (ACR) | |
| - name: Login to Azure Container Registry | |
| run: az acr login --name ${{ secrets.AZURE_CONTAINER_REGISTRY }} | |
| - name: Inject Backend IPs into Frontend main.js | |
| run: | | |
| echo "Injecting IPs into frontend/static/js/main.js" | |
| # Ensure frontend/main.js is directly in the path for sed | |
| sed -i "s|_PRODUCT_API_URL_|${{ inputs.product_api_ip }}|g" frontend/main.js | |
| sed -i "s|_ORDER_API_URL_|${{ inputs.order_api_ip }}|g" frontend/main.js | |
| # Display the modified file content for debugging | |
| echo "--- Modified main.js content ---" | |
| cat frontend/main.js | |
| echo "---------------------------------" | |
| # Build and Push Docker image for Frontend | |
| - name: Build and Push Frontend Image | |
| run: | | |
| docker build -t ${{ secrets.AZURE_CONTAINER_REGISTRY }}/frontend:latest ./frontend/ | |
| docker push ${{ secrets.AZURE_CONTAINER_REGISTRY }}/frontend:latest | |
| - name: Set Kubernetes context (get AKS credentials) | |
| uses: azure/aks-set-context@v3 | |
| with: | |
| resource-group: ${{ inputs.aks_resource_group }} | |
| cluster-name: ${{ inputs.aks_cluster_name }} | |
| - name: Deploy Frontend to AKS | |
| run: | | |
| echo "Deploying frontend with latest tag to AKS cluster: ${{ inputs.aks_cluster_name }}" | |
| cd k8s/ | |
| # Ensure frontend-service.yaml is configured with your ACR | |
| kubectl apply -f frontend.yaml | |
| - name: Logout from Azure (AKS deployment) | |
| run: az logout |