name: Deploy to Development on: push: branches: [ main, master, develop ] paths: - 'src/**' - 'Dockerfile' - 'nginx.conf' - 'package.json' - 'manifests/dev/**' workflow_dispatch: env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }} jobs: deploy-dev: runs-on: ubuntu-latest environment: development steps: - name: Checkout code uses: actions/checkout@v4 - name: Log in to Container Registry uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GH_TOKEN }} - name: Extract metadata id: meta uses: docker/metadata-action@v5 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: | type=ref,event=branch type=ref,event=pr type=sha,prefix={{branch}}- - name: Build and push Docker image uses: docker/build-push-action@v5 with: context: . push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} - name: Set image tag for deployment run: | IMAGE_TAG=$(echo "${{ steps.meta.outputs.tags }}" | head -n1 | cut -d':' -f2) echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV echo "🏷️ Using image tag: $IMAGE_TAG" - name: Deploy to development via webhook run: | echo "🚀 Triggering webhook deployment to development..." # Prepare deployment payload (compact JSON to avoid whitespace issues) PAYLOAD='{"environment":"development","image":"${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}","namespace":"game-2048-dev","service_name":"game-2048-dev","deployment_id":"${{ github.run_id }}-${{ github.run_attempt }}","commit_sha":"${{ github.sha }}","triggered_by":"${{ github.actor }}","timestamp":"'$(date -u +%Y-%m-%dT%H:%M:%SZ)'"}' # Generate HMAC signature for webhook security SIGNATURE=$(echo -n "$PAYLOAD" | openssl dgst -sha256 -hmac "${{ secrets.WEBHOOK_SECRET }}" | sed 's/^.* //') # Send webhook HTTP_CODE=$(curl -s -o /tmp/webhook_response.json -w "%{http_code}" \ -X POST \ -H "Content-Type: application/json" \ -H "X-Signature-SHA256: sha256=$SIGNATURE" \ -H "X-GitHub-Event: deployment" \ -H "X-GitHub-Delivery: ${{ github.run_id }}" \ -d "$PAYLOAD" \ "${{ secrets.DEV_WEBHOOK_URL }}") echo "Webhook response code: $HTTP_CODE" cat /tmp/webhook_response.json || echo "No response body" if [ "$HTTP_CODE" -ge 200 ] && [ "$HTTP_CODE" -lt 300 ]; then echo "✅ Webhook deployment triggered successfully!" else echo "❌ Webhook deployment failed with code: $HTTP_CODE" exit 1 fi - name: Wait for deployment to complete run: | echo "⏳ Waiting for deployment to stabilize..." sleep 30 - name: Health check run: | echo "🏥 Performing health check..." MAX_RETRIES=10 RETRY_COUNT=0 # Use the canonical Knative domain for health check HEALTH_URL="https://game-2048-dev.game-2048-dev.${{ secrets.DEV_DOMAIN }}" while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do echo "Attempt $((RETRY_COUNT + 1))/$MAX_RETRIES - Checking: $HEALTH_URL" if curl -f -s --max-time 10 "$HEALTH_URL" > /dev/null; then echo "✅ Health check passed!" echo "🌐 Application is available at: $HEALTH_URL" exit 0 else echo "⚠️ Health check failed, retrying in 15 seconds..." sleep 15 RETRY_COUNT=$((RETRY_COUNT + 1)) fi done echo "❌ Health check failed after $MAX_RETRIES attempts" echo "The deployment webhook was sent successfully, but the service is not responding" echo "Please check your cluster logs for deployment issues" exit 1 - name: Deployment summary if: always() run: | echo "## 🚀 Development Deployment Summary" >> $GITHUB_STEP_SUMMARY echo "- **Environment:** Development" >> $GITHUB_STEP_SUMMARY echo "- **Image:** \`${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}\`" >> $GITHUB_STEP_SUMMARY echo "- **Deployment Method:** Webhook-based" >> $GITHUB_STEP_SUMMARY echo "- **Triggered by:** ${{ github.actor }}" >> $GITHUB_STEP_SUMMARY echo "- **Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY if [ "${{ job.status }}" = "success" ]; then echo "- **Status:** ✅ Success" >> $GITHUB_STEP_SUMMARY echo "- **URL:** https://game-2048-dev.game-2048-dev.${{ secrets.DEV_DOMAIN }}" >> $GITHUB_STEP_SUMMARY else echo "- **Status:** ❌ Failed" >> $GITHUB_STEP_SUMMARY fi