有手就行10——Jenkins+SonarQube代码审查
Jenkins+SonarQube代码审查(1) - 安装SonarQube
Jenkins+SonarQube代码审查(2) - 实现代码审查
Jenkins+SonarQube代码审查(1) - 安装SonarQube
SonarQube是一个用于管理代码质量的开放平台,可以快速的定位代码中潜在的或者明显的错误。
目前 支持java,C#,C/C++,Python,PL/sql,Cobol,JavaScrip,Groovy等二十几种编程语言的代码质量管理与检测,
底层使用elasticsearch作为代码检索工具。
实验环境:(与jenkins 在同一台服务器)主要是快
软件 |
服务器 |
版本 |
JDK |
20.0.0.30 |
1.8 |
20.0.0.30 |
5.7 |
|
SonarQube |
20.0.0.30 |
6.7.4 |
安 装 SonarQube
1)安装MysqL(已完成)不过多讲解。
2)安装SonarQube
下载sonar压缩包:
https://www.sonarqube.org/downloads/
解压sonar,并设置权限
yum install unzip #(已装) unzip sonarqube-6.7.4.zip #解压 mkdir /opt/sonar #创建目录 mv sonarqube-6.7.4/* /opt/sonar #移动文件 useradd sonar #创建sonar用户,必须sonar用于启动,否则报错 chown -R sonar. /opt/sonar #更改sonar目录及文件权限
cd /opt/sonar vim sonar/conf/sonar.properties
内容如下: sonar.jdbc.username=root sonar.jdbc.password=abc123 sonar.jdbc.url=jdbc:MysqL://localhost:3306/sonar? useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs= maxPerformance&useSSL=false (取消注释即可)
注意:sonar默认监听9000端口,如果9000端口被占用,需要更改。 启动sonar(注意:切换sonar用户)
cd /opt/sonar
su sonar ./bin/linux-x86-64/sonar.sh start #启动 su sonar ./bin/linux-x86-64/sonar.sh status #查看状态 su sonar ./bin/linux-x86-64/sonar.sh stop #停止 tail -f logs/sonar.logs #查看日志
访 问 sonar:
http://20.0.0.30:9000
默认账户:admin/admin 创建token
lvbu: 5013051625e85359eca937815c59a2da393707a5(和Jenkins整合会使用此密钥)
token要记下来!!!
Jenkins+SonarQube代码审查(2) - 实现代码审查
安装SonarQube Scanner插件
安装SonarQube
添加SonarQube凭证
Jenkins进行SonarQube配置
Manage Jenkins->Configure System->SonarQube servers
在项目添加SonaQube代码审查(非流水线项目)
以自由风格为例:打开
# must be unique in a given SonarQube instance sonar.projectKey=web_demo_freestyle # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1. sonar.projectName=web_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=**/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
然后构建:
在sonarqube服务器上刷新,查看结果
测试错误代码
新建Java和resource目录
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> </dependency>
新建编写Servlet文件
创建名称:com.lvbu.HelloServlet
内容如下:
ackage com.lvbu; import javax.servlet.servletexception; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws servletexception, IOException { super.doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws servletexception, IOException { //模拟错误代码 int i = 100/0; //模拟代码冗余 int j = 100; j = 200; resp.getWriter().write("hello Servlet"); } }
然后代码提交:
然后构建:看结果:
其中可能会报错:
解决方法:
然后再次提交项目加构建:
代码检查后就可以了!
在项目添加SonaQube代码审查(流水线项目)
1) 项目根目录下,创建sonar-project.properties文件
# must be unique in a given SonarQube instance
sonar.projectKey=web_demo_lsx
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=web_demo_lsx
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
2) 修改Jenkinsfile,加入SonarQube代码审查阶段
pipeline {
agent any
stages {
stage('pull code') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: 'd5bb0e98-15f2-477f-8db7-2c33ecc6c644', url: 'git@20.0.0.20:niuma/web_demo.git']]])
}
}
stage('code checking') {
steps {
script {
//引入了sonarqube-scanner工具
scannerHome = tool 'sonar-scanner'
}
//引入了sonarqube服务器系统环境
withSonarQubeEnv('sonarqube') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
stage('build project') {
steps {
sh 'mvn clean package'
}
}
stage('deploy item') {
steps {
deploy adapters: [tomcat8(credentialsId: '38dcb730-8901-41bb-b8d0-d1500aa9cf79', path: '', url: 'http://20.0.0.40:8080/')], contextpath: null, war: 'target/*.war'
}
}
}
post {
always {
emailext(
subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',body: '${FILE,path="email.html"}', to: '1321519531@qq.com'
)
}
}
}
把更改后的sonar-project.properties和Jenkinsfile进行提交
然后开始构建:
查看测试结果: