From 7c21fdb431cae7cae90d872bf1773ddd08216a07 Mon Sep 17 00:00:00 2001 From: RandithaK Date: Sat, 15 Nov 2025 03:27:44 +0530 Subject: [PATCH 1/3] chore: migrate to GitOps workflow with ArgoCD - Update build.yaml with branch-aware image tagging (branch-sha format) - Add update-manifest.yaml to update k8s-config manifests - Backup old deploy.yaml (no longer needed with GitOps) Refs: - k8s-config/argocd/GITOPS_CI_CD_WORKFLOW.md - k8s-config/argocd/SERVICE_MIGRATION_GUIDE.md --- .github/workflows/build.yaml | 128 +++++++++++++++---------- .github/workflows/deploy.yaml.old | 66 +++++++++++++ .github/workflows/update-manifest.yaml | 88 +++++++++++++++++ 3 files changed, 233 insertions(+), 49 deletions(-) create mode 100644 .github/workflows/deploy.yaml.old create mode 100644 .github/workflows/update-manifest.yaml diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 886ca85..f6e317b 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -1,18 +1,16 @@ -# Frontend_Web/.github/workflows/build.yaml -# This workflow builds, tests, AND packages the Next.js Frontend +# Updated build.yaml template for microservices +# This replaces the old build.yaml to add branch-aware image tagging -name: Build, Test, and Package Frontend +name: Build and Package Service on: push: branches: - 'main' - - 'devOps' - 'dev' pull_request: branches: - 'main' - - 'devOps' - 'dev' permissions: @@ -20,74 +18,93 @@ permissions: packages: write jobs: - # --- JOB 1: Build and Test --- - # This runs on all pushes AND all pull requests to verify the code - build-and-test: - name: Build and Test Frontend + # JOB 1: Build and test (runs on all pushes and PRs) + build-test: + name: Build and Test runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 - - name: Use Node.js 22 and cache npm - uses: actions/setup-node@v4 - with: - node-version: '22' - cache: 'npm' - cache-dependency-path: package-lock.json +# # For Java/Spring Boot services: +# - name: Set up JDK 17 +# uses: actions/setup-java@v4 +# with: +# java-version: '17' +# distribution: 'temurin' +# cache: maven - - name: Cache Next.js build artifacts - uses: actions/cache@v4 - with: - path: ./.next/cache - key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**/*.{js,jsx,ts,tsx,css,html}') }} - restore-keys: | - ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}- +# - name: Cache Maven packages +# uses: actions/cache@v4 +# with: +# path: ~/.m2/repository +# key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} +# restore-keys: | +# ${{ runner.os }}-maven- - - name: Install dependencies - run: npm ci +# - name: Build with Maven +# run: mvn -B clean package -DskipTests --file SERVICE_MODULE/pom.xml - - name: Run linter - run: npm run lint +# - name: Upload Build Artifact +# uses: actions/upload-artifact@v4 +# with: +# name: service-jar +# path: SERVICE_MODULE/target/*.jar - - name: Build production bundle - run: npm run build +# # For Node.js/Next.js services (Frontend): + # - name: Use Node.js and cache npm + # uses: actions/setup-node@v4 + # with: + # node-version: '22' + # cache: 'npm' + # + # - name: Install dependencies + # run: npm ci + # + # - name: Run linter + # run: npm run lint + # + # - name: Build + # run: npm run build - - name: Run a minimal start for smoke test (background) - run: | - npm run start & - sleep 5 - # Quick HTTP probe - if command -v curl >/dev/null 2>&1; then - curl -sSf http://localhost:3000/ || true - fi - pkill -f "next start" || true - shell: bash - - # --- JOB 2: Package as Docker Image --- - # This runs ONLY after Job 1 succeeds, AND - # This runs ONLY on pushes to your main branches (not PRs) + # JOB 2: Package as Docker image (only on pushes to main/dev, not PRs) build-and-push-docker: name: Build & Push Docker Image - needs: build-and-test - - # Only run on pushes to main branches - if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/devOps' || github.ref == 'refs/heads/dev') + needs: build-test + if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dev') runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 + + # For Java services: download JAR from previous job + - name: Download JAR Artifact + uses: actions/download-artifact@v4 + with: + name: service-jar + path: SERVICE_MODULE/target/ - - name: Docker meta + - name: Extract branch name + id: branch + run: | + BRANCH_NAME=${GITHUB_REF#refs/heads/} + echo "name=${BRANCH_NAME}" >> $GITHUB_OUTPUT + echo "📍 Building for branch: ${BRANCH_NAME}" + + - name: Docker meta (with branch-aware tags) id: meta uses: docker/metadata-action@v5 with: images: ghcr.io/techtorque-2025/frontend_web tags: | - type=sha,prefix= + # Branch + short SHA (e.g., dev-abc1234 or main-xyz5678) + type=raw,value=${{ steps.branch.outputs.name }}-{{sha}},enable=true + # Latest tag only for main branch type=raw,value=latest,enable={{is_default_branch}} + flavor: | + latest=false - name: Log in to GHCR uses: docker/login-action@v3 @@ -100,7 +117,20 @@ jobs: uses: docker/build-push-action@v5 with: context: . - file: ./Dockerfile push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + + - name: Image Summary + run: | + echo "### 🐳 Docker Image Built" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "**Tags pushed:**" >> $GITHUB_STEP_SUMMARY + echo '```' >> $GITHUB_STEP_SUMMARY + echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY + echo '```' >> $GITHUB_STEP_SUMMARY + +# REPLACEMENTS NEEDED: +# - SERVICE_MODULE: e.g., "auth-service", "time-logging-service" (for Java services) +# - frontend_web: e.g., "authentication", "timelogging_service", "frontend_web" +# - Uncomment Node.js steps for Frontend_Web diff --git a/.github/workflows/deploy.yaml.old b/.github/workflows/deploy.yaml.old new file mode 100644 index 0000000..56e6b43 --- /dev/null +++ b/.github/workflows/deploy.yaml.old @@ -0,0 +1,66 @@ +# Frontend_Web/.github/workflows/deploy.yaml + +name: Deploy Frontend to Kubernetes + +on: + workflow_run: + workflows: ["Build, Test, and Package Frontend"] + types: + - completed + branches: + - 'main' + - 'devOps' + +jobs: + deploy: + name: Deploy Frontend to Kubernetes + if: ${{ github.event.workflow_run.conclusion == 'success' }} + runs-on: ubuntu-latest + + steps: + - name: Get Commit SHA + id: get_sha + run: | + echo "sha=$(echo ${{ github.event.workflow_run.head_sha }} | cut -c1-7)" >> $GITHUB_OUTPUT + + - name: Checkout K8s Config Repo + uses: actions/checkout@v4 + with: + repository: 'TechTorque-2025/k8s-config' + token: ${{ secrets.REPO_ACCESS_TOKEN }} + path: 'config-repo' + ref: 'main' + + - name: Install kubectl + uses: azure/setup-kubectl@v3 + + - name: Install yq + run: | + sudo wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/bin/yq + sudo chmod +x /usr/bin/yq + + - name: Set Kubernetes context + uses: azure/k8s-set-context@v4 + with: + kubeconfig: ${{ secrets.KUBE_CONFIG_DATA }} + + - name: Update image tag in YAML + run: | + yq -i '(select(.kind == "Deployment") | .spec.template.spec.containers[0].image) = "ghcr.io/techtorque-2025/frontend_web:${{ steps.get_sha.outputs.sha }}"' config-repo/k8s/services/frontend-deployment.yaml + + - name: Display file contents before apply + run: | + echo "--- Displaying k8s/services/frontend-deployment.yaml ---" + cat config-repo/k8s/services/frontend-deployment.yaml + echo "------------------------------------------------------" + + - name: Deploy to Kubernetes + run: | + kubectl apply -f config-repo/k8s/configmaps/frontend-configmap.yaml + kubectl apply -f config-repo/k8s/services/frontend-deployment.yaml + kubectl rollout status deployment/frontend-deployment + + - name: Verify deployment + run: | + kubectl get pods -l app=frontend + kubectl get svc frontend-service diff --git a/.github/workflows/update-manifest.yaml b/.github/workflows/update-manifest.yaml new file mode 100644 index 0000000..f0df949 --- /dev/null +++ b/.github/workflows/update-manifest.yaml @@ -0,0 +1,88 @@ +# GitHub Actions Workflow Template for GitOps with ArgoCD +# This workflow should replace the old deploy.yaml in each microservice repo + +name: Update K8s Manifest + +on: + workflow_run: + workflows: ["Build, Test, and Package Frontend"] # Or "Build, Test, and Package Frontend" for Frontend_Web + types: [completed] + branches: ['main', 'dev'] + +jobs: + update-manifest: + name: Update Image Tag in k8s-config + if: ${{ github.event.workflow_run.conclusion == 'success' }} + runs-on: ubuntu-latest + + steps: + - name: Get branch and SHA info + id: info + run: | + BRANCH="${{ github.event.workflow_run.head_branch }}" + SHORT_SHA="$(echo ${{ github.event.workflow_run.head_sha }} | cut -c1-7)" + echo "branch=${BRANCH}" >> $GITHUB_OUTPUT + echo "sha=${SHORT_SHA}" >> $GITHUB_OUTPUT + echo "📍 Branch: ${BRANCH}, SHA: ${SHORT_SHA}" + + - name: Checkout k8s-config repo (matching branch) + uses: actions/checkout@v4 + with: + repository: 'TechTorque-2025/k8s-config' + token: ${{ secrets.REPO_ACCESS_TOKEN }} + ref: ${{ steps.info.outputs.branch }} # Checkout dev or main to match microservice branch + path: 'k8s-config' + + - name: Install yq (YAML processor) + run: | + sudo wget -qO /usr/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 + sudo chmod +x /usr/bin/yq + + - name: Update image tag in deployment manifest + env: + SERVICE_NAME: "frontend_web" # e.g., "timelogging_service", "frontend_web", "authentication" + DEPLOYMENT_FILE: "frontend-deployment.yaml" # e.g., "timelogging-deployment.yaml", "frontend-deployment.yaml" + run: | + cd k8s-config + NEW_IMAGE="ghcr.io/techtorque-2025/${SERVICE_NAME}:${{ steps.info.outputs.branch }}-${{ steps.info.outputs.sha }}" + + echo "🔄 Updating ${DEPLOYMENT_FILE} to use image: ${NEW_IMAGE}" + + yq eval -i \ + '(select(.kind == "Deployment") | .spec.template.spec.containers[0].image) = env(NEW_IMAGE)' \ + k8s/services/${DEPLOYMENT_FILE} + + echo "✅ Updated manifest:" + yq eval 'select(.kind == "Deployment") | .spec.template.spec.containers[0].image' k8s/services/${DEPLOYMENT_FILE} + + - name: Commit and push changes + env: + SERVICE_NAME: "frontend_web" + run: | + cd k8s-config + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + + git add k8s/services/ + + if git diff --cached --quiet; then + echo "⚠️ No changes detected, skipping commit" + exit 0 + fi + + git commit -m "chore(${SERVICE_NAME}): update image to ${{ steps.info.outputs.branch }}-${{ steps.info.outputs.sha }}" \ + -m "Triggered by: ${{ github.event.workflow_run.html_url }}" + + git push origin ${{ steps.info.outputs.branch }} + + echo "✅ Pushed manifest update to k8s-config/${{ steps.info.outputs.branch }}" + echo "🚀 ArgoCD will automatically deploy this change" + + - name: Summary + run: | + echo "### 🎉 Manifest Update Complete" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "- **Branch**: ${{ steps.info.outputs.branch }}" >> $GITHUB_STEP_SUMMARY + echo "- **Image Tag**: ${{ steps.info.outputs.branch }}-${{ steps.info.outputs.sha }}" >> $GITHUB_STEP_SUMMARY + echo "- **Manifest Updated**: k8s/services/frontend-deployment.yaml" >> $GITHUB_STEP_SUMMARY + echo "- **Next Step**: ArgoCD will sync this change to the cluster" >> $GITHUB_STEP_SUMMARY From cd8e7929cd477c0aa67c94063799a7b0406ac939 Mon Sep 17 00:00:00 2001 From: RandithaK Date: Sat, 15 Nov 2025 04:33:04 +0530 Subject: [PATCH 2/3] fix: use export for NEW_IMAGE variable in yq command (fixes --arg incompatibility) --- .github/workflows/update-manifest.yaml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/update-manifest.yaml b/.github/workflows/update-manifest.yaml index f0df949..378bd6c 100644 --- a/.github/workflows/update-manifest.yaml +++ b/.github/workflows/update-manifest.yaml @@ -45,13 +45,14 @@ jobs: run: | cd k8s-config NEW_IMAGE="ghcr.io/techtorque-2025/${SERVICE_NAME}:${{ steps.info.outputs.branch }}-${{ steps.info.outputs.sha }}" - + export NEW_IMAGE + echo "🔄 Updating ${DEPLOYMENT_FILE} to use image: ${NEW_IMAGE}" - + yq eval -i \ '(select(.kind == "Deployment") | .spec.template.spec.containers[0].image) = env(NEW_IMAGE)' \ k8s/services/${DEPLOYMENT_FILE} - + echo "✅ Updated manifest:" yq eval 'select(.kind == "Deployment") | .spec.template.spec.containers[0].image' k8s/services/${DEPLOYMENT_FILE} From a0c0e0eccdbc59236262d05e596bab1e29abc947 Mon Sep 17 00:00:00 2001 From: RandithaK Date: Sat, 15 Nov 2025 13:37:21 +0530 Subject: [PATCH 3/3] chore: refactor CI/CD workflows for improved build and deployment process --- .../{buildtest.yaml => build-test.yaml} | 8 +- .github/workflows/build.yaml | 82 +------------------ .github/workflows/deploy.yaml | 66 --------------- .github/workflows/update-manifest.yaml | 2 +- 4 files changed, 8 insertions(+), 150 deletions(-) rename .github/workflows/{buildtest.yaml => build-test.yaml} (93%) delete mode 100644 .github/workflows/deploy.yaml diff --git a/.github/workflows/buildtest.yaml b/.github/workflows/build-test.yaml similarity index 93% rename from .github/workflows/buildtest.yaml rename to .github/workflows/build-test.yaml index a96f6ef..8170fa2 100644 --- a/.github/workflows/buildtest.yaml +++ b/.github/workflows/build-test.yaml @@ -1,12 +1,10 @@ -name: Build and Test Frontend_Web +name: Build and Test on: - push: - branches: - - '**' pull_request: branches: - - '**' + - 'main' + - 'dev' jobs: build-test: diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index f6e317b..714feeb 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -1,90 +1,23 @@ -# Updated build.yaml template for microservices -# This replaces the old build.yaml to add branch-aware image tagging - -name: Build and Package Service +name: Build and Push Docker Image on: push: branches: - 'main' - 'dev' - pull_request: - branches: - - 'main' - - 'dev' permissions: contents: read packages: write jobs: - # JOB 1: Build and test (runs on all pushes and PRs) - build-test: - name: Build and Test - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v4 - -# # For Java/Spring Boot services: -# - name: Set up JDK 17 -# uses: actions/setup-java@v4 -# with: -# java-version: '17' -# distribution: 'temurin' -# cache: maven - -# - name: Cache Maven packages -# uses: actions/cache@v4 -# with: -# path: ~/.m2/repository -# key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} -# restore-keys: | -# ${{ runner.os }}-maven- - -# - name: Build with Maven -# run: mvn -B clean package -DskipTests --file SERVICE_MODULE/pom.xml - -# - name: Upload Build Artifact -# uses: actions/upload-artifact@v4 -# with: -# name: service-jar -# path: SERVICE_MODULE/target/*.jar - -# # For Node.js/Next.js services (Frontend): - # - name: Use Node.js and cache npm - # uses: actions/setup-node@v4 - # with: - # node-version: '22' - # cache: 'npm' - # - # - name: Install dependencies - # run: npm ci - # - # - name: Run linter - # run: npm run lint - # - # - name: Build - # run: npm run build - - # JOB 2: Package as Docker image (only on pushes to main/dev, not PRs) - build-and-push-docker: + build-and-push: name: Build & Push Docker Image - needs: build-test - if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dev') runs-on: ubuntu-latest - + steps: - name: Checkout code uses: actions/checkout@v4 - - # For Java services: download JAR from previous job - - name: Download JAR Artifact - uses: actions/download-artifact@v4 - with: - name: service-jar - path: SERVICE_MODULE/target/ - name: Extract branch name id: branch @@ -99,9 +32,7 @@ jobs: with: images: ghcr.io/techtorque-2025/frontend_web tags: | - # Branch + short SHA (e.g., dev-abc1234 or main-xyz5678) type=raw,value=${{ steps.branch.outputs.name }}-{{sha}},enable=true - # Latest tag only for main branch type=raw,value=latest,enable={{is_default_branch}} flavor: | latest=false @@ -120,7 +51,7 @@ jobs: push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} - + - name: Image Summary run: | echo "### 🐳 Docker Image Built" >> $GITHUB_STEP_SUMMARY @@ -129,8 +60,3 @@ jobs: echo '```' >> $GITHUB_STEP_SUMMARY echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY echo '```' >> $GITHUB_STEP_SUMMARY - -# REPLACEMENTS NEEDED: -# - SERVICE_MODULE: e.g., "auth-service", "time-logging-service" (for Java services) -# - frontend_web: e.g., "authentication", "timelogging_service", "frontend_web" -# - Uncomment Node.js steps for Frontend_Web diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml deleted file mode 100644 index 56e6b43..0000000 --- a/.github/workflows/deploy.yaml +++ /dev/null @@ -1,66 +0,0 @@ -# Frontend_Web/.github/workflows/deploy.yaml - -name: Deploy Frontend to Kubernetes - -on: - workflow_run: - workflows: ["Build, Test, and Package Frontend"] - types: - - completed - branches: - - 'main' - - 'devOps' - -jobs: - deploy: - name: Deploy Frontend to Kubernetes - if: ${{ github.event.workflow_run.conclusion == 'success' }} - runs-on: ubuntu-latest - - steps: - - name: Get Commit SHA - id: get_sha - run: | - echo "sha=$(echo ${{ github.event.workflow_run.head_sha }} | cut -c1-7)" >> $GITHUB_OUTPUT - - - name: Checkout K8s Config Repo - uses: actions/checkout@v4 - with: - repository: 'TechTorque-2025/k8s-config' - token: ${{ secrets.REPO_ACCESS_TOKEN }} - path: 'config-repo' - ref: 'main' - - - name: Install kubectl - uses: azure/setup-kubectl@v3 - - - name: Install yq - run: | - sudo wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/bin/yq - sudo chmod +x /usr/bin/yq - - - name: Set Kubernetes context - uses: azure/k8s-set-context@v4 - with: - kubeconfig: ${{ secrets.KUBE_CONFIG_DATA }} - - - name: Update image tag in YAML - run: | - yq -i '(select(.kind == "Deployment") | .spec.template.spec.containers[0].image) = "ghcr.io/techtorque-2025/frontend_web:${{ steps.get_sha.outputs.sha }}"' config-repo/k8s/services/frontend-deployment.yaml - - - name: Display file contents before apply - run: | - echo "--- Displaying k8s/services/frontend-deployment.yaml ---" - cat config-repo/k8s/services/frontend-deployment.yaml - echo "------------------------------------------------------" - - - name: Deploy to Kubernetes - run: | - kubectl apply -f config-repo/k8s/configmaps/frontend-configmap.yaml - kubectl apply -f config-repo/k8s/services/frontend-deployment.yaml - kubectl rollout status deployment/frontend-deployment - - - name: Verify deployment - run: | - kubectl get pods -l app=frontend - kubectl get svc frontend-service diff --git a/.github/workflows/update-manifest.yaml b/.github/workflows/update-manifest.yaml index 378bd6c..ac13c77 100644 --- a/.github/workflows/update-manifest.yaml +++ b/.github/workflows/update-manifest.yaml @@ -5,7 +5,7 @@ name: Update K8s Manifest on: workflow_run: - workflows: ["Build, Test, and Package Frontend"] # Or "Build, Test, and Package Frontend" for Frontend_Web + workflows: ["Build and Push Docker Image"] types: [completed] branches: ['main', 'dev']