diff --git a/.github/workflows/backend-cd.yml b/.github/workflows/backend-cd.yml index 6035ed15..2c6cddab 100644 --- a/.github/workflows/backend-cd.yml +++ b/.github/workflows/backend-cd.yml @@ -1,29 +1,21 @@ name: CD - Deploy Backend Services to AKS on: - workflow_dispatch: - inputs: - aks_cluster_name: - description: 'Name of the AKS Cluster to deploy to' - required: true - default: '' - aks_resource_group: - description: 'Resource Group of the AKS Cluster' - required: true - default: '' - aks_acr_name: - description: 'Name of ACR' - required: true - default: '' + workflow_dispatch: # still allow manual runs + workflow_run: # auto-trigger after CI completes successfully + workflows: ["Backend CI - Test, Build and Push Images to ACR"] + types: + - completed jobs: deploy_backend: + if: ${{ github.event.workflow_run.conclusion == 'success' }} runs-on: ubuntu-latest environment: Production - + outputs: - PRODUCT_API_IP: ${{ steps.get_product_ip.outputs.external_ip }} - ORDER_API_IP: ${{ steps.get_order_ip.outputs.external_ip }} + PRODUCT_API_IP: ${{ steps.set_product_ip.outputs.external_ip }} + ORDER_API_IP: ${{ steps.set_order_ip.outputs.external_ip }} steps: - name: Checkout repository @@ -37,15 +29,20 @@ jobs: - name: Set Kubernetes context (get AKS credentials) run: | - az aks get-credentials --resource-group ${{ github.event.inputs.aks_resource_group }} --name ${{ github.event.inputs.aks_cluster_name }} --overwrite-existing + az aks get-credentials \ + --resource-group ${{ secrets.AKS_RESOURCE_GROUP }} \ + --name ${{ secrets.AKS_CLUSTER_NAME }} \ + --overwrite-existing - name: Attach ACR run: | - 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 }} + az aks update \ + --name ${{ secrets.AKS_CLUSTER_NAME }} \ + --resource-group ${{ secrets.AKS_RESOURCE_GROUP }} \ + --attach-acr ${{ secrets.AZURE_CONTAINER_REGISTRY_NAME }} - - name: Deploy Backend Infrastructure (Namespace, ConfigMaps, Secrets, Databases) + - name: Deploy Backend Infrastructure run: | - echo "Deploying backend infrastructure..." cd k8s/ kubectl apply -f configmaps.yaml kubectl apply -f secrets.yaml @@ -64,38 +61,39 @@ jobs: echo "Waiting for Product, Order LoadBalancer IPs to be assigned (up to 5 minutes)..." PRODUCT_IP="" ORDER_IP="" - + for i in $(seq 1 60); do echo "Attempt $i/60 to get IPs..." PRODUCT_IP=$(kubectl get service product-service-w08e1 -o jsonpath='{.status.loadBalancer.ingress[0].ip}') ORDER_IP=$(kubectl get service order-service-w08e1 -o jsonpath='{.status.loadBalancer.ingress[0].ip}') if [[ -n "$PRODUCT_IP" && -n "$ORDER_IP" ]]; then - echo "All backend LoadBalancer IPs assigned!" echo "Product Service IP: $PRODUCT_IP" echo "Order Service IP: $ORDER_IP" break fi sleep 5 # Wait 5 seconds before next attempt done - + if [[ -z "$PRODUCT_IP" || -z "$ORDER_IP" ]]; then echo "Error: One or more LoadBalancer IPs not assigned after timeout." exit 1 # Fail the job if IPs are not obtained fi - + # These are environment variables for subsequent steps in the *same job* # And used to set the job outputs echo "PRODUCT_IP=$PRODUCT_IP" >> $GITHUB_ENV echo "ORDER_IP=$ORDER_IP" >> $GITHUB_ENV - - name: Capture Product Service IP for Workflow Output - id: get_product_ip + - name: Set Product IP Output + id: set_product_ip run: echo "external_ip=${{ env.PRODUCT_IP }}" >> $GITHUB_OUTPUT - - name: Capture Order Service IP for Workflow Output - id: get_order_ip + - name: Set Order IP Output + id: set_order_ip run: echo "external_ip=${{ env.ORDER_IP }}" >> $GITHUB_OUTPUT + # Logout from Azure for security (runs even if image push fails) - name: Logout from Azure run: az logout + if: always() diff --git a/.github/workflows/backend_ci.yml b/.github/workflows/backend_ci.yml index d69725aa..9d9a29f8 100644 --- a/.github/workflows/backend_ci.yml +++ b/.github/workflows/backend_ci.yml @@ -1,21 +1,33 @@ # week08/.github/workflows/backend_ci.yml +#this is for trigger the pipeline +#test the workflow 123 +#test workflow + name: Backend CI - Test, Build and Push Images to ACR # Trigger the workflow on pushes to the 'main' branch -# You can also add 'pull_request:' to run on PRs +# 'pull_request:' to run on PRs on: # Manual trigger workflow_dispatch: - # Automatically on pushes to main branch + # Automatically on pushes to development branch push: branches: - - main + - development paths: # Only trigger if changes are in backend directories - 'backend/**' - '.github/workflows/backend_ci.yml' # Trigger if this workflow file changes + pull_request: + branches: + - development + - main + paths: + - 'backend/**' + - '.github/workflows/backend_ci.yml' # Trigger if this workflow file changes + # Define global environment variables that can be used across jobs env: # ACR Login Server (e.g., myregistry.azurecr.io) @@ -143,4 +155,4 @@ jobs: # Logout from Azure for security (runs even if image push fails) - name: Logout from Azure run: az logout - if: always() + if: always() \ No newline at end of file diff --git a/.github/workflows/frontend-cd.yml b/.github/workflows/frontend-cd.yml index 0a0879c8..b25274e4 100644 --- a/.github/workflows/frontend-cd.yml +++ b/.github/workflows/frontend-cd.yml @@ -1,46 +1,19 @@ # 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: + # Allow manual trigger workflow_dispatch: - inputs: - product_api_ip: - description: 'External IP of Product Service' - required: true - default: 'http://:8000' - order_api_ip: - description: 'External IP of Order Service (e.g., http://Y.Y.Y.Y:8001)' - required: true - default: 'http://:8001' - aks_cluster_name: - description: 'Name of the AKS Cluster to deploy to' - required: true - default: '' - aks_resource_group: - description: 'Resource Group of the AKS Cluster' - required: true - default: '<' - 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 + # Automatically run after Frontend CI completes successfully + workflow_run: + workflows: ["CD - Deploy Backend Services to AKS"] + types: + - completed jobs: deploy_frontend: + if: ${{ github.event.workflow_run.conclusion == 'success' }} runs-on: ubuntu-latest environment: Production @@ -48,27 +21,25 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 - # Azure login using a Service Principal secret + # Azure login - name: Azure Login uses: azure/login@v1 with: creds: ${{ secrets.AZURE_CREDENTIALS }} - # Login to Azure Container Registry (ACR) + # Login to Azure Container Registry - name: Login to Azure Container Registry run: az acr login --name ${{ secrets.AZURE_CONTAINER_REGISTRY }} + # Inject Backend IPs into Frontend main.js - 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 ---" + echo "Injecting Product and Order IPs..." + sed -i "s|_PRODUCT_API_URL_|${{ github.event.workflow_run.outputs.PRODUCT_API_IP }}|g" frontend/main.js + sed -i "s|_ORDER_API_URL_|${{ github.event.workflow_run.outputs.ORDER_API_IP }}|g" frontend/main.js + echo "--- Modified main.js ---" cat frontend/main.js - echo "---------------------------------" + echo "------------------------" # Build and Push Docker image for Frontend - name: Build and Push Frontend Image @@ -76,18 +47,20 @@ jobs: docker build -t ${{ secrets.AZURE_CONTAINER_REGISTRY }}/frontend:latest ./frontend/ docker push ${{ secrets.AZURE_CONTAINER_REGISTRY }}/frontend:latest + # Set Kubernetes context (from secrets instead of workflow inputs) - 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 }} + run: | + az aks get-credentials \ + --resource-group ${{ secrets.AKS_RESOURCE_GROUP }} \ + --name ${{ secrets.AKS_CLUSTER_NAME }} \ + --overwrite-existing - 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) + # Logout from Azure for security (runs even if image push fails) + - name: Logout from Azure run: az logout + if: always() diff --git a/.github/workflows/frontend_ci.yml b/.github/workflows/frontend_ci.yml index 9f9e76d9..1f60a2b5 100644 --- a/.github/workflows/frontend_ci.yml +++ b/.github/workflows/frontend_ci.yml @@ -1,53 +1,36 @@ # week08/.github/workflows/frontend_ci.yml - -name: Frontend CI - Build & Push Image +#this is to trigger frontend pipeline +name: Frontend CI - Build Frontend Image on: # Manual trigger workflow_dispatch: - # Automatically on pushes to main branch + # Automatically on pushes to development branch push: branches: - - main - paths: # Only trigger if changes are in the frontend directory + - development + paths: - 'frontend/**' - - '.github/workflows/frontend_ci.yml' # Trigger if this workflow file changes + - '.github/workflows/frontend_ci.yml' -# Define global environment variables that can be used across jobs -env: - # ACR Login Server (e.g., myregistry.azurecr.io) - # This needs to be set as a GitHub Repository Secret - ACR_LOGIN_SERVER: ${{ secrets.AZURE_CONTAINER_REGISTRY }} - # Dynamically generate image tags based on Git SHA and GitHub Run ID - # This provides unique, traceable tags for each image build - IMAGE_TAG: ${{ github.sha }}-${{ github.run_id }} + # Run on pull requests targeting development or main branch + pull_request: + branches: + - development + - main + paths: + - 'frontend/**' + - '.github/workflows/frontend_ci.yml' jobs: - build_and_push_frontend: + build_frontend: runs-on: ubuntu-latest 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 ${{ env.ACR_LOGIN_SERVER }} - - # Build and Push Docker image for Frontend - - name: Build and Push Frontend Image - run: | - docker build -t ${{ env.ACR_LOGIN_SERVER }}/frontend:latest ./frontend/ - docker push ${{ env.ACR_LOGIN_SERVER }}/frontend:latest - - # Logout from Azure for security (runs even if image push fails) - - name: Logout from Azure - run: az logout - if: always() + # Build Docker image only (no push) + - name: Build Frontend Image (validation only) + run: docker build -t frontend-ci-test ./frontend/ diff --git a/frontend/main.js b/frontend/main.js index f321fd91..f6c338b2 100644 --- a/frontend/main.js +++ b/frontend/main.js @@ -1,4 +1,5 @@ // week08/frontend/main.js +// this line to trigger frontend CI document.addEventListener('DOMContentLoaded', () => { // API endpoints for the Product and Order services. diff --git a/k8s/frontend.yaml b/k8s/frontend.yaml index 1948536d..f321e75a 100644 --- a/k8s/frontend.yaml +++ b/k8s/frontend.yaml @@ -18,7 +18,7 @@ spec: spec: containers: - name: frontend-container - image: durgeshsamariya.azurecr.io/frontend:latest + image: pasindufernando9c.azurecr.io/frontend:latest imagePullPolicy: Always ports: - containerPort: 80 diff --git a/k8s/order-service.yaml b/k8s/order-service.yaml index c9d92e4d..fa9e8d30 100644 --- a/k8s/order-service.yaml +++ b/k8s/order-service.yaml @@ -18,7 +18,7 @@ spec: spec: containers: - name: order-service-container - image: durgeshsamariya.azurecr.io/order_service:latest + image: pasindufernando9c.azurecr.io/order_service:latest imagePullPolicy: Always ports: - containerPort: 8000 diff --git a/k8s/product-service.yaml b/k8s/product-service.yaml index 0cbbd505..6cd19b9e 100644 --- a/k8s/product-service.yaml +++ b/k8s/product-service.yaml @@ -18,7 +18,7 @@ spec: spec: containers: - name: product-service-container - image: durgeshsamariya.azurecr.io/product_service:latest + image: pasindufernando9c.azurecr.io/product_service:latest imagePullPolicy: Always ports: - containerPort: 8000 diff --git a/k8s/secrets.yaml b/k8s/secrets.yaml index 5eebe1fa..cde2fec0 100644 --- a/k8s/secrets.yaml +++ b/k8s/secrets.yaml @@ -13,6 +13,6 @@ data: # Azure Storage Account Credentials for Product Service image uploads # REPLACE WITH YOUR ACTUAL BASE64 ENCODED VALUES from your Azure Storage Account # Example: echo -n 'myblobstorageaccount' | base64 - AZURE_STORAGE_ACCOUNT_NAME: "ZHVyZ2VzaHNhbWFyaXlh" + AZURE_STORAGE_ACCOUNT_NAME: "cGFzaW5kdWZlcm5hbmRvOWM=" # Example: echo -n 'your_storage_account_key_string' | base64 - AZURE_STORAGE_ACCOUNT_KEY: "aEFNQ24rbkh2cmhwSGFEaW5jSnAxNFlHaU5nTnJja2NJR05Bc3Y5VXZPUlpsblJkbkVUR3drdTREdSszblBDR3E4ZEVTVjlFNE1jMytBU3RubmZ5QVE9PQ==" + AZURE_STORAGE_ACCOUNT_KEY: "L1RDWUt3VWIxM1N0TkVnelgrMFpVMkIxcStNMDVRNXN6YnRhU3VVMS9qTVVhbGtIam5uZ2Zwc01JdVpZdFEzUFYwazdNVHgzb21SNCtBU3RzQTBqTHc9PQ=="