共计 1397 个字符,预计需要花费 4 分钟才能阅读完成。
jenkins流水线对接sonarqube需要用到插件:,此处不赘述sonarqube如何部署。。。
配置sonarqube server
此处配置需要用到sonarqube中用户的token,请在sonar中提前准备生成好,然后再jenkins中创建screat text的凭据,此处略
配置sonar-scanner工具
jenkinsfile文件
pipeline {
agent any
tools {
// Install the Maven version configured as "M3" and add it to the path.
maven "maven"
}
stages {
stage('Get code') {
steps {
git url: 'https://gitlab.xadocker.cn/resource/springboot-demo.git', branch: 'main', credentialsId: '867be6fa-88b1-4715-bcd6-3b2ebe671138'
}
}
stage("Build") {
steps {
sh "mvn -Dmaven.test.failure.ignore=true clean package"
}
post {
// If Maven was able to run the tests, even if some of the test
// failed, record the test results and archive the jar file.
success {
// junit '**/target/surefire-reports/TEST-*.xml'
archiveArtifacts 'target/*.jar'
}
}
}
stage('build && SonarQube analysis') {
steps {
script {
scannerHome = tool 'sonarqubescanner2.9'
}
withSonarQubeEnv('sonarqube') {
sh "${scannerHome}/bin/sonar-scanner " +
//在sonar中项目的名称
"-Dsonar.projectKey=helloworld " +
//在sonar中项目的名称
"-Dsonar.projectName=helloworld " +
"-Dsonar.sourceEncoding=UTF-8 " +
//项目类型
"-Dsonar.language=java " +
//需要过滤的文件类型
"-Dsonar.exclusions=**/*.css,**/*.js,**/*.xml " +
//sonar记录版本
"-Dsonar.projectVersion=${JOB_BASE_NAME}-${BUILD_ID} " +
//需要检查代码的路径
"-Dsonar.sources=src/main/java/ " +
//需要检查代码源码路径
"-Dsonar.java.binaries=target/classes"
}
sleep(10)
// 这个睡眠时为了防止没有分析完成就去请求结果
timeout(time: 5, unit: 'MINUTES') {
script {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to a quality gate failure: ${qg.status}"
}
}
}
}
}
}
}
正文完