|
| 1 | +# ============================================================================ |
| 2 | +# Build and Deploy to Kubernetes Workflow |
| 3 | +# ============================================================================ |
| 4 | +# |
| 5 | +# This workflow builds Docker images for all microservices and deploys them |
| 6 | +# to a Kubernetes cluster using Helm charts with proper secret management. |
| 7 | +# |
| 8 | +# TRIGGERS: |
| 9 | +# - Manual trigger (workflow_dispatch): Run manually from GitHub Actions UI |
| 10 | +# - Push to dev/main branches: Automatically builds and deploys on code changes |
| 11 | +# |
| 12 | +# SERVICES BUILT: |
| 13 | +# - client: Vue.js frontend application |
| 14 | +# - auth-service: Spring Boot authentication service with JWT |
| 15 | +# - quiz-service: Spring Boot quiz management service |
| 16 | +# - flashcard-service: Spring Boot flashcard management service |
| 17 | +# - genai-service: FastAPI service for AI-powered features |
| 18 | +# |
| 19 | +# DEPLOYMENT TARGET: |
| 20 | +# - Kubernetes cluster with Helm |
| 21 | +# - Namespace: dev |
| 22 | +# - Domain: team-deployaas.student.k8s.aet.cit.tum.de |
| 23 | +# |
| 24 | +# REQUIRED SECRETS: |
| 25 | +# - POSTGRES_PASSWORD: Database password |
| 26 | +# - JWT_SECRET: JWT signing secret (32+ characters) |
| 27 | +# - GENAI_API_KEY: API key for GenAI service |
| 28 | +# - GRAFANA_PASSWORD: Grafana admin password |
| 29 | +# - KUBE_CONFIG: Base64-encoded Kubernetes config file |
| 30 | +# |
| 31 | +# SWAGGER ENDPOINTS (after deployment): |
| 32 | +# - Auth Service: https://team-deployaas.student.k8s.aet.cit.tum.de/auth/swagger-ui |
| 33 | +# - Quiz Service: https://team-deployaas.student.k8s.aet.cit.tum.de/quiz/swagger-ui |
| 34 | +# - Flashcard Service: https://team-deployaas.student.k8s.aet.cit.tum.de/flashcard/swagger-ui |
| 35 | +# - GenAI Service: https://team-deployaas.student.k8s.aet.tum.de/api/genai/docs |
| 36 | +# ============================================================================ |
| 37 | + |
| 38 | +name: Build and Deploy to Kubernetes |
| 39 | + |
| 40 | +on: |
| 41 | + # Manual trigger - can be run from GitHub Actions UI |
| 42 | + workflow_dispatch: |
| 43 | + # Automatic trigger on push to main development branches |
| 44 | + push: |
| 45 | + branches: [dev, main] |
| 46 | + |
| 47 | +jobs: |
| 48 | + # ============================================================================ |
| 49 | + # BUILD JOB: Build and push Docker images for all microservices |
| 50 | + # ============================================================================ |
| 51 | + build: |
| 52 | + name: Build & Push Docker Images |
| 53 | + runs-on: ubuntu-latest |
| 54 | + |
| 55 | + # Matrix strategy builds all services in parallel for efficiency |
| 56 | + strategy: |
| 57 | + matrix: |
| 58 | + include: |
| 59 | + # Vue.js frontend with Nginx |
| 60 | + - service: client |
| 61 | + context: frontend/client-vue |
| 62 | + dockerfile: frontend/client-vue/Dockerfile |
| 63 | + # Spring Boot authentication service |
| 64 | + - service: auth-service |
| 65 | + context: auth-service |
| 66 | + dockerfile: auth-service/Dockerfile |
| 67 | + # Spring Boot quiz management service |
| 68 | + - service: quiz-service |
| 69 | + context: quiz-service |
| 70 | + dockerfile: quiz-service/Dockerfile |
| 71 | + # Spring Boot flashcard management service |
| 72 | + - service: flashcard-service |
| 73 | + context: flashcard-service |
| 74 | + dockerfile: flashcard-service/Dockerfile |
| 75 | + # FastAPI AI service |
| 76 | + - service: genai-service |
| 77 | + context: genai-service |
| 78 | + dockerfile: genai-service/Dockerfile |
| 79 | + |
| 80 | + steps: |
| 81 | + # Checkout source code |
| 82 | + - uses: actions/checkout@v4 |
| 83 | + |
| 84 | + # Login to GitHub Container Registry using automatic token |
| 85 | + - name: Log in to GitHub Container Registry |
| 86 | + uses: docker/login-action@v3 |
| 87 | + with: |
| 88 | + registry: ghcr.io |
| 89 | + username: ${{ github.actor }} |
| 90 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 91 | + |
| 92 | + # Set up Docker Buildx for multi-platform builds |
| 93 | + - uses: docker/setup-buildx-action@v3 |
| 94 | + |
| 95 | + # Generate image metadata (tags, labels) |
| 96 | + - id: meta |
| 97 | + uses: docker/metadata-action@v5 |
| 98 | + with: |
| 99 | + images: ghcr.io/${{ github.repository }}/${{ matrix.service }} |
| 100 | + tags: | |
| 101 | + type=raw,value=latest # Always tag as 'latest' |
| 102 | + type=ref,event=branch # Tag with branch name |
| 103 | + type=sha # Tag with git commit SHA |
| 104 | +
|
| 105 | + # Build and push Docker image for current service |
| 106 | + - name: Build & Push ${{ matrix.service }} |
| 107 | + uses: docker/build-push-action@v5 |
| 108 | + with: |
| 109 | + context: ./${{ matrix.context }} # Build context (service directory) |
| 110 | + file: ./${{ matrix.dockerfile }} # Dockerfile path |
| 111 | + push: true # Push to registry |
| 112 | + tags: ${{ steps.meta.outputs.tags }} # Use generated tags |
| 113 | + labels: ${{ steps.meta.outputs.labels }} |
| 114 | + platforms: linux/amd64,linux/arm64 # Multi-platform build |
| 115 | + cache-from: type=gha # Use GitHub Actions cache |
| 116 | + cache-to: type=gha,mode=max # Cache layers for faster builds |
| 117 | + |
| 118 | + # ============================================================================ |
| 119 | + # DEPLOY JOB: Deploy to Kubernetes using Helm with secrets management |
| 120 | + # ============================================================================ |
| 121 | + deploy: |
| 122 | + name: Deploy to Kubernetes |
| 123 | + runs-on: ubuntu-latest |
| 124 | + needs: build # Wait for build job to complete |
| 125 | + environment: kubernetes # Deployment environment (for protection rules) |
| 126 | + |
| 127 | + steps: |
| 128 | + # Checkout source code for Helm charts |
| 129 | + - uses: actions/checkout@v4 |
| 130 | + |
| 131 | + # Install kubectl for Kubernetes cluster management |
| 132 | + - name: Setup Kubectl |
| 133 | + uses: azure/setup-kubectl@v3 |
| 134 | + with: |
| 135 | + version: 'latest' |
| 136 | + |
| 137 | + # Install Helm for package management |
| 138 | + - name: Setup Helm |
| 139 | + uses: azure/setup-helm@v3 |
| 140 | + with: |
| 141 | + version: '3.12.0' |
| 142 | + |
| 143 | + # Configure Kubernetes cluster access |
| 144 | + - name: Configure Kubernetes |
| 145 | + run: | |
| 146 | + mkdir -p ~/.kube |
| 147 | + # Decode base64-encoded kubeconfig from secrets |
| 148 | + echo "${{ secrets.KUBE_CONFIG }}" | base64 -d > ~/.kube/config |
| 149 | + chmod 600 ~/.kube/config |
| 150 | + |
| 151 | + # Set context to student cluster |
| 152 | + kubectl config use-context student |
| 153 | +
|
| 154 | + # Add Bitnami repository for PostgreSQL dependency |
| 155 | + - name: Add Bitnami Helm Repository |
| 156 | + run: | |
| 157 | + helm repo add bitnami https://charts.bitnami.com/bitnami |
| 158 | + helm repo update |
| 159 | +
|
| 160 | + # ============================================================================ |
| 161 | + # HELM DEPLOYMENT: Deploy application with secure secrets management |
| 162 | + # ============================================================================ |
| 163 | + # |
| 164 | + # Uses values-secure.yaml which references Kubernetes secrets created |
| 165 | + # from GitHub Actions secrets. This ensures no sensitive data is stored |
| 166 | + # in version control. |
| 167 | + # |
| 168 | + # Deployed services: |
| 169 | + # - PostgreSQL database (Bitnami chart) |
| 170 | + # - All microservices with Swagger UI enabled |
| 171 | + # - Prometheus + Grafana monitoring stack |
| 172 | + # - Nginx ingress with Let's Encrypt TLS |
| 173 | + # ============================================================================ |
| 174 | + |
| 175 | + - name: Deploy with Helm |
| 176 | + run: | |
| 177 | + cd helm/study-assistant |
| 178 | + |
| 179 | + # Update Helm chart dependencies (PostgreSQL) |
| 180 | + helm dependency update |
| 181 | + |
| 182 | + # Deploy/upgrade the complete application stack |
| 183 | + # Uses values-secure.yaml for production-ready configuration |
| 184 | + helm upgrade --install study-assistant . \ |
| 185 | + --namespace=dev \ |
| 186 | + --timeout=10m \ |
| 187 | + --wait \ |
| 188 | + --values values-secure.yaml \ |
| 189 | + --set secrets.postgresPassword="${{ secrets.POSTGRES_PASSWORD }}" \ |
| 190 | + --set secrets.jwtSecret="${{ secrets.JWT_SECRET }}" \ |
| 191 | + --set secrets.genaiApiKey="${{ secrets.GENAI_API_KEY }}" \ |
| 192 | + --set secrets.grafanaPassword="${{ secrets.GRAFANA_PASSWORD }}" |
| 193 | +
|
| 194 | + - name: Verify Deployment |
| 195 | + run: | |
| 196 | + echo "Checking deployment status..." |
| 197 | + kubectl get pods -n dev -l app.kubernetes.io/instance=study-assistant |
| 198 | + kubectl get svc -n dev -l app.kubernetes.io/instance=study-assistant |
| 199 | + kubectl get ingress -n dev -l app.kubernetes.io/instance=study-assistant |
| 200 | + |
| 201 | + # Wait for pods to be ready |
| 202 | + kubectl wait --for=condition=ready pod -l app.kubernetes.io/instance=study-assistant -n dev --timeout=300s |
| 203 | +
|
| 204 | + - name: Get Application URL |
| 205 | + run: | |
| 206 | + INGRESS_HOST=$(kubectl get ingress -n dev -l app.kubernetes.io/instance=study-assistant -o jsonpath='{.items[0].spec.rules[0].host}' 2>/dev/null || echo "No ingress found") |
| 207 | + if [ "$INGRESS_HOST" != "No ingress found" ]; then |
| 208 | + echo "🚀 Application deployed successfully!" |
| 209 | + echo "📱 Application URL: https://$INGRESS_HOST" |
| 210 | + else |
| 211 | + echo "⚠️ Application deployed but no ingress URL found" |
| 212 | + fi |
0 commit comments