Jfrog 与 jenkins Sonarqube的 测试样例 (From jfrog 培训)

时间:2022-05-23 01:39:06

1. 获取jfrog 仓库地址 sonar的地址 jenkins的地址.

本次例子的地址为:

jenkins地址:http://10.24.101.99:8080/
artifactory地址: http://10.24.101.99:8081/artifactory
sonar的地址:http://10.10.9.36:9000/
sonar自己一会儿安装一个

2. 进行配置,首先在 系统管理-系统设置 中增加sonar的服务器的地址

如图示:

服务器session的值为:

ffb8d267e6f0eb2fdbbcabbb097cebb0b7fbaa76

Jfrog 与 jenkins Sonarqube的 测试样例 (From jfrog 培训)

3 在系统管理-全局工具配置中 增加sonarqube的值

这一块 需要与 groovy中的脚本关联起来 才可以用.

Jfrog 与 jenkins Sonarqube的 测试样例 (From jfrog 培训)

4. 设置完成之后 创建 pipeline

与上面一个 基本相同.

Jfrog 与 jenkins Sonarqube的 测试样例 (From jfrog 培训)

将测试用的脚本放进去 ,并且取消掉 沙盒运行

import groovy.json.JsonOutput
import groovy.json.JsonSlurperClassic node {
def server = Artifactory.newServer url: SERVER_URL, credentialsId: CREDENTIALS
def rtMaven = Artifactory.newMavenBuild() stage 'Build'
git url: 'https://github.com/wj20160308/project-examples.git' stage 'Artifactory configuration' rtMaven.tool = MAVEN_TOOL // Tool name from Jenkins configuration
rtMaven.deployer releaseRepo:'zhaobsh_mvn_release_local', snapshotRepo:'zhaobsh_mvn_snapshot_local', server: server
rtMaven.resolver releaseRepo:'libs-release', snapshotRepo:'libs-snapshot', server: server
rtMaven.deployer.addProperty("unit-test", "pass").addProperty("qa-team", "platform", "ui")
def buildInfo = Artifactory.newBuildInfo()
buildInfo.env.capture = true stage 'Exec Maven'
rtMaven.run pom: 'maven-example/pom.xml', goals: 'clean install', buildInfo: buildInfo stage 'sonar'
def scannerHome = tool 'sonarqube'; withSonarQubeEnv('sonar') {
echo "${scannerHome}" sh "${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=${JOB_NAME} -Dsonar.projectName=${JOB_NAME} -Dsonar.projectVersion=${BUILD_NUMBER} -Dsonar.sources= "
//sh "mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar"
} stage 'Publish & Scan' manager.envVars['RELEASE_VERSION'] = RELEASE_VERSION ;
def releaseVersion = RELEASE_VERSION;
def warVersion = releaseVersion;
manager.listener.logger.println(manager.envVars['RELEASE_VERSION'] ); def curlstr="curl -uadmin:AKCp5aUjBmuJPiBcGcjvYDWi7jrgWESiMyKKw1qb28aevmeZoSYWPNg8tkZyUzZyZF5RxyNL5 \'http://10.10.9.33/artifactory/"
def warverstr= curlstr + "api/search/latestVersion?g=org.jfrog.test&a=multi3&repos=wangj-snapshots-local&v=${releaseVersion}\'";
echo warverstr
warVersion = ["bash","-c",warverstr].execute().text
echo warVersion //获取sonar扫描结果
def getSonarIssuesCmd = "curl GET -v http://10.10.9.36:9000/api/issues/search?componentRoots=${JOB_NAME}";
echo getSonarIssuesCmd
process = [ 'bash', '-c', getSonarIssuesCmd].execute().text //增加sonar扫描结果到artifactory
echo process
//def issueMap = jsonSlurper.parseText(process);
def issueMap = jsonParse(process);
commandText = "curl -X PUT \"http://10.10.9.33/artifactory/api/storage/wangj-snapshots-local/org/jfrog/test/multi3/"+releaseVersion+"-SNAPSHOT/multi3-"+releaseVersion + warVersion+".war?properties=qulity.gate.sonarUrl=http://47.93.114.82:9000/dashboard/index/jfrog:multi3;qulity.gate.sonarIssue="+issueMap.total+"\" -uadmin:AKCp5aUjBmuJPiBcGcjvYDWi7jrgWESiMyKKw1qb28aevmeZoSYWPNg8tkZyUzZyZF5RxyNL5"; echo commandText
process = [ 'bash', '-c', commandText].execute().text step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml'])
if (reportOnTestsForBuild ()) {
currentBuild.result = 'UNSTABLE'
}
server.publishBuildInfo buildInfo
} @NonCPS
def reportOnTestsForBuild () {
def failedTests = []
def build = manager.build
if (build.getAction(hudson.tasks.junit.TestResultAction.class) == null) {
println "No Tests"
return true
}
def result = build.getAction(hudson.tasks.junit.TestResultAction.class).result
if ((result == null)) {
println "No test results"
return true
} else {
println "Failed test count: " + result.getFailCount()
println "Passed test count: " + result.getPassCount()
failedTests = result.getFailedTests()
failedTests.each { test ->
println test.name
}
return (result.getFailCount())
}
}
@NonCPS
def jsonParse(def json) {
new groovy.json.JsonSlurperClassic().parseText(json) }

放置位置

Jfrog 与 jenkins Sonarqube的 测试样例 (From jfrog 培训)

选择 参数化 构建过程

Jfrog 与 jenkins Sonarqube的 测试样例 (From jfrog 培训)

增加参数 与后面的groovy脚本对应

首先增加 artifactory 的地址

Jfrog 与 jenkins Sonarqube的 测试样例 (From jfrog 培训)

增加的参数为:

Jfrog 与 jenkins Sonarqube的 测试样例 (From jfrog 培训)

以及进行 MAVEN_TOOL的设置

Jfrog 与 jenkins Sonarqube的 测试样例 (From jfrog 培训)

进行添加

Jfrog 与 jenkins Sonarqube的 测试样例 (From jfrog 培训)

后续还要添加的参数有

sonarqube 值就为 刚才设置的系统设置变量名字
RELEASE_VERSION 定义一个任意数据即可

进行处理即可.

这里需要在aritifactory 中创建 仓库 比较简单 上一个blog中 已经说明过了. 不在阐述.