React Native项目Bitbucket Pipeline中SonarQube质量门检查超时求助
React Native项目Bitbucket Pipeline集成SonarQube质量门超时解决方案
问题描述
在React Native项目的Bitbucket Pipeline中执行SonarQube扫描时,触发Quality Gate check timeout exceeded错误,导致流水线执行失败,本次扫描总耗时达6分26秒,超出默认超时限制。
现有配置
bitbucket-pipelines.yml
pipelines: branches: final_design: - step: name: final_design_sonar image: node:16 script: - pipe: sonarsource/sonarqube-scan:1.1.0 variables: SONAR_HOST_URL: ${SONAR_HOST_URL} SONAR_TOKEN: ${SONAR_TOKEN}
sonar-project.properties
sonar.projectKey=battle-buddies-rn-app sonar.exclusions=.github/**, .vscode/**, android/**, assets/**, build/**, ios/**, node_modules/**, scripts/** sonar.qualitygate.wait=true
解决方案
1. 延长质量门等待超时时间
SonarQube默认质量门等待超时为5分钟,可通过sonar.qualitygate.timeout参数自定义超时时长(单位:秒)。修改sonar-project.properties:
sonar.projectKey=battle-buddies-rn-app sonar.exclusions=.github/**, .vscode/**, android/**, assets/**, build/**, ios/**, node_modules/**, scripts/** sonar.qualitygate.wait=true sonar.qualitygate.timeout=720 # 设置为12分钟,可根据实际扫描时长调整
2. 优化扫描效率,缩短总执行时间
- 升级SonarQube扫描管道版本:当前使用的
sonarsource/sonarqube-scan:1.1.0版本较旧,建议升级到最新稳定版(如2.2.0),新版本通常包含性能优化:- pipe: sonarsource/sonarqube-scan:2.2.0 - 增加Pipeline资源配额:在step中添加
size: 2x,为容器分配更多CPU和内存,加快扫描速度:- step: name: final_design_sonar image: node:16 size: 2x # 启用2倍资源配置 script: - pipe: sonarsource/sonarqube-scan:2.2.0 variables: SONAR_HOST_URL: ${SONAR_HOST_URL} SONAR_TOKEN: ${SONAR_TOKEN} - 细化扫描排除规则:确认是否还有其他无需扫描的文件(如测试报告、临时缓存文件等),进一步缩小扫描范围,减少分析时间。
3. 检查SonarQube服务器状态
- 排查SonarQube服务器的CPU、内存负载,若服务器资源不足,会直接导致质量门分析延迟;
- 查看SonarQube任务队列,确认是否有积压任务影响当前分析的处理速度;
- 验证SonarQube与Bitbucket的网络连通性,排除网络延迟导致的结果返回缓慢问题。
4. 分离扫描与质量门检查(可选)
若不想让质量门等待阻塞流水线,可将扫描和质量门检查拆分为两个独立步骤:
- 第一步仅执行SonarQube扫描,不等待质量门结果;
- 第二步通过SonarQube API轮询质量门状态,完成后判断是否失败。
示例配置:
pipelines: branches: final_design: - step: name: SonarQube 扫描 image: node:16 size: 2x script: - pipe: sonarsource/sonarqube-scan:2.2.0 variables: SONAR_HOST_URL: ${SONAR_HOST_URL} SONAR_TOKEN: ${SONAR_TOKEN} SONAR_QUALITYGATE_WAIT: 'false' # 禁用扫描时等待质量门 - step: name: 质量门检查 image: curlimages/curl:latest script: - | # 安装jq用于解析JSON apk add --no-cache jq # 获取最新成功分析的任务ID TASK_ID=$(curl -u "${SONAR_TOKEN}:" "${SONAR_HOST_URL}/api/ce/tasks?componentKey=battle-buddies-rn-app&status=SUCCESS" | jq -r '.tasks[0].id') # 轮询任务完成状态 while true; do TASK_STATUS=$(curl -u "${SONAR_TOKEN}:" "${SONAR_HOST_URL}/api/ce/task?id=${TASK_ID}" | jq -r '.task.status') if [ "$TASK_STATUS" = "SUCCESS" ]; then break elif [ "$TASK_STATUS" = "FAILED" ]; then echo "SonarQube分析任务失败" exit 1 fi echo "等待分析完成...当前状态: ${TASK_STATUS}" sleep 10 done # 检查质量门结果 GATE_STATUS=$(curl -u "${SONAR_TOKEN}:" "${SONAR_HOST_URL}/api/qualitygates/project_status?projectKey=battle-buddies-rn-app" | jq -r '.projectStatus.status') if [ "$GATE_STATUS" != "OK" ]; then echo "质量门检查未通过" exit 1 fi echo "质量门检查通过"
内容的提问来源于stack exchange,提问作者fayir
相关产品推荐
相关产品推荐

