Skip to content

Deploy to Kubernetes #2

Deploy to Kubernetes

Deploy to Kubernetes #2

Workflow file for this run

name: Deploy to Kubernetes
on:
push:
branches:
- main
workflow_dispatch:
env:
REGISTRY: crretoxmas2024.azurecr.io
NAMESPACE: reto-xmas-2025-goland-ia-backend
DEPLOY_TIMEOUT: 3m
jobs:
build-and-deploy:
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
fail-fast: false
matrix:
service:
- name: docs-manager
path: ./DocsManager
image: reto-xmas-2025-goland-ia-backend-docs-manager
deployment: docs-manager
- name: rag-manager
path: ./RAGManager
image: reto-xmas-2025-goland-ia-backend-rag-manager
deployment: rag-manager
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to ACR
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.ACR_USERNAME }}
password: ${{ secrets.ACR_PASSWORD }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: ${{ matrix.service.path }}
platforms: linux/amd64
push: true
tags: |
${{ env.REGISTRY }}/${{ matrix.service.image }}:latest
${{ env.REGISTRY }}/${{ matrix.service.image }}:${{ github.sha }}
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ matrix.service.image }}:buildcache
cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ matrix.service.image }}:buildcache,mode=max
provenance: false
- name: Set up kubectl
uses: azure/setup-kubectl@v3
with:
version: 'latest'
- name: Configure kubectl
run: |
mkdir -p $HOME/.kube
echo "${{ secrets.KUBECONFIG }}" | base64 -d > $HOME/.kube/config
chmod 600 $HOME/.kube/config
- name: Update deployment image
run: |
kubectl set image deployment/${{ matrix.service.deployment }} \
api=${{ env.REGISTRY }}/${{ matrix.service.image }}:${{ github.sha }} \
-n ${{ env.NAMESPACE }}
- name: Wait for rollout
timeout-minutes: 5
run: |
kubectl rollout status deployment/${{ matrix.service.deployment }} \
-n ${{ env.NAMESPACE }} \
--timeout=${{ env.DEPLOY_TIMEOUT }} || {
echo "Deployment failed or timed out"
kubectl get pods -n ${{ env.NAMESPACE }} -l app=${{ matrix.service.deployment }}
exit 1
}
- name: Verify deployment
if: success()
run: |
echo "Deployment successful for ${{ matrix.service.name }}"
kubectl get pods -n ${{ env.NAMESPACE }} -l app=${{ matrix.service.deployment }}
- name: Get logs on failure
if: failure()
run: |
echo "=== Pod Logs ==="
kubectl logs -n ${{ env.NAMESPACE }} \
-l app=${{ matrix.service.deployment }} \
--tail=100 \
--all-containers=true \
--prefix=true || echo "Could not fetch logs"
- name: Rollback on failure
if: failure()
run: |
echo "Rolling back deployment"
kubectl rollout undo deployment/${{ matrix.service.deployment }} -n ${{ env.NAMESPACE }}
- name: Deployment Summary
if: always()
run: |
STATUS="${{ job.status }}"
echo "### Deployment - ${{ matrix.service.name }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "| Property | Value |" >> $GITHUB_STEP_SUMMARY
echo "|----------|-------|" >> $GITHUB_STEP_SUMMARY
echo "| Image | \`${{ env.REGISTRY }}/${{ matrix.service.image }}:${{ github.sha }}\` |" >> $GITHUB_STEP_SUMMARY
if [ "$STATUS" == "success" ]; then
echo "| Status | Success |" >> $GITHUB_STEP_SUMMARY
else
echo "| Status | Failed |" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "#### Pods:" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
kubectl get pods -n ${{ env.NAMESPACE }} -l app=${{ matrix.service.deployment }} >> $GITHUB_STEP_SUMMARY || true
echo '```' >> $GITHUB_STEP_SUMMARY
notify-success:
name: Deployment Success
runs-on: ubuntu-latest
needs: [build-and-deploy]
if: success()
steps:
- name: Success Summary
run: |
echo "### ll Services Deployed!" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Live URLs:**" >> $GITHUB_STEP_SUMMARY
echo "- [DocsManager](https://goland-ia-backend-docs-manager.reto-ucu.net/docs)" >> $GITHUB_STEP_SUMMARY
echo "- [RAGManager](https://goland-ia-backend-rag-manager.reto-ucu.net/docs)" >> $GITHUB_STEP_SUMMARY
notify-failure:
name: Deployment Failed
runs-on: ubuntu-latest
needs: [build-and-deploy]
if: failure()
steps:
- name: Failure Summary
run: |
echo "### Deployment Failed" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "Automatic rollback initiated" >> $GITHUB_STEP_SUMMARY