@@ -26,7 +26,6 @@ concurrency:
2626
2727permissions :
2828 contents : read
29- id-token : " write"
3029
3130jobs :
3231 paths-filter :
@@ -175,6 +174,9 @@ jobs:
175174 dotnet-test :
176175 needs : paths-filter
177176 if : needs.paths-filter.outputs.dotnetChanges == 'true'
177+ permissions :
178+ contents : read
179+ id-token : " write"
178180 strategy :
179181 fail-fast : false
180182 matrix :
@@ -204,10 +206,127 @@ jobs:
204206 if : ${{ runner.os == 'Windows' && (needs.paths-filter.outputs.cosmosDbChanges == 'true' || (github.event_name != 'pull_request' && matrix.integration-tests)) }}
205207 shell : pwsh
206208 run : |
207- Write-Host "Launching Azure Cosmos DB Emulator"
209+ $ErrorActionPreference = "Stop"
210+ $emulatorPath = "$env:ProgramFiles\Azure Cosmos DB Emulator\Microsoft.Azure.Cosmos.Emulator.exe"
211+ $emulatorKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
212+ $maxRetries = 3
213+ $timeout = 600 # 10 minutes instead of default 4 minutes
214+ $retryCount = 0
215+ $started = $false
216+
217+ Write-Host "======================================" -ForegroundColor Cyan
218+ Write-Host "Starting Azure Cosmos DB Emulator" -ForegroundColor Cyan
219+ Write-Host "Timeout: $timeout seconds, Max retries: $maxRetries" -ForegroundColor Cyan
220+ Write-Host "======================================" -ForegroundColor Cyan
221+
208222 Import-Module "$env:ProgramFiles\Azure Cosmos DB Emulator\PSModules\Microsoft.Azure.CosmosDB.Emulator"
209- Start-CosmosDbEmulator -NoUI -Key "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
210- echo "COSMOSDB_EMULATOR_AVAILABLE=true" >> $env:GITHUB_ENV
223+
224+ while (-not $started -and $retryCount -lt $maxRetries) {
225+ $retryCount++
226+
227+ if ($retryCount -gt 1) {
228+ Write-Host "`n--- Retry attempt $retryCount of $maxRetries ---" -ForegroundColor Yellow
229+
230+ # Stop and cleanup before retry
231+ Write-Host "Stopping existing emulator instances..." -ForegroundColor Yellow
232+ Stop-CosmosDbEmulator -ErrorAction SilentlyContinue
233+ Start-Sleep -Seconds 5
234+
235+ # Force kill if still running
236+ Get-Process -Name "Microsoft.Azure.Cosmos.Emulator" -ErrorAction SilentlyContinue | Stop-Process -Force
237+ Start-Sleep -Seconds 3
238+
239+ # Cleanup old data
240+ $dataPath = Join-Path $env:LOCALAPPDATA "CosmosDbEmulator"
241+ if (Test-Path $dataPath) {
242+ Write-Host "Cleaning up emulator data..." -ForegroundColor Yellow
243+ Remove-Item -Recurse -Force $dataPath -ErrorAction SilentlyContinue
244+ Start-Sleep -Seconds 2
245+ }
246+ }
247+
248+ try {
249+ Write-Host "`nStarting emulator (attempt $retryCount)..." -ForegroundColor Cyan
250+ Start-CosmosDbEmulator -NoUI -Key $emulatorKey -AllowNetworkAccess -NoFirewall
251+
252+ # Wait for emulator to be ready with extended timeout
253+ $startTime = [DateTimeOffset]::Now
254+ $stopTime = $startTime.AddSeconds($timeout)
255+ $lastStatus = -1
256+
257+ Write-Host "Waiting for emulator to be ready..." -ForegroundColor Yellow
258+
259+ do {
260+ Start-Sleep -Seconds 5
261+ $elapsed = [int](([DateTimeOffset]::Now - $startTime).TotalSeconds)
262+
263+ # Check status
264+ $statusProcess = Start-Process $emulatorPath -ArgumentList "/getstatus" -PassThru -Wait -NoNewWindow -ErrorAction SilentlyContinue
265+ $statusCode = if ($statusProcess) { $statusProcess.ExitCode } else { -1 }
266+
267+ # Log status changes
268+ if ($statusCode -ne $lastStatus) {
269+ $statusMsg = switch ($statusCode) {
270+ 1 { "Emulator is starting..." }
271+ 2 { "Emulator is running!"; $started = $true }
272+ 3 { "Emulator is stopped" }
273+ default { "Unknown status (code: $statusCode)" }
274+ }
275+ Write-Host "[${elapsed}s] $statusMsg" -ForegroundColor $(if ($statusCode -eq 2) { "Green" } else { "Yellow" })
276+ $lastStatus = $statusCode
277+ }
278+
279+ if ($started) { break }
280+
281+ # Check if process crashed
282+ $processRunning = Get-Process -Name "Microsoft.Azure.Cosmos.Emulator" -ErrorAction SilentlyContinue
283+ if (-not $processRunning -and $statusCode -ne 2) {
284+ Write-Warning "Emulator process is not running"
285+ break
286+ }
287+
288+ } while ([DateTimeOffset]::Now -lt $stopTime)
289+
290+ if (-not $started) {
291+ Write-Error "Emulator failed to start within $timeout seconds"
292+ }
293+ }
294+ catch {
295+ Write-Warning "Error on attempt ${retryCount}: $_"
296+ if ($retryCount -ge $maxRetries) { throw }
297+ }
298+ }
299+
300+ if ($started) {
301+ Write-Host "`n======================================" -ForegroundColor Green
302+ Write-Host "Cosmos DB Emulator started successfully!" -ForegroundColor Green
303+ Write-Host "======================================`n" -ForegroundColor Green
304+
305+ # Verify connectivity
306+ try {
307+ $response = Invoke-WebRequest -Uri "https://localhost:8081/" -UseBasicParsing -SkipCertificateCheck -TimeoutSec 10 -ErrorAction Stop
308+ Write-Host "Emulator is responding (Status: $($response.StatusCode))" -ForegroundColor Green
309+ }
310+ catch {
311+ Write-Warning "Emulator may not be fully ready: $_"
312+ }
313+
314+ echo "COSMOSDB_EMULATOR_AVAILABLE=true" >> $env:GITHUB_ENV
315+ }
316+ else {
317+ Write-Host "`n======================================" -ForegroundColor Red
318+ Write-Host "Failed to start emulator after $maxRetries attempts" -ForegroundColor Red
319+ Write-Host "======================================" -ForegroundColor Red
320+
321+ # Show diagnostic info
322+ $logPath = Join-Path $env:LOCALAPPDATA "CosmosDbEmulator\cosmosdb.log"
323+ if (Test-Path $logPath) {
324+ Write-Host "`nLast 30 lines of emulator log:" -ForegroundColor Yellow
325+ Get-Content $logPath -Tail 30
326+ }
327+
328+ throw "Failed to start Cosmos DB Emulator"
329+ }
211330
212331 - name : Start Azurite Blob service
213332 if : ${{ runner.os == 'Linux' && (needs.paths-filter.outputs.azureStorageChanges == 'true' || (github.event_name != 'pull_request' && matrix.integration-tests)) }}
@@ -351,7 +470,7 @@ jobs:
351470 with :
352471 reports : " ./TestResults/Coverage/**/*.cobertura.xml"
353472 targetdir : " ./TestResults/Reports"
354- reporttypes : " HtmlInline;JsonSummary"
473+ reporttypes : " HtmlInline;JsonSummary;Cobertura "
355474
356475 - name : Upload coverage report artifact
357476 if : matrix.targetFramework == env.COVERAGE_FRAMEWORK
@@ -373,13 +492,41 @@ jobs:
373492 path : IntegrationTestResults/**/*.junit
374493 if-no-files-found : ignore
375494
495+ dotnet-upload-coverage :
496+ needs : [paths-filter, dotnet-test]
497+ if : needs.paths-filter.outputs.dotnetChanges == 'true' && github.event_name != 'merge_group' && ((github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]') || (github.event_name != 'pull_request' && github.ref_name == github.event.repository.default_branch))
498+ runs-on : ubuntu-latest
499+ permissions :
500+ actions : read
501+ contents : read
502+ code-quality : write
503+ pull-requests : read
504+ steps :
505+ - name : Download coverage report artifact
506+ uses : actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
507+ with :
508+ name : CoverageReport-ubuntu-latest-${{ env.COVERAGE_FRAMEWORK }}-Release
509+ path : ./TestResults/Reports
510+ - name : Upload coverage report to GitHub
511+ uses : actions/upload-code-coverage@d8e329117199404bba6fc81efe8093dc7c015e34 # v1.4.2
512+ with :
513+ file : ./TestResults/Reports/Cobertura.xml
514+ language : CSharp
515+ label : code-coverage/dotnet
516+ fail-on-error : false
517+ - name : Summarize GitHub coverage report
518+ run : echo ".NET coverage is uploaded to GitHub's integrated coverage report with label \`code-coverage/dotnet\`." >> "$GITHUB_STEP_SUMMARY"
519+
376520 # The Foundry hosted-agent IT is costly (it builds a container, pushes to ACR, and provisions
377521 # live agents on a separate Foundry project). Running it in its own job keeps the overall
378522 # workflow time roughly flat: it executes in parallel to dotnet-build and dotnet-test and is
379523 # gated on paths-filter.outputs.foundryHostingChanges so unrelated edits skip the work.
380524 dotnet-foundry-hosted-it :
381525 needs : paths-filter
382526 if : github.event_name != 'pull_request' && needs.paths-filter.outputs.foundryHostingChanges == 'true'
527+ permissions :
528+ contents : read
529+ id-token : " write"
383530 runs-on : ubuntu-latest
384531 environment : integration
385532 env :
0 commit comments