|
| 1 | +# GitHub Actions Workflow Template for GitOps with ArgoCD |
| 2 | +# This workflow should replace the old deploy.yaml in each microservice repo |
| 3 | + |
| 4 | +name: Update K8s Manifest |
| 5 | + |
| 6 | +on: |
| 7 | + workflow_run: |
| 8 | + workflows: ["Build and Push Docker Image"] # Or "Build, Test, and Package Frontend" for Frontend_Web |
| 9 | + types: [completed] |
| 10 | + branches: ['main', 'dev'] |
| 11 | + |
| 12 | +jobs: |
| 13 | + update-manifest: |
| 14 | + name: Update Image Tag in k8s-config |
| 15 | + if: ${{ github.event.workflow_run.conclusion == 'success' }} |
| 16 | + runs-on: ubuntu-latest |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: Get branch and SHA info |
| 20 | + id: info |
| 21 | + run: | |
| 22 | + BRANCH="${{ github.event.workflow_run.head_branch }}" |
| 23 | + SHORT_SHA="$(echo ${{ github.event.workflow_run.head_sha }} | cut -c1-7)" |
| 24 | + echo "branch=${BRANCH}" >> $GITHUB_OUTPUT |
| 25 | + echo "sha=${SHORT_SHA}" >> $GITHUB_OUTPUT |
| 26 | + echo "📍 Branch: ${BRANCH}, SHA: ${SHORT_SHA}" |
| 27 | + |
| 28 | + - name: Checkout k8s-config repo (matching branch) |
| 29 | + uses: actions/checkout@v4 |
| 30 | + with: |
| 31 | + repository: 'TechTorque-2025/k8s-config' |
| 32 | + token: ${{ secrets.REPO_ACCESS_TOKEN }} |
| 33 | + ref: ${{ steps.info.outputs.branch }} # Checkout dev or main to match microservice branch |
| 34 | + path: 'k8s-config' |
| 35 | + |
| 36 | + - name: Install yq (YAML processor) |
| 37 | + run: | |
| 38 | + sudo wget -qO /usr/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 |
| 39 | + sudo chmod +x /usr/bin/yq |
| 40 | + |
| 41 | + - name: Update image tag in deployment manifest |
| 42 | + env: |
| 43 | + SERVICE_NAME: "time_logging_service" # e.g., "timelogging_service", "frontend_web", "authentication" |
| 44 | + DEPLOYMENT_FILE: "timelogging-deployment.yaml" # e.g., "timelogging-deployment.yaml", "frontend-deployment.yaml" |
| 45 | + run: | |
| 46 | + cd k8s-config |
| 47 | + NEW_IMAGE="ghcr.io/techtorque-2025/${SERVICE_NAME}:${{ steps.info.outputs.branch }}-${{ steps.info.outputs.sha }}" |
| 48 | + export NEW_IMAGE |
| 49 | +
|
| 50 | + echo "🔄 Updating ${DEPLOYMENT_FILE} to use image: ${NEW_IMAGE}" |
| 51 | +
|
| 52 | + yq eval -i \ |
| 53 | + '(select(.kind == "Deployment") | .spec.template.spec.containers[0].image) = env(NEW_IMAGE)' \ |
| 54 | + k8s/services/${DEPLOYMENT_FILE} |
| 55 | +
|
| 56 | + echo "✅ Updated manifest:" |
| 57 | + yq eval 'select(.kind == "Deployment") | .spec.template.spec.containers[0].image' k8s/services/${DEPLOYMENT_FILE} |
| 58 | + |
| 59 | + - name: Commit and push changes |
| 60 | + env: |
| 61 | + SERVICE_NAME: "timelogging_service" |
| 62 | + run: | |
| 63 | + cd k8s-config |
| 64 | + git config user.name "github-actions[bot]" |
| 65 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 66 | + |
| 67 | + git add k8s/services/ |
| 68 | + |
| 69 | + if git diff --cached --quiet; then |
| 70 | + echo "⚠️ No changes detected, skipping commit" |
| 71 | + exit 0 |
| 72 | + fi |
| 73 | + |
| 74 | + git commit -m "chore(${SERVICE_NAME}): update image to ${{ steps.info.outputs.branch }}-${{ steps.info.outputs.sha }}" \ |
| 75 | + -m "Triggered by: ${{ github.event.workflow_run.html_url }}" |
| 76 | + |
| 77 | + git push origin ${{ steps.info.outputs.branch }} |
| 78 | + |
| 79 | + echo "✅ Pushed manifest update to k8s-config/${{ steps.info.outputs.branch }}" |
| 80 | + echo "🚀 ArgoCD will automatically deploy this change" |
| 81 | + |
| 82 | + - name: Summary |
| 83 | + run: | |
| 84 | + echo "### 🎉 Manifest Update Complete" >> $GITHUB_STEP_SUMMARY |
| 85 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 86 | + echo "- **Branch**: ${{ steps.info.outputs.branch }}" >> $GITHUB_STEP_SUMMARY |
| 87 | + echo "- **Image Tag**: ${{ steps.info.outputs.branch }}-${{ steps.info.outputs.sha }}" >> $GITHUB_STEP_SUMMARY |
| 88 | + echo "- **Manifest Updated**: k8s/services/timelogging-deployment.yaml" >> $GITHUB_STEP_SUMMARY |
| 89 | + echo "- **Next Step**: ArgoCD will sync this change to the cluster" >> $GITHUB_STEP_SUMMARY |
0 commit comments