DevOps CI/CD 流水线中,在静态代码扫描阶段,偶发 SonarQube 超时,依次尝试修改超时时间为 10 分钟,30 分钟,60 分钟,但实际依然偶发超时。
stage('static check') { steps { echo "starting codeAnalyze with SonarQube......" echo "imageTag:${imageTag}" //sonar:sonar.QualityGate should pass withSonarQubeEnv('sonar') { //固定使用项目根目录${basedir}下的pom.xml进行代码检查 sh "mvn -f pom.xml clean compile sonar:sonar -U" } script { timeout(60) { //利用sonar webhook功能通知pipeline代码检测结果 def qg = waitForQualityGate() if (qg.status != 'OK') { echo "未通过Sonarqube的代码质量阈检查,请及时修改!failure: ${qg.status}" } } } } }
复制代码
查找社区后,发现这是一个 SonarQube 的 bug, 解决方案是在 Sonar Qube 状态确认前,先让 SonarQube 跑一会儿( sleep(5) ),确保 SonarQube Task 成功运行状态。
Need a “sleep” between “withSonarQubeEnv” and “waitForQualityGate” or it spins in “IN PROGRESS”
方案一、更改后 Pipeline stage 如下:
stage('static check') { steps { echo "starting codeAnalyze with SonarQube......" echo "imageTag:${imageTag}" //sonar:sonar.QualityGate should pass withSonarQubeEnv('sonar') { //固定使用项目根目录${basedir}下的pom.xml进行代码检查 sh "mvn -f pom.xml clean compile sonar:sonar -U" } script { timeout(20) { sleep(5) //利用sonar webhook功能通知pipeline代码检测结果 def qg = waitForQualityGate() if (qg.status != 'OK') { echo "未通过Sonarqube的代码质量阈检查,请及时修改!failure: ${qg.status}" } } } } }
复制代码
方案二、实际上,添加 SonarQube 对应的 Webhooks 后从根本上解决问题
评论