|
| 1 | +name: CI/CD |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + pull_request: |
| 7 | + branches: [main] |
| 8 | + |
| 9 | +env: |
| 10 | + REGISTRY: ghcr.io |
| 11 | + BACKEND_IMAGE: ghcr.io/${{ github.repository }}/backend |
| 12 | + FRONTEND_IMAGE: ghcr.io/${{ github.repository }}/frontend |
| 13 | + |
| 14 | +jobs: |
| 15 | + # ── CI: Lint, Test & Security Audit ────────────────────────── |
| 16 | + |
| 17 | + backend-test: |
| 18 | + name: Backend — Lint & Test |
| 19 | + runs-on: ubuntu-latest |
| 20 | + defaults: |
| 21 | + run: |
| 22 | + working-directory: backend |
| 23 | + |
| 24 | + steps: |
| 25 | + - uses: actions/checkout@v4 |
| 26 | + |
| 27 | + - uses: actions/setup-python@v5 |
| 28 | + with: |
| 29 | + python-version: "3.12" |
| 30 | + cache: pip |
| 31 | + cache-dependency-path: backend/requirements.txt |
| 32 | + |
| 33 | + - name: Install dependencies |
| 34 | + run: pip install -r requirements.txt |
| 35 | + |
| 36 | + - name: Lint with pyright |
| 37 | + run: pip install pyright && pyright app/ |
| 38 | + |
| 39 | + - name: Run tests |
| 40 | + env: |
| 41 | + SECRET_KEY: ci-test-secret |
| 42 | + JWT_SECRET_KEY: ci-test-jwt-secret |
| 43 | + DATABASE_URL: "sqlite:///:memory:" |
| 44 | + run: python -m pytest tests/ -v --tb=short |
| 45 | + |
| 46 | + - name: Security audit |
| 47 | + continue-on-error: true |
| 48 | + run: pip install pip-audit && pip-audit |
| 49 | + |
| 50 | + frontend-test: |
| 51 | + name: Frontend — Lint, Type Check & Test |
| 52 | + runs-on: ubuntu-latest |
| 53 | + defaults: |
| 54 | + run: |
| 55 | + working-directory: frontend |
| 56 | + |
| 57 | + steps: |
| 58 | + - uses: actions/checkout@v4 |
| 59 | + |
| 60 | + - uses: actions/setup-node@v4 |
| 61 | + with: |
| 62 | + node-version: 20 |
| 63 | + cache: npm |
| 64 | + cache-dependency-path: frontend/package-lock.json |
| 65 | + |
| 66 | + - name: Install dependencies |
| 67 | + run: npm ci |
| 68 | + |
| 69 | + - name: Type check |
| 70 | + run: npx tsc --noEmit |
| 71 | + |
| 72 | + - name: Lint |
| 73 | + run: npm run lint |
| 74 | + |
| 75 | + - name: Run tests |
| 76 | + run: npm test |
| 77 | + |
| 78 | + - name: Security audit |
| 79 | + continue-on-error: true |
| 80 | + run: npm audit --audit-level=high |
| 81 | + |
| 82 | + # ── CD: Build & Push Docker Images ────────────────────────── |
| 83 | + |
| 84 | + build-and-push: |
| 85 | + name: Build & Push Docker Images |
| 86 | + runs-on: ubuntu-latest |
| 87 | + needs: [backend-test, frontend-test] |
| 88 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 89 | + permissions: |
| 90 | + contents: read |
| 91 | + packages: write |
| 92 | + |
| 93 | + steps: |
| 94 | + - uses: actions/checkout@v4 |
| 95 | + |
| 96 | + - name: Log in to GitHub Container Registry |
| 97 | + uses: docker/login-action@v3 |
| 98 | + with: |
| 99 | + registry: ${{ env.REGISTRY }} |
| 100 | + username: ${{ github.actor }} |
| 101 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 102 | + |
| 103 | + - name: Set up Docker Buildx |
| 104 | + uses: docker/setup-buildx-action@v3 |
| 105 | + |
| 106 | + - name: Extract metadata |
| 107 | + id: meta |
| 108 | + run: | |
| 109 | + echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT |
| 110 | + echo "date=$(date +'%Y%m%d')" >> $GITHUB_OUTPUT |
| 111 | +
|
| 112 | + - name: Build & push backend image |
| 113 | + uses: docker/build-push-action@v6 |
| 114 | + with: |
| 115 | + context: ./backend |
| 116 | + push: true |
| 117 | + tags: | |
| 118 | + ${{ env.BACKEND_IMAGE }}:latest |
| 119 | + ${{ env.BACKEND_IMAGE }}:${{ steps.meta.outputs.sha_short }} |
| 120 | + cache-from: type=gha |
| 121 | + cache-to: type=gha,mode=max |
| 122 | + |
| 123 | + - name: Build & push frontend image |
| 124 | + uses: docker/build-push-action@v6 |
| 125 | + with: |
| 126 | + context: ./frontend |
| 127 | + push: true |
| 128 | + tags: | |
| 129 | + ${{ env.FRONTEND_IMAGE }}:latest |
| 130 | + ${{ env.FRONTEND_IMAGE }}:${{ steps.meta.outputs.sha_short }} |
| 131 | + cache-from: type=gha |
| 132 | + cache-to: type=gha,mode=max |
| 133 | + |
| 134 | + # ── CD: Deploy (staging simulation) ───────────────────────── |
| 135 | + |
| 136 | + deploy: |
| 137 | + name: Deploy to Staging |
| 138 | + runs-on: ubuntu-latest |
| 139 | + needs: [build-and-push] |
| 140 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 141 | + environment: |
| 142 | + name: staging |
| 143 | + url: https://staging.example.com |
| 144 | + |
| 145 | + steps: |
| 146 | + - uses: actions/checkout@v4 |
| 147 | + |
| 148 | + - name: Deploy notification |
| 149 | + run: | |
| 150 | + echo "🚀 Deploying commit ${{ github.sha }}" |
| 151 | + echo " Backend: ${{ env.BACKEND_IMAGE }}:latest" |
| 152 | + echo " Frontend: ${{ env.FRONTEND_IMAGE }}:latest" |
| 153 | +
|
| 154 | + - name: Verify Docker Compose config |
| 155 | + env: |
| 156 | + DB_PASSWORD: ci-placeholder |
| 157 | + SECRET_KEY: ci-placeholder |
| 158 | + JWT_SECRET_KEY: ci-placeholder |
| 159 | + run: docker compose config --quiet |
| 160 | + |
| 161 | + - name: Smoke test — container builds |
| 162 | + env: |
| 163 | + DB_PASSWORD: ci-placeholder |
| 164 | + SECRET_KEY: ci-placeholder |
| 165 | + JWT_SECRET_KEY: ci-placeholder |
| 166 | + FRONTEND_URL: "https://staging.example.com" |
| 167 | + run: | |
| 168 | + docker compose build |
| 169 | + echo "✅ All containers build successfully" |
| 170 | +
|
| 171 | + - name: Deployment summary |
| 172 | + run: | |
| 173 | + echo "## Deployment Summary" >> $GITHUB_STEP_SUMMARY |
| 174 | + echo "| Item | Value |" >> $GITHUB_STEP_SUMMARY |
| 175 | + echo "|------|-------|" >> $GITHUB_STEP_SUMMARY |
| 176 | + echo "| **Commit** | \`$(git rev-parse --short HEAD)\` |" >> $GITHUB_STEP_SUMMARY |
| 177 | + echo "| **Backend Image** | \`${{ env.BACKEND_IMAGE }}:latest\` |" >> $GITHUB_STEP_SUMMARY |
| 178 | + echo "| **Frontend Image** | \`${{ env.FRONTEND_IMAGE }}:latest\` |" >> $GITHUB_STEP_SUMMARY |
| 179 | + echo "| **Status** | ✅ Ready for production |" >> $GITHUB_STEP_SUMMARY |
0 commit comments