持續集成平臺與SonarQube質量平臺集成實踐(三)

持續集成平臺與SonarQube質量平臺集成實踐(三)


持續集成平臺與SonarQube質量平臺集成實踐(三)

引導

您好,本章主要我們主要學習與質量管理平臺Sonarqube集成。主要包含以下內容:1. 使用sonarqube接口 2. 配置多分支掃描

適合人群: 所有對DevOps感興趣的同學。


使用SonarQube接口

為什麼要使用接口? 不知道大家有沒有在使用SonarQube進行掃描的時候遇到這種問題: 當存在多個質量閾的時候,默認是給新項目配置默認的質量閾。也就是第一次掃描的項目直接使用的是默認的質量閾,而不是自己定義的質量閾。這種場景下就需要掃描前先指定質量閾再進行掃描了!


調研接口

可參考官方的文檔,找到自己要操作的接口及使用方法。

<code>//查找項目
api/projects/search?projects=${projectName}"

//創建項目

api/projects/create?name=${projectName}&project=${projectName}"

//更新語言規則集
api/qualityprofiles/add_project?language=${language}&qualityProfile=${qualityProfile}&project=${projectName}"

//項目授權
api/permissions/apply_template?projectKey=${projectKey}&templateName=${templateName}"

//更新質量閾
api/qualitygates/select?projectKey=${projectKey}&gateId=${gateId}"/<code>

共享庫封裝

<code>package org.devops


//封裝HTTP

def HttpReq(reqType,reqUrl,reqBody){
def sonarServer = "http://xxxxxxx/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

}

//搜索Sonar項目
def SerarchProject(projectName){
apiUrl = "projects/search?projects=${projectName}"
response = HttpReq("GET",apiUrl,'')

response = readJSON text: """${response.content}"""
result = response["paging"]["total"]

if(result.toString() == "0"){
return "false"
} else {
return "true"
}
}

//創建Sonar項目
def CreateProject(projectName){
apiUrl = "projects/create?name=${projectName}&project=${projectName}"
response = HttpReq("POST",apiUrl,'')
println(response)
}

//配置項目質量規則

def ConfigQualityProfiles(projectName,lang,qpname){
apiUrl = "qualityprofiles/add_project?language=${lang}&project=${projectName}&qualityProfile=${qpname}"
response = HttpReq("POST",apiUrl,'')
println(response)
}


//獲取質量閾ID
def GetQualtyGateId(gateName){
apiUrl= "qualitygates/show?name=${gateName}"
response = HttpReq("GET",apiUrl,'')
response = readJSON text: """${response.content}"""
result = response["id"]

return result
}

//配置項目質量閾

def ConfigQualityGates(projectName,gateName){
gateId = GetQualtyGateId(gateName)
apiUrl = "qualitygates/select?gateId=${gateId}&projectKey=${projectName}"

response = HttpReq("POST",apiUrl,'')
println(response)println(response)
}/<code>


應用實踐

<code>stage("QA"){
steps {
/> //搜索項目
result = sonarapi.SerarchProject("${JOB_NAME}")
println(result)

//判斷項目是否存在
if (result == "false"){
println("${JOB_NAME}---項目不存在,準備創建項目---> ${JOB_NAME}!")
sonarapi.CreateProject("${JOB_NAME}")
} else {
println("${JOB_NAME}---項目已存在!")
}

//配置項目質量規則
qpName="${JOB_NAME}".split("-")[0] //Sonar%20way
sonarapi.ConfigQualityProfiles("${JOB_NAME}","java",qpName)

//配置質量閾
sonarapi.ConfigQualityGates("${JOB_NAME}",qpName)

//代碼掃描
sonar.SonarScan("test","${JOB_NAME}","${JOB_NAME}","src")


sleep 30
//獲取掃描結果
result = sonarapi.GetProjectStatus("${JOB_NAME}")


println(result)
if (result.toString() == "ERROR"){
error " 代碼質量閾錯誤!請及時修復!"

} else {
println(result)
}
}
}
}/<code>


SonarQube配置多分支

將插件放到兩個目錄中,然後重啟sonar


持續集成平臺與SonarQube質量平臺集成實踐(三)

掃描參數增加 –Dsonar.branch.name=


持續集成平臺與SonarQube質量平臺集成實踐(三)

效果


持續集成平臺與SonarQube質量平臺集成實踐(三)




持續集成平臺與SonarQube質量平臺集成實踐(三)


持續集成平臺與SonarQube質量平臺集成實踐(三)


分享到:


相關文章: