|
| 1 | +name: AutoDocOps CI/CD |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main, develop ] |
| 6 | + pull_request: |
| 7 | + branches: [ main ] |
| 8 | + |
| 9 | +env: |
| 10 | + DOTNET_VERSION: '8.0.x' |
| 11 | + NODE_VERSION: '20.x' |
| 12 | + REGISTRY: ghcr.io |
| 13 | + IMAGE_NAME: ${{ github.repository }} |
| 14 | + |
| 15 | +jobs: |
| 16 | + # Job 1: Build y Test Backend |
| 17 | + backend-build-test: |
| 18 | + name: Backend Build & Test |
| 19 | + runs-on: ubuntu-latest |
| 20 | + |
| 21 | + steps: |
| 22 | + - name: Checkout código |
| 23 | + uses: actions/checkout@v4 |
| 24 | + |
| 25 | + - name: Setup .NET |
| 26 | + uses: actions/setup-dotnet@v4 |
| 27 | + with: |
| 28 | + dotnet-version: ${{ env.DOTNET_VERSION }} |
| 29 | + |
| 30 | + - name: Restore dependencias |
| 31 | + run: dotnet restore |
| 32 | + working-directory: ./backend |
| 33 | + |
| 34 | + - name: Build backend |
| 35 | + run: dotnet build --no-restore --configuration Release |
| 36 | + working-directory: ./backend |
| 37 | + |
| 38 | + - name: Run unit tests |
| 39 | + run: dotnet test --no-build --configuration Release --verbosity normal --collect:"XPlat Code Coverage" |
| 40 | + working-directory: ./backend |
| 41 | + |
| 42 | + - name: Upload coverage reports |
| 43 | + uses: codecov/codecov-action@v3 |
| 44 | + with: |
| 45 | + directory: ./backend/TestResults |
| 46 | + flags: backend |
| 47 | + name: backend-coverage |
| 48 | + |
| 49 | + - name: Publish backend artifacts |
| 50 | + run: dotnet publish --no-build --configuration Release --output ./publish |
| 51 | + working-directory: ./backend/src/AutoDocOps.Api/AutoDocOps.Api |
| 52 | + |
| 53 | + - name: Upload backend artifacts |
| 54 | + uses: actions/upload-artifact@v4 |
| 55 | + with: |
| 56 | + name: backend-artifacts |
| 57 | + path: ./backend/src/AutoDocOps.Api/AutoDocOps.Api/publish |
| 58 | + |
| 59 | + # Job 2: Build y Test Frontend |
| 60 | + frontend-build-test: |
| 61 | + name: Frontend Build & Test |
| 62 | + runs-on: ubuntu-latest |
| 63 | + |
| 64 | + steps: |
| 65 | + - name: Checkout código |
| 66 | + uses: actions/checkout@v4 |
| 67 | + |
| 68 | + - name: Setup Node.js |
| 69 | + uses: actions/setup-node@v4 |
| 70 | + with: |
| 71 | + node-version: ${{ env.NODE_VERSION }} |
| 72 | + cache: 'npm' |
| 73 | + cache-dependency-path: ./frontend/AutoDocOps-Frontend/package-lock.json |
| 74 | + |
| 75 | + - name: Install dependencies |
| 76 | + run: npm ci |
| 77 | + working-directory: ./frontend/AutoDocOps-Frontend |
| 78 | + |
| 79 | + - name: Run linting |
| 80 | + run: npm run lint |
| 81 | + working-directory: ./frontend/AutoDocOps-Frontend |
| 82 | + continue-on-error: true |
| 83 | + |
| 84 | + - name: Run type checking |
| 85 | + run: npx tsc --noEmit |
| 86 | + working-directory: ./frontend/AutoDocOps-Frontend |
| 87 | + continue-on-error: true |
| 88 | + |
| 89 | + - name: Build frontend |
| 90 | + run: npm run build |
| 91 | + working-directory: ./frontend/AutoDocOps-Frontend |
| 92 | + |
| 93 | + - name: Upload frontend artifacts |
| 94 | + uses: actions/upload-artifact@v4 |
| 95 | + with: |
| 96 | + name: frontend-artifacts |
| 97 | + path: ./frontend/AutoDocOps-Frontend/dist |
| 98 | + |
| 99 | + # Job 3: Security Scanning |
| 100 | + security-scan: |
| 101 | + name: Security Scanning |
| 102 | + runs-on: ubuntu-latest |
| 103 | + needs: [backend-build-test] |
| 104 | + |
| 105 | + steps: |
| 106 | + - name: Checkout código |
| 107 | + uses: actions/checkout@v4 |
| 108 | + |
| 109 | + - name: Run Trivy vulnerability scanner |
| 110 | + uses: aquasecurity/trivy-action@master |
| 111 | + with: |
| 112 | + scan-type: 'fs' |
| 113 | + scan-ref: '.' |
| 114 | + format: 'sarif' |
| 115 | + output: 'trivy-results.sarif' |
| 116 | + |
| 117 | + - name: Upload Trivy scan results |
| 118 | + uses: github/codeql-action/upload-sarif@v3 |
| 119 | + with: |
| 120 | + sarif_file: 'trivy-results.sarif' |
| 121 | + |
| 122 | + - name: Setup .NET for security scan |
| 123 | + uses: actions/setup-dotnet@v4 |
| 124 | + with: |
| 125 | + dotnet-version: ${{ env.DOTNET_VERSION }} |
| 126 | + |
| 127 | + - name: Install security scan tools |
| 128 | + run: | |
| 129 | + dotnet tool install --global security-scan |
| 130 | + npm install -g audit-ci |
| 131 | + continue-on-error: true |
| 132 | + |
| 133 | + - name: Run .NET security scan |
| 134 | + run: security-scan ./backend/AutoDocOps.sln |
| 135 | + working-directory: ./ |
| 136 | + continue-on-error: true |
| 137 | + |
| 138 | + - name: Run npm audit |
| 139 | + run: npm audit --audit-level moderate |
| 140 | + working-directory: ./frontend/AutoDocOps-Frontend |
| 141 | + continue-on-error: true |
| 142 | + |
| 143 | + # Job 4: Build Docker Images |
| 144 | + build-docker: |
| 145 | + name: Build Docker Images |
| 146 | + runs-on: ubuntu-latest |
| 147 | + needs: [backend-build-test, frontend-build-test] |
| 148 | + if: github.ref == 'refs/heads/main' |
| 149 | + |
| 150 | + permissions: |
| 151 | + contents: read |
| 152 | + packages: write |
| 153 | + |
| 154 | + steps: |
| 155 | + - name: Checkout código |
| 156 | + uses: actions/checkout@v4 |
| 157 | + |
| 158 | + - name: Log in to Container Registry |
| 159 | + uses: docker/login-action@v3 |
| 160 | + with: |
| 161 | + registry: ${{ env.REGISTRY }} |
| 162 | + username: ${{ github.actor }} |
| 163 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 164 | + |
| 165 | + - name: Extract metadata |
| 166 | + id: meta |
| 167 | + uses: docker/metadata-action@v5 |
| 168 | + with: |
| 169 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 170 | + tags: | |
| 171 | + type=ref,event=branch |
| 172 | + type=ref,event=pr |
| 173 | + type=sha,prefix={{branch}}- |
| 174 | + type=raw,value=latest,enable={{is_default_branch}} |
| 175 | + |
| 176 | + - name: Build and push Docker image |
| 177 | + uses: docker/build-push-action@v5 |
| 178 | + with: |
| 179 | + context: ./backend |
| 180 | + file: ./backend/Dockerfile |
| 181 | + push: true |
| 182 | + tags: ${{ steps.meta.outputs.tags }} |
| 183 | + labels: ${{ steps.meta.outputs.labels }} |
| 184 | + |
| 185 | + # Job 5: Deploy to Staging |
| 186 | + deploy-staging: |
| 187 | + name: Deploy to Staging |
| 188 | + runs-on: ubuntu-latest |
| 189 | + needs: [build-docker, security-scan] |
| 190 | + if: github.ref == 'refs/heads/develop' |
| 191 | + environment: staging |
| 192 | + |
| 193 | + steps: |
| 194 | + - name: Checkout código |
| 195 | + uses: actions/checkout@v4 |
| 196 | + |
| 197 | + - name: Setup Fly CLI |
| 198 | + uses: superfly/flyctl-actions/setup-flyctl@master |
| 199 | + |
| 200 | + - name: Deploy to Fly.io Staging |
| 201 | + run: flyctl deploy --config fly.staging.toml |
| 202 | + env: |
| 203 | + FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} |
| 204 | + |
| 205 | + - name: Run health check |
| 206 | + run: | |
| 207 | + sleep 30 |
| 208 | + curl -f https://autodocops-staging.fly.dev/health || exit 1 |
| 209 | + |
| 210 | + # Job 6: Deploy to Production |
| 211 | + deploy-production: |
| 212 | + name: Deploy to Production |
| 213 | + runs-on: ubuntu-latest |
| 214 | + needs: [build-docker, security-scan] |
| 215 | + if: github.ref == 'refs/heads/main' |
| 216 | + environment: production |
| 217 | + |
| 218 | + steps: |
| 219 | + - name: Checkout código |
| 220 | + uses: actions/checkout@v4 |
| 221 | + |
| 222 | + - name: Setup Fly CLI |
| 223 | + uses: superfly/flyctl-actions/setup-flyctl@master |
| 224 | + |
| 225 | + - name: Deploy Backend to Fly.io |
| 226 | + run: flyctl deploy --config fly.toml |
| 227 | + env: |
| 228 | + FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} |
| 229 | + |
| 230 | + - name: Deploy Frontend to Cloudflare Pages |
| 231 | + uses: cloudflare/pages-action@v1 |
| 232 | + with: |
| 233 | + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 234 | + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 235 | + projectName: autodocops-frontend |
| 236 | + directory: ./frontend/AutoDocOps-Frontend/dist |
| 237 | + gitHubToken: ${{ secrets.GITHUB_TOKEN }} |
| 238 | + |
| 239 | + - name: Run production health check |
| 240 | + run: | |
| 241 | + sleep 60 |
| 242 | + curl -f https://api.autodocops.com/health || exit 1 |
| 243 | + curl -f https://autodocops.com || exit 1 |
| 244 | + |
| 245 | + - name: Notify deployment success |
| 246 | + uses: 8398a7/action-slack@v3 |
| 247 | + if: success() |
| 248 | + with: |
| 249 | + status: success |
| 250 | + text: '🚀 AutoDocOps deployed successfully to production!' |
| 251 | + env: |
| 252 | + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} |
| 253 | + |
| 254 | + - name: Notify deployment failure |
| 255 | + uses: 8398a7/action-slack@v3 |
| 256 | + if: failure() |
| 257 | + with: |
| 258 | + status: failure |
| 259 | + text: '❌ AutoDocOps deployment to production failed!' |
| 260 | + env: |
| 261 | + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} |
| 262 | + |
| 263 | + # Job 7: Performance Testing |
| 264 | + performance-test: |
| 265 | + name: Performance Testing |
| 266 | + runs-on: ubuntu-latest |
| 267 | + needs: [deploy-staging] |
| 268 | + if: github.ref == 'refs/heads/develop' |
| 269 | + |
| 270 | + steps: |
| 271 | + - name: Checkout código |
| 272 | + uses: actions/checkout@v4 |
| 273 | + |
| 274 | + - name: Setup k6 |
| 275 | + run: | |
| 276 | + sudo gpg -k |
| 277 | + sudo gpg --no-default-keyring --keyring /usr/share/keyrings/k6-archive-keyring.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69 |
| 278 | + echo "deb [signed-by=/usr/share/keyrings/k6-archive-keyring.gpg] https://dl.k6.io/deb stable main" | sudo tee /etc/apt/sources.list.d/k6.list |
| 279 | + sudo apt-get update |
| 280 | + sudo apt-get install k6 |
| 281 | + |
| 282 | + - name: Run performance tests |
| 283 | + run: k6 run --out json=results.json ./tests/performance/load-test.js |
| 284 | + env: |
| 285 | + BASE_URL: https://autodocops-staging.fly.dev |
| 286 | + |
| 287 | + - name: Upload performance results |
| 288 | + uses: actions/upload-artifact@v4 |
| 289 | + with: |
| 290 | + name: performance-results |
| 291 | + path: results.json |
| 292 | + |
| 293 | + # Job 8: E2E Testing |
| 294 | + e2e-test: |
| 295 | + name: E2E Testing |
| 296 | + runs-on: ubuntu-latest |
| 297 | + needs: [deploy-staging] |
| 298 | + if: github.ref == 'refs/heads/develop' |
| 299 | + |
| 300 | + steps: |
| 301 | + - name: Checkout código |
| 302 | + uses: actions/checkout@v4 |
| 303 | + |
| 304 | + - name: Setup Node.js |
| 305 | + uses: actions/setup-node@v4 |
| 306 | + with: |
| 307 | + node-version: ${{ env.NODE_VERSION }} |
| 308 | + |
| 309 | + - name: Install Playwright |
| 310 | + run: | |
| 311 | + npm install -g @playwright/test |
| 312 | + npx playwright install --with-deps |
| 313 | + |
| 314 | + - name: Run E2E tests |
| 315 | + run: npx playwright test |
| 316 | + env: |
| 317 | + BASE_URL: https://autodocops-staging.fly.dev |
| 318 | + |
| 319 | + - name: Upload E2E test results |
| 320 | + uses: actions/upload-artifact@v4 |
| 321 | + if: always() |
| 322 | + with: |
| 323 | + name: e2e-test-results |
| 324 | + path: | |
| 325 | + test-results/ |
| 326 | + playwright-report/ |
| 327 | + |
0 commit comments