|
| 1 | +name: Azure Stamps Pattern CI/CD |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main, master ] |
| 6 | + pull_request: |
| 7 | + branches: [ main, master ] |
| 8 | + |
| 9 | +env: |
| 10 | + DOTNET_VERSION: '6.0.x' |
| 11 | + AZURE_FUNCTIONAPP_PACKAGE_PATH: 'AzureArchitecture' |
| 12 | + |
| 13 | +jobs: |
| 14 | + # Bicep Validation and Linting |
| 15 | + bicep-validation: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + name: 'Bicep Validation' |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Checkout |
| 21 | + uses: actions/checkout@v4 |
| 22 | + |
| 23 | + - name: Setup Azure CLI |
| 24 | + uses: azure/setup-azure-cli@v1 |
| 25 | + |
| 26 | + - name: Azure CLI Login |
| 27 | + uses: azure/login@v1 |
| 28 | + with: |
| 29 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 30 | + |
| 31 | + - name: Install Bicep CLI |
| 32 | + run: az bicep install |
| 33 | + |
| 34 | + - name: Bicep Build and Validate |
| 35 | + run: | |
| 36 | + cd AzureArchitecture |
| 37 | + echo "Building main.bicep..." |
| 38 | + az bicep build --file main.bicep |
| 39 | + |
| 40 | + echo "Building deploymentStampLayer.bicep..." |
| 41 | + az bicep build --file deploymentStampLayer.bicep |
| 42 | + |
| 43 | + echo "Building globalLayer.bicep..." |
| 44 | + az bicep build --file globalLayer.bicep --ignore-warnings |
| 45 | + |
| 46 | + echo "Building regionalLayer.bicep..." |
| 47 | + az bicep build --file regionalLayer.bicep --ignore-warnings |
| 48 | + |
| 49 | + - name: Bicep What-If Deployment |
| 50 | + run: | |
| 51 | + cd AzureArchitecture |
| 52 | + az deployment group what-if \ |
| 53 | + --resource-group rg-stamps-dev \ |
| 54 | + --template-file main.bicep \ |
| 55 | + --parameters main.parameters.json \ |
| 56 | + --parameters sqlAdminPassword="${{ secrets.SQL_ADMIN_PASSWORD }}" |
| 57 | + continue-on-error: true |
| 58 | + |
| 59 | + # .NET Build and Test |
| 60 | + dotnet-build-test: |
| 61 | + runs-on: ubuntu-latest |
| 62 | + name: 'Build and Test .NET Functions' |
| 63 | + |
| 64 | + steps: |
| 65 | + - name: Checkout |
| 66 | + uses: actions/checkout@v4 |
| 67 | + |
| 68 | + - name: Setup .NET Core |
| 69 | + uses: actions/setup-dotnet@v3 |
| 70 | + with: |
| 71 | + dotnet-version: ${{ env.DOTNET_VERSION }} |
| 72 | + |
| 73 | + - name: Restore NuGet packages |
| 74 | + run: | |
| 75 | + cd AzureArchitecture |
| 76 | + dotnet restore AzureArchitecture.sln |
| 77 | + |
| 78 | + - name: Build Functions |
| 79 | + run: | |
| 80 | + cd AzureArchitecture |
| 81 | + dotnet build AzureArchitecture.sln --configuration Release --no-restore |
| 82 | + |
| 83 | + - name: Run Unit Tests |
| 84 | + run: | |
| 85 | + cd AzureArchitecture |
| 86 | + dotnet test Tests/CreateTenantFunctionTests.cs --configuration Release --no-build --verbosity normal --collect:"XPlat Code Coverage" |
| 87 | + |
| 88 | + - name: Upload Code Coverage |
| 89 | + uses: codecov/codecov-action@v3 |
| 90 | + with: |
| 91 | + files: ./AzureArchitecture/TestResults/*/coverage.cobertura.xml |
| 92 | + flags: unittests |
| 93 | + name: stamps-pattern-coverage |
| 94 | + |
| 95 | + # Security Scanning |
| 96 | + security-scan: |
| 97 | + runs-on: ubuntu-latest |
| 98 | + name: 'Security and Code Quality' |
| 99 | + |
| 100 | + steps: |
| 101 | + - name: Checkout |
| 102 | + uses: actions/checkout@v4 |
| 103 | + |
| 104 | + - name: Run Bandit Security Scan |
| 105 | + uses: tj-actions/bandit@v5 |
| 106 | + with: |
| 107 | + options: "-r . -f json -o bandit-report.json" |
| 108 | + continue-on-error: true |
| 109 | + |
| 110 | + - name: Run CodeQL Analysis |
| 111 | + uses: github/codeql-action/init@v2 |
| 112 | + with: |
| 113 | + languages: csharp |
| 114 | + |
| 115 | + - name: Setup .NET Core |
| 116 | + uses: actions/setup-dotnet@v3 |
| 117 | + with: |
| 118 | + dotnet-version: ${{ env.DOTNET_VERSION }} |
| 119 | + |
| 120 | + - name: Build for CodeQL |
| 121 | + run: | |
| 122 | + cd AzureArchitecture |
| 123 | + dotnet build AzureArchitecture.sln --configuration Release |
| 124 | + |
| 125 | + - name: Perform CodeQL Analysis |
| 126 | + uses: github/codeql-action/analyze@v2 |
| 127 | + |
| 128 | + # Infrastructure Deployment (Development) |
| 129 | + deploy-infrastructure-dev: |
| 130 | + if: github.ref == 'refs/heads/main' && github.event_name == 'push' |
| 131 | + needs: [bicep-validation, dotnet-build-test, security-scan] |
| 132 | + runs-on: ubuntu-latest |
| 133 | + name: 'Deploy Infrastructure (Dev)' |
| 134 | + environment: development |
| 135 | + |
| 136 | + steps: |
| 137 | + - name: Checkout |
| 138 | + uses: actions/checkout@v4 |
| 139 | + |
| 140 | + - name: Azure CLI Login |
| 141 | + uses: azure/login@v1 |
| 142 | + with: |
| 143 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 144 | + |
| 145 | + - name: Install Bicep CLI |
| 146 | + run: az bicep install |
| 147 | + |
| 148 | + - name: Create Resource Group |
| 149 | + run: | |
| 150 | + az group create --name rg-stamps-dev --location eastus |
| 151 | + |
| 152 | + - name: Deploy Infrastructure |
| 153 | + run: | |
| 154 | + cd AzureArchitecture |
| 155 | + az deployment group create \ |
| 156 | + --resource-group rg-stamps-dev \ |
| 157 | + --template-file main.bicep \ |
| 158 | + --parameters main.parameters.json \ |
| 159 | + --parameters environment=dev \ |
| 160 | + --parameters sqlAdminPassword="${{ secrets.SQL_ADMIN_PASSWORD }}" |
| 161 | + |
| 162 | + - name: Output Deployment Results |
| 163 | + run: | |
| 164 | + echo "Infrastructure deployment completed successfully" |
| 165 | + az deployment group show \ |
| 166 | + --resource-group rg-stamps-dev \ |
| 167 | + --name main \ |
| 168 | + --query properties.outputs |
| 169 | +
|
| 170 | + # Function App Deployment (Development) |
| 171 | + deploy-functions-dev: |
| 172 | + if: github.ref == 'refs/heads/main' && github.event_name == 'push' |
| 173 | + needs: [deploy-infrastructure-dev] |
| 174 | + runs-on: ubuntu-latest |
| 175 | + name: 'Deploy Functions (Dev)' |
| 176 | + environment: development |
| 177 | + |
| 178 | + steps: |
| 179 | + - name: Checkout |
| 180 | + uses: actions/checkout@v4 |
| 181 | + |
| 182 | + - name: Setup .NET Core |
| 183 | + uses: actions/setup-dotnet@v3 |
| 184 | + with: |
| 185 | + dotnet-version: ${{ env.DOTNET_VERSION }} |
| 186 | + |
| 187 | + - name: Build Function App |
| 188 | + run: | |
| 189 | + cd AzureArchitecture |
| 190 | + dotnet publish AzureArchitecture.sln --configuration Release --output ./output |
| 191 | + |
| 192 | + - name: Azure CLI Login |
| 193 | + uses: azure/login@v1 |
| 194 | + with: |
| 195 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 196 | + |
| 197 | + - name: Deploy Function Apps |
| 198 | + run: | |
| 199 | + # Deploy to primary region (eastus) |
| 200 | + az functionapp deployment source config-zip \ |
| 201 | + --resource-group rg-stamps-dev \ |
| 202 | + --name fa-stamps-eastus-dev \ |
| 203 | + --src AzureArchitecture/output.zip |
| 204 | + |
| 205 | + # Deploy to secondary region (westus2) |
| 206 | + az functionapp deployment source config-zip \ |
| 207 | + --resource-group rg-stamps-dev \ |
| 208 | + --name fa-stamps-westus2-dev \ |
| 209 | + --src AzureArchitecture/output.zip |
| 210 | +
|
| 211 | + # Integration Tests |
| 212 | + integration-tests: |
| 213 | + if: github.ref == 'refs/heads/main' && github.event_name == 'push' |
| 214 | + needs: [deploy-functions-dev] |
| 215 | + runs-on: ubuntu-latest |
| 216 | + name: 'Integration Tests' |
| 217 | + environment: development |
| 218 | + |
| 219 | + steps: |
| 220 | + - name: Checkout |
| 221 | + uses: actions/checkout@v4 |
| 222 | + |
| 223 | + - name: Azure CLI Login |
| 224 | + uses: azure/login@v1 |
| 225 | + with: |
| 226 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 227 | + |
| 228 | + - name: Get Function App URLs |
| 229 | + run: | |
| 230 | + FUNCTION_URL=$(az functionapp show --resource-group rg-stamps-dev --name fa-stamps-eastus-dev --query defaultHostName --output tsv) |
| 231 | + echo "FUNCTION_URL=https://$FUNCTION_URL" >> $GITHUB_ENV |
| 232 | + |
| 233 | + - name: Test Function Health |
| 234 | + run: | |
| 235 | + curl -f "$FUNCTION_URL/api/health" || exit 1 |
| 236 | + |
| 237 | + - name: Test Tenant Creation |
| 238 | + run: | |
| 239 | + curl -X POST "$FUNCTION_URL/api/tenant" \ |
| 240 | + -H "Content-Type: application/json" \ |
| 241 | + -d '{ |
| 242 | + "tenantId": "test-tenant-'$(date +%s)'", |
| 243 | + "subdomain": "test-subdomain", |
| 244 | + "tenantTier": "Shared", |
| 245 | + "region": "eastus" |
| 246 | + }' || exit 1 |
| 247 | +
|
| 248 | + # Production Deployment (Manual Approval) |
| 249 | + deploy-production: |
| 250 | + if: github.ref == 'refs/heads/main' && github.event_name == 'push' |
| 251 | + needs: [integration-tests] |
| 252 | + runs-on: ubuntu-latest |
| 253 | + name: 'Deploy to Production' |
| 254 | + environment: production |
| 255 | + |
| 256 | + steps: |
| 257 | + - name: Checkout |
| 258 | + uses: actions/checkout@v4 |
| 259 | + |
| 260 | + - name: Azure CLI Login |
| 261 | + uses: azure/login@v1 |
| 262 | + with: |
| 263 | + creds: ${{ secrets.AZURE_CREDENTIALS_PROD }} |
| 264 | + |
| 265 | + - name: Deploy Production Infrastructure |
| 266 | + run: | |
| 267 | + cd AzureArchitecture |
| 268 | + az group create --name rg-stamps-prod --location eastus |
| 269 | + |
| 270 | + az deployment group create \ |
| 271 | + --resource-group rg-stamps-prod \ |
| 272 | + --template-file main.bicep \ |
| 273 | + --parameters main.parameters.json \ |
| 274 | + --parameters environment=prod \ |
| 275 | + --parameters sqlAdminPassword="${{ secrets.SQL_ADMIN_PASSWORD_PROD }}" |
| 276 | + |
| 277 | + - name: Deploy Production Functions |
| 278 | + run: | |
| 279 | + cd AzureArchitecture |
| 280 | + dotnet publish AzureArchitecture.sln --configuration Release --output ./output |
| 281 | + |
| 282 | + # Deploy to production regions |
| 283 | + az functionapp deployment source config-zip \ |
| 284 | + --resource-group rg-stamps-prod \ |
| 285 | + --name fa-stamps-eastus-prod \ |
| 286 | + --src output.zip |
| 287 | + |
| 288 | + az functionapp deployment source config-zip \ |
| 289 | + --resource-group rg-stamps-prod \ |
| 290 | + --name fa-stamps-westus2-prod \ |
| 291 | + --src output.zip |
| 292 | +
|
| 293 | + # Cost Analysis and Monitoring Setup |
| 294 | + monitoring-setup: |
| 295 | + if: github.ref == 'refs/heads/main' && github.event_name == 'push' |
| 296 | + needs: [deploy-production] |
| 297 | + runs-on: ubuntu-latest |
| 298 | + name: 'Setup Monitoring and Alerts' |
| 299 | + environment: production |
| 300 | + |
| 301 | + steps: |
| 302 | + - name: Checkout |
| 303 | + uses: actions/checkout@v4 |
| 304 | + |
| 305 | + - name: Azure CLI Login |
| 306 | + uses: azure/login@v1 |
| 307 | + with: |
| 308 | + creds: ${{ secrets.AZURE_CREDENTIALS_PROD }} |
| 309 | + |
| 310 | + - name: Create Cost Budget Alert |
| 311 | + run: | |
| 312 | + az consumption budget create \ |
| 313 | + --resource-group rg-stamps-prod \ |
| 314 | + --budget-name stamps-monthly-budget \ |
| 315 | + --amount 5000 \ |
| 316 | + --time-grain Monthly \ |
| 317 | + --start-date $(date -d "first day of this month" +%Y-%m-%d) \ |
| 318 | + --end-date $(date -d "last day of next year" +%Y-%m-%d) \ |
| 319 | + --notifications '[{ |
| 320 | + "enabled": true, |
| 321 | + "operator": "GreaterThan", |
| 322 | + "threshold": 80, |
| 323 | + "contactEmails": ["platform-team@contoso.com"], |
| 324 | + "contactRoles": ["Owner"] |
| 325 | + }]' |
| 326 | + |
| 327 | + - name: Setup Application Insights Alerts |
| 328 | + run: | |
| 329 | + # Create alerts for function failures, high latency, etc. |
| 330 | + az monitor metrics alert create \ |
| 331 | + --name "Function App High Error Rate" \ |
| 332 | + --resource-group rg-stamps-prod \ |
| 333 | + --scopes $(az functionapp show --resource-group rg-stamps-prod --name fa-stamps-eastus-prod --query id --output tsv) \ |
| 334 | + --condition "count requests/failed > 10" \ |
| 335 | + --window-size 5m \ |
| 336 | + --evaluation-frequency 1m \ |
| 337 | + --action-group-ids /subscriptions/${{ secrets.SUBSCRIPTION_ID }}/resourceGroups/rg-stamps-prod/providers/microsoft.insights/actionGroups/stamps-alerts |
0 commit comments