1.创建质量域
2.添加条件,关联项目
3.添加SonarQube凭证
4.安装Pipeline Utility Steps插件
5.sonarapi.groovy共享库
package org.devops //封装HTTP def HttpReq(reqType,reqUrl,reqBody){ def sonarServer = "http://192.168.164.158:9000/api" result = httpRequest authentication: 'sonar-admin-user', httpMode: reqType, contentType: "APPLICATION_JSON", consoleLogResponseBody: true, ignoreSslErrors: true, requestBody: reqBody, url: "${sonarServer}/${reqUrl}" //quiet: true return result } //获取Sonar质量阈状态 def GetProjectStatus(projectName){ apiUrl = "project_branches/list?project=${projectName}" response = HttpReq("GET",apiUrl,'') response = readJSON text: """${response.content}""" result = response["branches"][0]["status"]["qualityGateStatus"] //println(response) return result }
6.jenkinsfile调用sonarapi
def sonarapi = new org.devops.sonarapi() //获取扫描结果 tools.PrintMes("获取扫描结果","green") result = sonarapi.GetProjectStatus("$JOB_NAME") println(result) //判断返回值是否Error if(result.toString() == "ERROR"){ toemail.Email("代码质量域错误,请及时修复!",userEmail) error "代码质量域错误,请及时修复!" }else{ println(result) }
7.构建测试
标签:SonarQube,使用,result,sonarapi,println,true,response,def,质量 From: https://www.cnblogs.com/wuxinchun/p/16799320.html