> 文章列表 > Jenkins集成SonarQube实现代码质量检查

Jenkins集成SonarQube实现代码质量检查

Jenkins集成SonarQube实现代码质量检查

文章目录

    • 一、前提配置
      • 1.1 安装及配置SonarQube Scanner插件
      • 1.2 配置SonarQube servers
    • 二、非流水线集成SonarQube
      • 1.1 配置非流水线任务
    • 三、流水线集成SonarQube

一、前提配置

1.1 安装及配置SonarQube Scanner插件

(1) 点击【系统管理】>【插件管理】>【可选插件】搜索SonarQube Scanne点击安装
Jenkins集成SonarQube实现代码质量检查
(2)点击【系统管理】>【全局工具配置】搜索SonarQube Scanner新建一个自动安装
Jenkins集成SonarQube实现代码质量检查

1.2 配置SonarQube servers

(1)使用admin用户登入sonar平台,点击【右上角用户头像】>【我的账号】>【安全】生成一个新的令牌,复制令牌内容后面会使用到
Jenkins集成SonarQube实现代码质量检查
(2)新建凭证:点击【系统管理】>【Manager files】>【全局】创建sonar凭证
Jenkins集成SonarQube实现代码质量检查
(3)点击【系统管理】>【系统配置】搜索到 SonarQube servers 进行配置
Jenkins集成SonarQube实现代码质量检查
(4)SonarQube关闭审查上传SCM功能
Jenkins集成SonarQube实现代码质量检查

二、非流水线集成SonarQube

1.1 配置非流水线任务

Jenkins集成SonarQube实现代码质量检查

# 项目唯一标记
sonar.projectKey=web_JAR
# 项目名字
sonar.projectName=web_JAR
# 版本号
sonar.projectVersion=1.0
# 指定扫描目录 点表示项目根目录
sonar.sources=.
# 对test、target目录不进行扫描
sonar.exclusions=/test/,/target/
# JDK设置
sonar.java.source=1.8
sonar.java.target=1.8
# 字符集
sonar.sourceEncoding=UTF-8

三、流水线集成SonarQube

(1) 项目根目录,创建sonar-project.properties文件

# must be unique in a given SonarQube instance
sonar.projectKey=web_demo
# this is the name and version displayed in the SonarQube UI. Was mandatory
prior to SonarQube 6.1.
sonar.projectName=web_demo
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\\" by "/" on
Windows.
# This property is optional if sonar.modules is set.
sonar.sources=.
sonar.exclusions=/test/,/target/
sonar.java.source=1.8
sonar.java.target=1.8
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8

流水线代码:

pipeline {agent anystages {stage('拉取代码') {steps {checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[url: 'git@152.136.184.149:gitlab-instance-0d7d20f4/cicd.git']]])}}stage('打JAR包') {steps {sh 'mvn clean package'}}stage('SonarQube代码审查') {steps{script {//引入Jenkins SonarQube-Scanner全局工具 "全局配置中以SonarQube-Scanner命名的工具"scannerHome = tool 'SonarQube-Scanner'}//引用SonarQube环境 "系统配置中配置的SonarQube servers的name值 "withSonarQubeEnv('sonar') {//执行sonar-scanner命令sh "${scannerHome}/bin/sonar-scanner"}}}stage('部署前操作') {steps {sshPublisher(publishers: [sshPublisherDesc(configName: '120.48.34.146', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''source /etc/profile
bash /home/qinzt/jar/helloword/run.sh stop helloword-*.jar
mv /home/qinzt/jar/helloword/helloword-*.jar{,.$(date +%F-%S)}''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])}}stage('远程部署') {steps {sshPublisher(publishers: [sshPublisherDesc(configName: '120.48.34.146', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''source /etc/profile
cd /home/qinzt/jar/helloword && ./run.sh start helloword-*.jar''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'jar/helloword', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: '/helloword-*.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])}   }}post {//构建失败发邮件通知unsuccessful  {emailext attachLog: true, body: '${FILE,path="email.html"}', subject: '【构建通知】:${PROJECT_NAME}', to: '1790168505@qq.com'}}
}