工作流程图
1 jenkins安装SonarQube Scanner插件
2 jenkins添加SonarQube凭证
3 Manage Jenkins->Configure System->SonarQube servers
4 Manage Jenkins->Global Tool Configuration
5 SonaQube-web界面中关闭审查结果上传到SCM功能
6 项目根目录下,创建sonar-project.properties文件
sonar.projectKey=dockerimage_test
sonar.projectName=dockerimage_test
sonar.projectVersion=1.0
sonar.language=java
sonar.sourceEncoding=UTF-8
sonar.sources=.
sonar.java.binaries=target/classes
7 项目根目录下,创建文件Jenkinsfile,注意引用的名称要和步骤3,4的相同
pipeline {
agent any
stages {
stage('gitlab拉取代码') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[credentialsId: 'gitlab-ssh-user', url: 'git@118.31.52.50:root/dockerimage_test.git']]])
}
}
stage('编译项目') {
steps {
sh "mvn clean package -DskipTests"
}
}
stage('SonarQube代码审查') {
steps {
script {
scannerHome = tool 'SonarQube-Scanner'
}
withSonarQubeEnv('SonarQube7.4') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
}
}