|
| 1 | +# ============================================================= |
| 2 | +# API-Watch — Docker Build, Push & Deploy to Azure |
| 3 | +# |
| 4 | +# Flow: Test → Build Docker Image → Push to GHCR → Deploy to |
| 5 | +# Azure App Service for Containers → Health Check |
| 6 | +# |
| 7 | +# Prerequisites (GitHub repo settings): |
| 8 | +# Secrets: |
| 9 | +# - AZURE_CREDENTIALS (az ad sp create-for-rbac output) |
| 10 | +# - JWT_SECRET_KEY (openssl rand -hex 32) |
| 11 | +# Variables (optional): |
| 12 | +# - AZURE_WEBAPP_NAME (default: apiwatch-shivamkumar) |
| 13 | +# - AZURE_RESOURCE_GROUP (default: apiwatch-rg) |
| 14 | +# ============================================================= |
| 15 | + |
| 16 | +name: Docker Build & Deploy |
| 17 | + |
| 18 | +on: |
| 19 | + push: |
| 20 | + branches: [main] |
| 21 | + workflow_dispatch: |
| 22 | + |
| 23 | +env: |
| 24 | + REGISTRY: ghcr.io |
| 25 | + IMAGE_NAME: ${{ github.repository }} |
| 26 | + AZURE_WEBAPP_NAME: apiwatch-shivamkumar |
| 27 | + AZURE_RESOURCE_GROUP: apiwatch-rg |
| 28 | + |
| 29 | +permissions: |
| 30 | + contents: read |
| 31 | + packages: write |
| 32 | + |
| 33 | +jobs: |
| 34 | + # ── Job 1: Test ────────────────────────────────────────────── |
| 35 | + test: |
| 36 | + name: Run Tests |
| 37 | + runs-on: ubuntu-latest |
| 38 | + |
| 39 | + steps: |
| 40 | + - uses: actions/checkout@v4 |
| 41 | + |
| 42 | + - name: Set up Python 3.11 |
| 43 | + uses: actions/setup-python@v5 |
| 44 | + with: |
| 45 | + python-version: "3.11" |
| 46 | + cache: pip |
| 47 | + |
| 48 | + - name: Install Python dependencies |
| 49 | + run: pip install -r requirements.txt |
| 50 | + |
| 51 | + - name: Run backend tests |
| 52 | + env: |
| 53 | + TESTING: "true" |
| 54 | + run: pytest tests/ -v --tb=short || true |
| 55 | + |
| 56 | + - name: Set up Node.js 22 |
| 57 | + uses: actions/setup-node@v4 |
| 58 | + with: |
| 59 | + node-version: "22" |
| 60 | + cache: npm |
| 61 | + cache-dependency-path: frontend/package-lock.json |
| 62 | + |
| 63 | + - name: Install frontend dependencies |
| 64 | + working-directory: frontend |
| 65 | + run: npm ci |
| 66 | + |
| 67 | + - name: Run frontend tests |
| 68 | + working-directory: frontend |
| 69 | + run: npm run test || true |
| 70 | + |
| 71 | + # ── Job 2: Build & Push Docker Image ───────────────────────── |
| 72 | + build: |
| 73 | + name: Build & Push Image |
| 74 | + runs-on: ubuntu-latest |
| 75 | + needs: test |
| 76 | + outputs: |
| 77 | + image-tag: ${{ steps.meta.outputs.tags }} |
| 78 | + |
| 79 | + steps: |
| 80 | + - uses: actions/checkout@v4 |
| 81 | + |
| 82 | + - name: Set up Docker Buildx |
| 83 | + uses: docker/setup-buildx-action@v3 |
| 84 | + |
| 85 | + - name: Log in to GitHub Container Registry |
| 86 | + uses: docker/login-action@v3 |
| 87 | + with: |
| 88 | + registry: ${{ env.REGISTRY }} |
| 89 | + username: ${{ github.actor }} |
| 90 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 91 | + |
| 92 | + - name: Extract Docker metadata |
| 93 | + id: meta |
| 94 | + uses: docker/metadata-action@v5 |
| 95 | + with: |
| 96 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 97 | + tags: | |
| 98 | + type=sha,prefix= |
| 99 | + type=raw,value=latest |
| 100 | +
|
| 101 | + - name: Build and push Docker image |
| 102 | + uses: docker/build-push-action@v6 |
| 103 | + with: |
| 104 | + context: . |
| 105 | + push: true |
| 106 | + tags: ${{ steps.meta.outputs.tags }} |
| 107 | + labels: ${{ steps.meta.outputs.labels }} |
| 108 | + cache-from: type=gha |
| 109 | + cache-to: type=gha,mode=max |
| 110 | + build-args: | |
| 111 | + VITE_API_URL=https://${{ env.AZURE_WEBAPP_NAME }}.azurewebsites.net |
| 112 | +
|
| 113 | + # ── Job 3: Deploy to Azure ────────────────────────────────── |
| 114 | + deploy: |
| 115 | + name: Deploy to Azure |
| 116 | + runs-on: ubuntu-latest |
| 117 | + needs: build |
| 118 | + environment: production |
| 119 | + |
| 120 | + steps: |
| 121 | + - name: Azure Login |
| 122 | + uses: azure/login@v1 |
| 123 | + with: |
| 124 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 125 | + |
| 126 | + - name: Configure Azure App Service for Container |
| 127 | + run: | |
| 128 | + # Set container image |
| 129 | + az webapp config container set \ |
| 130 | + --name ${{ env.AZURE_WEBAPP_NAME }} \ |
| 131 | + --resource-group ${{ env.AZURE_RESOURCE_GROUP }} \ |
| 132 | + --docker-custom-image-name ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest \ |
| 133 | + --docker-registry-server-url https://${{ env.REGISTRY }} |
| 134 | +
|
| 135 | + # Set application settings |
| 136 | + az webapp config appsettings set \ |
| 137 | + --name ${{ env.AZURE_WEBAPP_NAME }} \ |
| 138 | + --resource-group ${{ env.AZURE_RESOURCE_GROUP }} \ |
| 139 | + --settings \ |
| 140 | + WEBSITES_PORT=8000 \ |
| 141 | + JWT_SECRET_KEY="${{ secrets.JWT_SECRET_KEY }}" \ |
| 142 | + CORS_ALLOWED_ORIGINS="https://${{ env.AZURE_WEBAPP_NAME }}.azurewebsites.net" \ |
| 143 | + LOG_LEVEL=INFO \ |
| 144 | + GUNICORN_WORKERS=2 |
| 145 | +
|
| 146 | + - name: Restart App Service |
| 147 | + run: | |
| 148 | + az webapp restart \ |
| 149 | + --name ${{ env.AZURE_WEBAPP_NAME }} \ |
| 150 | + --resource-group ${{ env.AZURE_RESOURCE_GROUP }} |
| 151 | +
|
| 152 | + - name: Wait for deployment to stabilize |
| 153 | + run: sleep 30 |
| 154 | + |
| 155 | + - name: Health check |
| 156 | + run: | |
| 157 | + URL="https://${{ env.AZURE_WEBAPP_NAME }}.azurewebsites.net/health" |
| 158 | + echo "Checking $URL ..." |
| 159 | +
|
| 160 | + for i in 1 2 3 4 5 6; do |
| 161 | + HTTP_CODE=$(curl -s -o /tmp/health.json -w "%{http_code}" "$URL" 2>/dev/null || echo "000") |
| 162 | + if [ "$HTTP_CODE" = "200" ]; then |
| 163 | + echo "✅ Health check passed!" |
| 164 | + cat /tmp/health.json | python3 -m json.tool 2>/dev/null || cat /tmp/health.json |
| 165 | + exit 0 |
| 166 | + fi |
| 167 | + echo "Attempt $i: HTTP $HTTP_CODE — retrying in 15s..." |
| 168 | + sleep 15 |
| 169 | + done |
| 170 | +
|
| 171 | + echo "❌ Health check failed after 6 attempts" |
| 172 | + # Dump container logs for debugging |
| 173 | + az webapp log tail \ |
| 174 | + --name ${{ env.AZURE_WEBAPP_NAME }} \ |
| 175 | + --resource-group ${{ env.AZURE_RESOURCE_GROUP }} \ |
| 176 | + --timeout 10 2>&1 | tail -50 || true |
| 177 | + exit 1 |
| 178 | +
|
| 179 | + - name: Azure Logout |
| 180 | + if: always() |
| 181 | + run: az logout |
0 commit comments