- SonarQube是一个用于管理代码质量的开放平台,可以快速的定位代码中潜在的或者明显的错误。
- 目前支持java,C#,C/C++,Python,PL/SQL,Cobol,JavaScrip,Groovy等二十几种编程语言的代码质量管理与检测
- 官网:https://www.sonarqube.org/
环境要求
软件 | 服务器 | 版本 |
---|
JDK | 192.168.100.89 | 1.8 | MySQL | 192.168.100.89 | 5.7 | SonarQube | 192.168.100.89 | 6.7.4 |
安装SonarQube
安装MySQL
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
rpm -ivh mysql80-community-release-el7-3.noarch.rpm
vim /etc/yum.repos.d/mysql-community.repo
yum -y install mysql-community-server*
systemctl start mysqld && systemctl enable mysqld
grep password /var/log/mysqld.log
mysqladmin -uroot -p'w-gp/1LeR5dp' password 'Zxc@1234'
mysql -uroot -p'Zxc@1234'
mysql> create database sonar;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sonar |
| sys |
+--------------------+
5 rows in set (0.00 sec)
安装SonarQube
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-6.7.7.zip
unzip sonarqube-6.7.7.zip
mkdir /usr/local/sonar
mv sonarqube-6.7.7/* /usr/local/sonar/
useradd sonar
chown -R sonar.sonar /usr/local/sonar/
vim /usr/local/sonar/conf/sonar.properties
16 sonar.jdbc.username=root
17 sonar.jdbc.password=Zxc@1234
26 sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=t rue&useConfigs=maxPerformance&useSSL=false
111
su sonar /usr/local/sonar/bin/linux-x86-64/sonar.sh start
tailf /usr/local/sonar/logs/sonar.log
2021.08.19 16:57:45 INFO app[][o.s.a.SchedulerImpl] Process[es] is up
登陆SonarQube,默认有一个账号 admin 密码也是 admin
生成的秘钥需要保存,后续整合Jenkins的时候会使用
27f52ff1f49169419e54acda8a2a1b9fc10bea21
实现代码审查环境配置
安装SonarQube Scanner 插件
Jenkins安装SonarQube Scanner 软件,使用Jenkins UI界面自动安装,或是Linux服务器安装
创建SonarQube 全局凭据
Jenkins配置SonarQube环境,以便Jenkins连接SonarQube
在项目添加SonaQube代码审查(非流水线项目)
# must be unique in a given SonarQube instance
sonar.projectKey=java_demo_freestyle
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=java_demo_freestyle
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=**/testtarget
保存设置后构建项目
进入SonarQube 查看代码审查结果
在项目添加SonaQube代码审查(流水线项目)
在项目根目录下,创建sonar-project.properties 文件,文件名字唯一
# must be unique in a given SonarQube instance
sonar.projectKey=java_demo_pipline
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=java_demo_pipline
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=**/testtarget
更改Jenkinsfile 文件
pipeline {
agent any
stages {
stage('pull code') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: '911f1631-98e1-49b5-93eb-f4ef6d4dafe2', url: 'git@192.168.100.88:pakho_group/java_demo.git']]])
}
}
stage('code checking') {
steps {
script {
scannerHome = tool 'sonar-scanner'
}
withSonarQubeEnv('sonarqube') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
stage('build project') {
steps {
sh 'mvn clean package'
}
}
stage('publish project') {
steps {
deploy adapters: [tomcat8(credentialsId: '18c54ca2-ffd9-438a-b4dc-09fab43d8ef3', path: '', url: 'http://192.168.100.90:8080')], contextPath: null, war: 'target/*.war'
}
}
}
post {
always {
emailext (
subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',
body: '${FILE,path="email.html"}',
to: 'xxx@qq.com'
)
}
}
}
分别Push两个文件至Gitlab 仓库
完成后重新构建项目
返回SonarQube 查看代码审查结果,成功执行审查
|