一、添加token问题
SonarQube servers中没有添加Server authentication token的文本框。
需要在Jenkins的凭据中添加全局的token。
1、步骤:凭据–>添加凭据–>添加信息并save
2、具体信息填写如下:
3、设置sonarqube server的token
二、pipeline编写错误问题
1、jenkins构建报错信息
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 71: Unknown stage section "node". Starting with version 0.5, steps in a stage must be in a ‘steps’ block. @ line 71, column 3.
stage("build & SonarQube analysis") {
^
WorkflowScript: 79: Unknown stage section "timeout". Starting with version 0.5, steps in a stage must be in a ‘steps’ block. @ line 79, column 9.
stage("Quality Gate"){
^
WorkflowScript: 71: Expected one of "steps", "stages", or "parallel" for stage "build & SonarQube analysis" @ line 71, column 3.
stage("build & SonarQube analysis") {
^
WorkflowScript: 79: Expected one of "steps", "stages", or "parallel" for stage "Quality Gate" @ line 79, column 9.
stage("Quality Gate"){
^
4 errors
2、根据报错信息分析(由于参考:https://www.jianshu.com/p/180af9a15c4b?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation导致的问题)
- 由于build & SonarQube analysis和Quality Gate两个阶段下缺少steps,也就是stage下的内容需要在steps中。
- node节点的错误在我此次的pipeline中没有使用,是应该去掉的。
3、解决办法:
- 去除node{}
- build & SonarQube analysis和Quality Gate两个阶段下添加steps{},并将内容填充在steps中
4、配置实例
jenkins_file
// 第一种代码检查和webhooks方案
stage("build & SonarQube analysis") {
steps{
withSonarQubeEnv('Sonarqube') {
sh 'mvn clean compile sonar:sonar -Dsonar.analysis.branch=qa'
}
}
}
stage("Quality Gate"){
steps{
timeout(time: 15, unit: 'MINUTES') {
waitForQualityGate abortPipeline: true
}
}
}
// 第二种检查和webhooks方案
stage("build & SonarQube analysis") {
steps{
echo 'SonarQube analysis 开始'
withSonarQubeEnv('Sonarqube') {
sh 'mvn clean verify sonar:sonar -Dsonar.analysis.branch=qa'
echo 'SonarQube analysis 结束'
}
script{
timeout(time: 10, unit: 'MINUTES') {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
}
}
三、构建失败、卡住问题(Sonarqube质量门不向jenkins发送webhook)
1、注意
-
若添加Quality Gate步骤必须在sonarqube中添加webhooks,否则会一直监听,导致卡死
-
sonarqube中webhooks 的url添加错误也会导致卡死,由于JK不能回调到sonarqube的监测结果信息
2、示例:
stage("Quality Gate"){
steps{
echo 'Quality Gate 开始'
timeout(time: 15, unit: 'MINUTES') { //添加超时时间,防止卡死在此步骤
waitForQualityGate abortPipeline: true
echo 'Quality Gate 结束'
}
}
}
SonarQube中配置Jenkins的回调URL:http://jkIP:port/sonarqube-webhook/
四、仅扫描master分支
1、概述:由于sonarqube开源版本不支持多分支管理,分析结果的json中,branch始终为master,默认只扫描master分支的代码,若需要扫描其他分支可以通过指定参数解决
2、解决
mvn clean compile sonar:sonar -Dsonar.analysis.branch=分支名称
参考:https://www.cnblogs.com/junejs/p/12692222.html
配置请参考:SonarQube+Jenkins配置使用