目录
1.Jenkins使用GitLab共享库实现自动更新后端项目质量配置
2.Jenkins使用GitLab共享库实现自动更新前端项目质量配置
一、实验
1.Jenkins使用GitLab共享库实现自动更新后端项目质量配置
(1)修改GitLab的Sonar.groovy
(2) 配置文件并提交
Groovy
package org.devops
def SonarRequest(apiUrl,method){
withCredentials([string(credentialsId: "855da77c-ad38-4f06-a941-3a5afcf6bbc6", variable: 'SONAR_TOKEN')]) {
sonarApi = "http://192.168.204.8:9000/api"
response = sh returnStdout: true,
script: """
curl --location \
--request ${method} \
"${sonarApi}/${apiUrl}" \
--header "Authorization: Basic ${SONAR_TOKEN}"
"""
// json格式化
try {
response = readJSON text: """ ${response - "\n"} """
} catch(e){
response = readJSON text: """{"errors" : true}"""
}
return response
}
}
//查找项目
def SearchProject(projectName){
apiUrl = "projects/search?projects=${projectName}"
response = SonarRequest(apiUrl,"GET")
if (response.paging.total == 0){
return false
}
return true
}
//创建项目
def CreateProject(projectName){
apiUrl = "projects/create?name=${projectName}&project=${projectName}"
response = SonarRequest(apiUrl,"POST")
try{
if (response.project.key == projectName ) {
println("Project Create success!...")
return true
}
}catch(e){
println(response.errors)
return false
}
}
// 更新质量阈
def UpdateQualityProfiles(lang, projectName, profileName){
apiUrl = "qualityprofiles/add_project?language=${lang}&project=${projectName}&qualityProfile=${profileName}"
response = SonarRequest(apiUrl,"POST")
if (response.errors != true){
println("ERROR: UpdateQualityProfiles ${response.errors}...")
return false
} else {
println("SUCCESS: UpdateQualityProfiles ${lang} > ${projectName} > ${profileName}" )
return true
}
}
def SonarJava(projectName,groupName){
withCredentials([string(credentialsId: '9a684e00-bc7b-4335-916f-1dfb0b1688fa', variable: 'AUTH_TOKEN')]) {
sh """
/opt/sonar-scanner-4.8.0.2856-linux/bin/sonar-scanner -Dsonar.host.url=http://192.168.204.8:9000 \
-Dsonar.projectKey=${projectName} \
-Dsonar.projectName=${projectName} \
-Dsonar.projectVersion=${BUILD_ID} \
-Dsonar.login=${AUTH_TOKEN} \
-Dsonar.ws.timeout=30 \
-Dsonar.projectDescription="my first project" \
-Dsonar.links.homepage=http://192.168.204.8:82/${groupName}/${projectName} \
-Dsonar.links.ci=http://192.168.204.15:8080/job/${groupName}/job/${projectName}/ \
-Dsonar.sources=src \
-Dsonar.sourceEncoding=UTF-8 \
-Dsonar.java.binaries=target/classes \
-Dsonar.java.test.binaries=target/test-classes \
-Dsonar.java.surefire.report=target/surefire-reports
"""
}
}
def SonarWeb(projectName,groupName){
withCredentials([string(credentialsId: '9a684e00-bc7b-4335-916f-1dfb0b1688fa', variable: 'AUTH_TOKEN')]) {
sh """
/opt/sonar-scanner-4.8.0.2856-linux/bin/sonar-scanner -Dsonar.host.url=http://192.168.204.8:9000 \
-Dsonar.projectKey=${projectName} \
-Dsonar.projectName=${projectName} \
-Dsonar.projectVersion=${BUILD_ID} \
-Dsonar.login=${AUTH_TOKEN} \
-Dsonar.ws.timeout=30 \
-Dsonar.projectDescription="my first project" \
-Dsonar.links.homepage=http://192.168.204.8:82/${groupName}/${projectName} \
-Dsonar.links.ci=http://192.168.204.15:8080/job/${groupName}/job/${projectName}/ \
-Dsonar.sources=src \
-Dsonar.sourceEncoding=UTF-8 \
"""
}
}
//Main
def CodeSonar(type,projectName,groupName){
if (SearchProject(projectName)){
println("${projectName} exist")
} else {
//项目不存在
println("${projectName} not fonud")
//创建项目
CreateProject(projectName)
}
//质量配置
profileMap = ["maven":"java","npm": "js"]
UpdateQualityProfiles(profileMap[type], projectName,groupName)
switch(type){
case "maven":
SonarJava(projectName,groupName)
break;
case "gradle":
SonarJava(projectName,groupName)
break;
case "npm":
SonarWeb(projectName,groupName)
break;
default:
println("No such tools ... [maven/ant/gradle/npm/yarn/go]")
break
}
}
(3)Jenkins手动构建后端项目流水线
(4)Blue Ocean 查看
(5)查看日志
(6)maven项目使用的质量阈为devops03
(7) Sonarqube 已新增项目
2.Jenkins使用GitLab共享库实现自动更新前端项目质量配置
(1)Sonarqube给前端项目添加质量配置
(2)根据需求添加
(3)显示已增加
(4)Jenkins手动构建前端项目流水线
(5)Blue Ocean 查看
(6)查看日志
(7)npm项目使用的质量阈为devops03
(8) Sonarqube 已新增项目
二、问题
1.Sonarqube如何添加自定义质量阈
(1)创建
(2)添加条件
(3)完成
(4)添加项目