-
Notifications
You must be signed in to change notification settings - Fork 0
509 lines (442 loc) · 18.8 KB
/
Copy pathmain.yml
File metadata and controls
509 lines (442 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
name: Build and Deploy
on:
push:
branches: [main]
pull_request:
branches: [main]
env:
AZURE_API_NAME: XenobiasoftSudokuApi-prod
AZURE_MCP_NAME: XenobiasoftSudokuMcp-prod
AZURE_FUNCTIONS_NAME: XenobiasoftSudokuFunctions-prod
AZURE_SWA_NAME: swa-sudoku-xenobiasoft-prod
DOTNET_VERSION: "10.x"
permissions:
contents: read
id-token: write
checks: write
pull-requests: write
jobs:
changes:
runs-on: ubuntu-latest
outputs:
code: ${{ steps.filter.outputs.code }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
code:
- 'src/**'
- 'infra/**'
- 'Tests/**'
- '**/*.csproj'
- '**/*.sln'
- 'package*.json'
- '.github/workflows/**'
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
dotnet-quality: "ga"
- name: Cache .NET packages
uses: actions/cache@v4
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }}
restore-keys: |
${{ runner.os }}-nuget-
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore --verbosity normal
- name: Publish API
run: dotnet publish src/backend/Sudoku.Api/Sudoku.Api.csproj --configuration Release --output ${{ github.workspace }}/publish-api --no-restore --no-build
- name: Publish MCP Server
run: dotnet publish src/backend/Sudoku.McpServer/Sudoku.McpServer.csproj --configuration Release --output ${{ github.workspace }}/publish-mcp --no-restore --no-build
- name: Publish Functions
run: dotnet publish src/backend/Sudoku.Functions/Sudoku.Functions.csproj --configuration Release --output ${{ github.workspace }}/publish-functions --no-restore --no-build
- name: Upload publish-api artifact
uses: actions/upload-artifact@v4
with:
name: publish-api
path: ${{ github.workspace }}/publish-api
- name: Upload publish-mcp artifact
uses: actions/upload-artifact@v4
with:
name: publish-mcp
path: ${{ github.workspace }}/publish-mcp
- name: Upload publish-functions artifact
uses: actions/upload-artifact@v4
with:
name: publish-functions
path: ${{ github.workspace }}/publish-functions
# The .NET isolated publish output includes a hidden `.azurefunctions`
# directory with the worker metadata. upload-artifact@v4 excludes
# hidden files by default, which drops it from the package and makes
# Flex Consumption reject the deploy ("Cannot find required
# .azurefunctions directory at root level in the .zip package").
include-hidden-files: true
build-frontend:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
cache: 'npm'
cache-dependency-path: src/frontend/Sudoku.React/package-lock.json
- name: Install dependencies
run: npm ci
working-directory: src/frontend/Sudoku.React
- name: Run frontend tests
run: npm run test
working-directory: src/frontend/Sudoku.React
- name: Build frontend
run: npm run build
working-directory: src/frontend/Sudoku.React
- name: Upload frontend artifact
uses: actions/upload-artifact@v4
with:
name: frontend-dist
path: src/frontend/Sudoku.React/dist
test:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
dotnet-quality: "ga"
- name: Cache .NET packages
uses: actions/cache@v4
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }}
restore-keys: |
${{ runner.os }}-nuget-
- name: Restore dependencies
run: dotnet restore
- name: Run tests with coverage
run: dotnet test --configuration Release --no-restore --verbosity normal --logger trx --results-directory TestResults --collect:"XPlat Code Coverage" --settings coverlet.runsettings
- name: Generate coverage report
uses: danielpalme/ReportGenerator-GitHub-Action@5
with:
reports: TestResults/**/coverage.cobertura.xml
targetdir: CoverageReport
reporttypes: HtmlInline;Cobertura;MarkdownSummaryGithub
verbosity: Warning
- name: Enforce 80% line coverage threshold
run: |
PERCENT=$(awk 'NR==2 && /line-rate/ { match($0, /line-rate="([0-9.]+)"/, a); printf "%.2f", a[1] * 100 }' CoverageReport/Cobertura.xml)
echo "Line coverage: ${PERCENT}%"
echo "### Coverage Summary" >> $GITHUB_STEP_SUMMARY
echo "Line coverage: **${PERCENT}%**" >> $GITHUB_STEP_SUMMARY
if awk "BEGIN { exit !($PERCENT < 80) }"; then
echo "❌ Coverage ${PERCENT}% is below the required 80% threshold." | tee -a $GITHUB_STEP_SUMMARY
exit 1
else
echo "✅ Coverage ${PERCENT}% meets the required 80% threshold." | tee -a $GITHUB_STEP_SUMMARY
fi
- name: Publish test results as PR annotations
uses: dorny/test-reporter@v1
if: always()
with:
name: .NET Tests
path: TestResults/*.trx
reporter: dotnet-trx
fail-on-error: false
- name: Upload test-results artifact
uses: actions/upload-artifact@v4
if: always()
with:
name: test-results
path: TestResults
deploy-infra:
needs: [test, changes]
if: github.event_name == 'push' && github.ref == 'refs/heads/main' && needs.changes.outputs.code == 'true'
runs-on: ubuntu-latest
environment: production
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Azure Login
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Debug Azure context
run: |
az account show --output table
az account list --output table
az group show -n rg-xenobiasoft-sudoku-prod-westus2 --output table
# Flex Consumption does not support in-place migration of an existing
# Function App from another plan (Azure returns Conflict 59602). If the
# app currently lives on a non-Flex plan, delete it so the Bicep deploy
# can recreate it fresh on the FC1 plan. Idempotent: once the app is on
# FlexConsumption this is a no-op. Does not touch the shared B1 plan that
# hosts the API and MCP apps.
- name: Recreate Functions app on Flex (delete if on a non-Flex plan)
shell: bash
run: |
set -euo pipefail
RG="rg-xenobiasoft-sudoku-prod-westus2"
APP="${AZURE_FUNCTIONS_NAME}"
FLEX_PLAN="XenobiasoftFunctionsPlan-prod"
COSMOS="cosmos-sudoku-prod"
# Existence via an RG-scoped list + client-side filter (returns an
# empty string / exit 0 when absent, so `set -e` only trips on a real
# CLI/auth failure). The plan is then read from properties.serverFarmId
# via `az resource show`: `az functionapp [show|list] --query
# appServicePlanId` returns null for Flex Consumption apps (and was
# empty in CI for the B1 app too), which previously mis-routed an
# existing app down the delete-or-create-fresh paths.
APP_ID=$(az functionapp list -g "$RG" --query "[?name=='$APP'].id | [0]" -o tsv)
if [ -z "$APP_ID" ]; then
echo "No existing Functions app; Bicep will create it fresh on FC1."
else
PLAN_ID=$(az resource show --ids "$APP_ID" --query "properties.serverFarmId" -o tsv)
if [[ "$PLAN_ID" == */"$FLEX_PLAN" ]]; then
echo "App already on Flex plan ($FLEX_PLAN); no recreate needed."
else
echo "Existing app is on a non-Flex plan ($PLAN_ID); deleting so Bicep can recreate it on FC1."
# The recreated app gets a NEW system-assigned principal. Azure
# forbids updating an existing role assignment's principalId, so the
# assignments tied to the old principal must be removed first or
# Bicep fails with RoleAssignmentUpdateNotPermitted. Capture the old
# principal's assignment ids while it still resolves, then delete.
OLD_PRINCIPAL=$(az functionapp show --ids "$APP_ID" --query identity.principalId -o tsv 2>/dev/null || true)
STALE_RBAC=""
STALE_COSMOS=""
if [ -n "$OLD_PRINCIPAL" ] && [ "$OLD_PRINCIPAL" != "null" ]; then
STALE_RBAC=$(az role assignment list --all --assignee "$OLD_PRINCIPAL" --query "[].id" -o tsv 2>/dev/null || true)
STALE_COSMOS=$(az cosmosdb sql role assignment list --account-name "$COSMOS" -g "$RG" --query "[?principalId=='$OLD_PRINCIPAL'].id" -o tsv 2>/dev/null || true)
fi
az functionapp delete --ids "$APP_ID"
for id in $STALE_RBAC; do
echo "Removing stale Azure RBAC assignment $id"
az role assignment delete --ids "$id" 2>/dev/null || true
done
for raid in $STALE_COSMOS; do
echo "Removing stale Cosmos SQL role assignment $raid"
az cosmosdb sql role assignment delete --account-name "$COSMOS" -g "$RG" --role-assignment-id "$raid" --yes 2>/dev/null || true
done
fi
fi
- name: Deploy Bicep template
id: deploy
uses: azure/arm-deploy@v2
with:
resourceGroupName: rg-xenobiasoft-sudoku-prod-westus2
template: ./infra/main.bicep
parameters: ./infra/params/prod.bicepparam
scope: resourcegroup
failOnStdErr: false
# Role assignments are not declared in Bicep (they go stale on resource
# recreate). They live in scripts/assign-roles.sh — the single source of
# truth — and are applied here, after Bicep has (re)created the managed
# identities. The script is idempotent and exits non-zero on real failure.
# See .claude/rules/rbac-role-assignments.md.
- name: Assign managed identity roles
shell: bash
run: bash scripts/assign-roles.sh
- name: DNS check (scm endpoint)
shell: bash
run: |
sudo apt-get update && sudo apt-get install -y dnsutils
for host in "${AZURE_API_NAME}.azurewebsites.net"; do
echo "Checking DNS for $host"
for i in {1..10}; do
if nslookup "$host"; then
echo "OK: $host"
break
fi
echo "Waiting for DNS to propagate ($i/10)..."
sleep 15
done
# Do not fail the job just because DNS isn't ready yet
nslookup "$host" || echo "WARNING: $host not resolvable yet; continuing to deploy."
done
- name: Dump deployment error details (if failed)
if: failure()
run: |
echo "Showing deployment operations for 'main'..."
az deployment operation group list \
--resource-group rg-xenobiasoft-sudoku-prod-westus2 \
--name main \
--output jsonc
echo "Showing deployment details for 'main'..."
az deployment group show \
--resource-group rg-xenobiasoft-sudoku-prod-westus2 \
--name main \
--output jsonc
deploy-apps:
needs: deploy-infra
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
environment: production
steps:
- name: Azure Login
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Download publish-api artifact
uses: actions/download-artifact@v4
with:
name: publish-api
path: ${{ github.workspace }}/publish-api
- name: Deploy API
uses: azure/webapps-deploy@v3
with:
app-name: ${{ env.AZURE_API_NAME }}
package: ${{ github.workspace }}/publish-api
- name: Download publish-mcp artifact
uses: actions/download-artifact@v4
with:
name: publish-mcp
path: ${{ github.workspace }}/publish-mcp
- name: Deploy MCP Server
uses: azure/webapps-deploy@v3
with:
app-name: ${{ env.AZURE_MCP_NAME }}
package: ${{ github.workspace }}/publish-mcp
- name: Download publish-functions artifact
uses: actions/download-artifact@v4
with:
name: publish-functions
path: ${{ github.workspace }}/publish-functions
- name: Deploy Functions
shell: bash
run: |
set -euo pipefail
cd "${{ github.workspace }}/publish-functions"
zip -r ../functions.zip .
# Flex Consumption deploys the package to its configured deployment
# blob container via `az functionapp deployment source config-zip`
# (the documented Flex method). `az functionapp deploy --type zip`
# targets the OneDeploy endpoint and returns HTTP 415 on Flex apps.
# No --build-remote: the .NET isolated publish output is pre-built.
az functionapp deployment source config-zip \
--name "${{ env.AZURE_FUNCTIONS_NAME }}" \
--resource-group "rg-xenobiasoft-sudoku-prod-westus2" \
--src "${{ github.workspace }}/functions.zip"
deploy-event-grid:
needs: deploy-apps
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
environment: production
steps:
- name: Azure Login
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
# Created here (not in Bicep) because the subscription must point at a live
# function. We use the WebHook destination against the Functions Event Grid
# extension URL rather than the AzureFunction destination: the latter
# resolves the function through the ARM "list functions" API, which is not
# reliably populated for dotnet-isolated apps on a Linux plan and returns
# NotFound. The WebHook path uses the eventgrid_extension system key, and
# the Functions runtime auto-answers Event Grid's validation handshake.
# The command is an upsert, so it is safe to re-run on every deploy.
- name: Create Event Grid subscription (BlobDeleted -> PuzzleReplenishFunction)
shell: bash
run: |
set -euo pipefail
RG="rg-xenobiasoft-sudoku-prod-westus2"
TOPIC="sudoku-puzzle-pool-prod"
FUNC_APP="${AZURE_FUNCTIONS_NAME}"
FUNC_NAME="PuzzleReplenishFunction"
# The eventgrid_extension system key only exists once the host has
# loaded the Event Grid-triggered function, so polling for it also
# gates on the function being indexed and running.
SYSKEY=""
for i in {1..15}; do
SYSKEY=$(az functionapp keys list \
--name "$FUNC_APP" --resource-group "$RG" \
--query "systemKeys.eventgrid_extension" -o tsv 2>/dev/null || true)
if [ -n "$SYSKEY" ] && [ "$SYSKEY" != "null" ]; then
echo "eventgrid_extension system key is available."
break
fi
echo "Waiting for function to index / system key to appear ($i/15)..."
sleep 20
done
if [ -z "$SYSKEY" ] || [ "$SYSKEY" = "null" ]; then
echo "ERROR: eventgrid_extension system key never appeared; the function did not index." >&2
exit 1
fi
echo "::add-mask::$SYSKEY"
ENDPOINT="https://${FUNC_APP}.azurewebsites.net/runtime/webhooks/eventgrid?functionName=${FUNC_NAME}&code=${SYSKEY}"
# The system key persists in Azure Storage across deployments, so its
# presence doesn't mean the newly-deployed app is ready. Poll the
# webhook endpoint directly until the runtime answers (non-404/non-000).
for i in {1..10}; do
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$ENDPOINT" || echo "000")
if [ "$HTTP_STATUS" != "404" ] && [ "$HTTP_STATUS" != "000" ]; then
echo "Webhook endpoint is responding (HTTP $HTTP_STATUS)."
break
fi
echo "Waiting for webhook endpoint to be ready ($i/10)..."
sleep 15
done
az eventgrid system-topic event-subscription create \
--name puzzle-replenish-sub \
--system-topic-name "$TOPIC" \
--resource-group "$RG" \
--endpoint-type webhook \
--endpoint "$ENDPOINT" \
--included-event-types Microsoft.Storage.BlobDeleted \
--subject-begins-with "/blobServices/default/containers/sudoku-puzzles/" \
--max-delivery-attempts 30 \
--event-ttl 1440
echo "Event Grid subscription created/updated."
deploy-swa:
needs: [deploy-infra, build-frontend, changes]
if: needs.changes.outputs.code == 'true'
runs-on: ubuntu-latest
environment: production
steps:
- name: Azure Login
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Get SWA deployment token
id: swa-token
run: |
TOKEN=$(az staticwebapp secrets list \
--name ${{ env.AZURE_SWA_NAME }} \
--query "properties.apiKey" \
--output tsv)
if [ -z "$TOKEN" ]; then
echo "ERROR: Failed to retrieve SWA deployment token for ${{ env.AZURE_SWA_NAME }}" >&2
exit 1
fi
echo "::add-mask::$TOKEN"
echo "token=$TOKEN" >> $GITHUB_OUTPUT
- name: Download frontend artifact
uses: actions/download-artifact@v4
with:
name: frontend-dist
path: frontend-dist
- name: Deploy to Static Web App
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ steps.swa-token.outputs.token }}
repo_token: ${{ secrets.GITHUB_TOKEN }}
action: "upload"
app_location: "frontend-dist"
output_location: ""
skip_app_build: true