GitLab无代码变更时SonarQube代码覆盖率计算偶发失败求助
问题描述
GitLab流水线包含SonarQube检查步骤,按固定时间间隔自动运行。在项目无任何代码变更的情况下,偶发出现SonarScanner执行失败,提示整体代码覆盖率低于80%导致质量门不通过,但重新运行流水线即可恢复正常。
错误日志
INFO: EXECUTION FAILURE INFO: ------------------------------------------------------------------------ INFO: Total time: 9.987s INFO: Final Memory: 17M/360M INFO: ------------------------------------------------------------------------ ERROR: Error during SonarScanner execution ERROR: QUALITY GATE STATUS: FAILED - View details on http://sonarqube...... In the link I have on overall code coverage is less than 80% error. But this is not correct because when I rerun the pipeline it works properly.
GitLab CI配置
sonarqube-check: stage: check script: - '& $SONAR_SCANNER_EXE begin /k:$SONAR_KEY /n:$SONAR_KEY /d:sonar.host.url=$SONAR_HOST_URL /d:sonar.login=$SONAR_TOKEN /d:sonar.cs.opencover.reportsPaths=coverage.opencover.xml /d:sonar.cs.nunit.reportsPaths=TestResult.xml /d:sonar.qualitygate.wait=true /d:sonar.qualitygate.timeout=180' - 'nuget restore -configfile $NUGET_CONFIG_PATH' # restore NuGet dependencies - 'msbuild -p:Configuration=Release -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg .\$VS_SOLUTION_FILE' - '& $SONAR_SCANNER_EXE end /d:sonar.login=$SONAR_TOKEN' tags: - cf-aws-windows-runner only: - main - merge_requests dependencies: - build_job
排查方向建议
验证覆盖率报告完整性:在SonarScanner执行前,添加步骤检查
coverage.opencover.xml是否存在且内容正常。比如在nuget restore前加入:if (!(Test-Path .\coverage.opencover.xml)) { Write-Error "覆盖率报告文件不存在" exit 1 } if ((Get-Item .\coverage.opencover.xml).Length -lt 1000) { Write-Error "覆盖率报告文件大小异常,可能为空或不完整" exit 1 }避免因报告文件缺失/不完整导致SonarQube计算错误覆盖率。
调整SonarQube质量门等待超时:当前配置
sonar.qualitygate.timeout=180(3分钟),如果SonarQube服务器在定时任务时段负载较高,可能导致计算延迟。尝试延长超时时间至5分钟(300),确保质量门计算完成后再返回结果。检查Runner资源状态:Windows runner是否存在并发任务过多导致磁盘IO、内存不足的情况?查看Runner日志,确认失败时段是否有资源占用过高的警告,必要时增加Runner资源或限制并发数。
确认报告路径准确性:Windows环境下相对路径可能存在解析问题,建议在
sonar.cs.opencover.reportsPaths中使用绝对路径,或在begin步骤添加/d:sonar.projectBaseDir=$PWD指定工作目录,确保Scanner能正确找到报告文件。清除SonarQube项目缓存:手动清除对应项目的SonarQube缓存,排查是否因缓存数据异常导致偶发计算错误。
内容的提问来源于stack exchange,提问作者Aylin Çelik

