本文目的在于演示sonarqube+jenkins+svn+maven是如何集成工作的,最后通过一个java项目和一个python项目展示代码质量检测过程。
Sonar (SonarQube)是一个开源平台,用于管理源代码的质量。 Sonar 不只是一个质量数据报告工具,更是代码质量管理平台。 支持java, C#, C/C++, PL/SQL, Cobol, JavaScrip, Groovy 等等二十几种编程语言的代码质量管理与检测。 Sonar可以从以下七个维度检测代码质量,而作为开发人员至少需要处理前5种代码质量问题。
一款CI/CD工具
代码版本管理
Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的项目管理工具软件。
本实验环境主要在一台vmware虚拟机中进行,虚拟机ip:192.168.110.51
192.168.110.51(centos7) |
192.168.110.1(windows7) |
||
安装软件 | 1 | Jenkins-2.150 (https://jenkins.io/download/) |
TortoiseSVN-x64-1.10.0 |
2 | Sonarqube-7.6 (https://www.sonarqube.org/) |
||
3 | Sonar-runner-2.4 (http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip) |
||
4 | subversion -1.7.14 (yum安装) |
||
5 | Maven-3.6.0 (http://maven.apache.org/) |
||
6 | Jdk1.8 (https://www.oracle.com/technetwork/java/javase/downloads/index.html) |
||
7 | Mysql5.6 (https://www.mysql.com/) |
除非特别说明,以下所有操作均在虚拟机192.168.110.51上执行
使用普通用户apps安装
1)查询是否安装svn
[apps@localhost ~]# rpm -qa|grep -i subversion
[apps@localhost ~]#
2)yum安装
[apps@localhost ~]# sudo yum install subversion
3)新建文件夹用于存放svn仓库
[apps@localhost ~]# sudo mkdir -p /apps/svr/svn
[apps@localhost ~]# sudo chown -R apps:apps /apps
4)创建一个版本库
[apps@localhost ~]# svnadmin create /apps/svr/svn/project1
[apps@localhost ~]#
5)修改配置文件
[apps@localhost ~]# cd /apps/svr/svn/project1/conf/
[apps@localhost conf]#
[apps@localhost conf]# vi authz #最后添加
[/]
dyh = rw
[apps@localhost conf]#
[apps@localhost conf]# vi passwd #最后添加
dyh = 123
[apps@localhost conf]# vi svnserve.conf #修改
[general]
anon-access = none
auth-access = write
password-db = passwd
authz-db = authz
realm = project1
6)启动svn
[apps@localhost conf]# svnserve -d -r /apps/svr/svn/
[apps@localhost conf]#
[apps@localhost conf]# sudo netstat -nltp|grep svn
tcp 0 0 0.0.0.0:3690 0.0.0.0:* LISTEN 3046/svnserve
[apps@localhost conf]#
在客户端windows上安装TortoiseSVN-x64-1.10.0,安装完后新建一个文件夹,点右键SVN Checkout
URL输入svn://192.168.110.51/project1,点OK
输入账号密码
出现下面界面,表示svn配置成功
1)当前目录
[apps@localhost svr]$ pwd
/apps/svr
[apps@localhost svr]$
2)上传文件
[apps@localhost svr]$ rz -bye
3)解压文件
[apps@localhost svr]$ tar -zxvf jdk-8u201-linux-x64.tar.gz
4)添加环境变量
[apps@localhost svr]$ vi ~/.bash_profile
JAVA_HOME=/apps/svr/jdk1.8.0_201
PATH=$PATH:$JAVA_HOME/bin
5)使环境变量生效
[apps@localhost svr]$ source ~/.bash_profile
[apps@localhost svr]$
6)查看jdk版本
[apps@localhost svr]$ java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
[apps@localhost svr]$
1)当前目录
[apps@localhost svr]$ pwd
/apps/svr
2)上传文件
[apps@localhost svr]$ rz -bye
rz waiting to receive.
Starting zmodem transfer. Press Ctrl+C to cancel.
Transferring apache-maven-3.6.0-bin.tar.gz...
100% 8851 KB 8851 KB/sec 00:00:01 0 Errors
3)解压
[apps@localhost svr]$ tar zxvf apache-maven-3.6.0-bin.tar.gz
4)修改环境变量
[apps@localhost svr]$ vi ~/.bash_profile
MAVEN_HOME=/apps/svr/apache-maven-3.6.0
PATH=$PATH:$MAVEN_HOME/bin
5)使环境变量生效
[apps@localhost svr]$ source ~/.bash_profile
[apps@localhost svr]$
6)查看maven版本
[apps@localhost svr]$ mvn --version
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T14:41:47-04:00)
Maven home: /apps/svr/apache-maven-3.6.0
Java version: 1.8.0_201, vendor: Oracle Corporation, runtime: /apps/svr/jdk1.8.0_201/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-862.el7.x86_64", arch: "amd64", family: "unix"
[apps@localhost svr]$
3.5.1 jenkins安装
jenkins有两种安装文件,一种是war文件,一种是rpm文件,此处用rpm安装
1)上传文件
[apps@localhost svr]$ rz -bye
2)安装
[apps@localhost svr]$ sudo rpm -ivh jenkins-2.150.3-1.1.noarch.rpm
warning: jenkins-2.150.3-1.1.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID d50582e6: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:jenkins-2.150.3-1.1 ################################# [100%]
[apps@localhost svr]$
3)修改启动脚本, 添加红色部分
[apps@localhost ~]# sudo vi /etc/init.d/jenkins
candidates="
/apps/svr/jdk1.8.0_201/bin/java
/etc/alternatives/java
/usr/lib/jvm/java-1.8.0/bin/java
/usr/lib/jvm/jre-1.8.0/bin/java
/usr/lib/jvm/java-1.7.0/bin/java
/usr/lib/jvm/jre-1.7.0/bin/java
/usr/bin/java
"
4)启动
[apps@localhost ~]$ sudo systemctl daemon-reload
[apps@localhost ~]$
[apps@localhost ~]$ sudo /etc/init.d/jenkins start
Starting jenkins (via systemctl): [ OK ]
[apps@localhost ~]$
5)查看是否启动成功
[apps@localhost ~]$ sudo netstat -nltp|grep java
tcp6 0 0 :::8080 :::* LISTEN 3465/java
[apps@localhost ~]$
[apps@localhost ~]$
6)访问http://192.168.110.51:8080 ,第一次访问的话会进入引导界面,按相应一步一步往下即可。
3.5.2 jenkins插件安装
1)系统管理-->插件管理
2)安装SVN插件及MAVEN插件
3.5.3 配置插件
1)系统管理--> 全局工具配置
添加JDK: /apps/svr/jdk1.8.0_201
添加maven: /apps/svr/apache-maven-3.6.0
2)路径:系统管理-->系统设置, 在工具位置处添加jdk和maven目录
1)安装依赖
[apps@dyh51 mysql]$ sudo yum install perl-Data-Dumper perl perl-devel
2)上传文件
[apps@dyh51 svr]$ rz -bye
rz waiting to receive.
Starting zmodem transfer. Press Ctrl+C to cancel.
Transferring mysql-5.6.25-linux-glibc2.5-x86_64.tar.gz...
100% 305031 KB 4841 KB/sec 00:01:03 0 Errors
[apps@dyh51 svr]$
3)解压
[apps@dyh51 svr]$ tar -zxvf mysql-5.6.25-linux-glibc2.5-x86_64.tar.gz
4)移动目录
[apps@dyh51 svr]$ mv mysql-5.6.25-linux-glibc2.5-x86_64 mysql/
5)创建目录
[apps@dyh51 svr]$ cd mysql
[apps@dyh51 mysql]$ mkdir /apps/data/mysql -p
[apps@dyh51 mysql]$ mkdir -p /apps/logs/mysql/binlog
[apps@dyh51 mysql]$ mkdir -p /apps/run/mysql
[apps@dyh51 mysql]$ mkdir -p /apps/conf/mysql
[apps@dyh51 mysql]$
6)安装数据库
[apps@ dyh51 mysql]$ ./scripts/mysql_install_db --user=apps --basedir=/apps/svr/mysql --datadir=/apps/data/mysql/
7)修改配置文件
[apps@ dyh51 mysql]$ cp my.cnf /apps/conf/mysql/
[apps@dyh51 mysql]$ vi /apps/conf/mysql/my.cnf
[mysqld]
innodb_buffer_pool_size = 256m
basedir = /apps/svr/mysql
datadir = /apps/data/mysql/
port = 3306
socket = /apps/run/mysql/mysql.sock
log-error = /apps/logs/mysql/mysql-error.log
log-bin = /apps/logs/mysql/binlog/mysql-bin
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
binlog-format=MIXED
character-set-server=utf8
[mysqld_safe]
pid-file=/apps/run/mysql/mysql.pid
8)启动
[apps@dyh51 mysql]$ /apps/svr/mysql/bin/mysqld_safe --defaults-file=/apps/conf/mysql/my.cnf --basedir=/apps/svr/mysql/ --datadir=/apps/data/mysql/ &
9)创建sonarqube所用数据库和用户
[apps@dyh51 mysql]$ /apps/svr/mysql/bin/mysql -uroot -h127.0.0.1
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.25-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> create database sonar default charset "utf8";
Query OK, 1 row affected (0.01 sec)
mysql>
mysql> grant all privileges on sonar.* to apps@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql>
1)上传安装包
[apps@dyh51 ~]$ cd /apps/svr
[apps@dyh51 svr]$ rz -bye
2)解压
[apps@dyh51 svr]$ unzip sonarqube-7.6.zip
3)修改配置文件
[apps@dyh51 svr]$ cd /apps/svr/sonarqube-7.6/conf/
[apps@dyh51 conf]$ vi sonar.properties
sonar.jdbc.username=apps
sonar.jdbc.password=123456
sonar.jdbc.url=jdbc:mysql://192.168.110.51:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.web.host=0.0.0.0
sonar.web.port=9000
4)修改系统参数
[apps@dyh51 conf]$ sudo vi /etc/security/limits.conf #在文件末尾添加
apps hard nofile 65536
apps soft nofile 65536
[apps@dyh51 conf]$
[apps@dyh51 conf]$ sudo vi /etc/sysctl.conf #在文件末尾添加
vm.max_map_count=262144
5)使参数生效
[apps@dyh51 conf]$ sudo sysctl -p
vm.max_map_count = 262144
[apps@dyh51 conf]$
6)启动sonarqube
[apps@dyh51 ~]$ cd /apps/svr/sonarqube-7.6/bin/linux-x86-64
[apps@dyh51 linux-x86-64]$
[apps@dyh51 linux-x86-64]$ ./sonar.sh start
Starting SonarQube...
Started SonarQube.
[apps@dyh51 linux-x86-64]$
7)查看是否启动
[apps@dyh51 logs]$ sudo netstat -nltp|grep 9000
tcp6 0 0 :::9000 :::* LISTEN 3135/java
[apps@dyh51 logs]$
8)页面访问
http://192.168.110.51:9000/
1)上传安装包
[apps@dyh51 svr]$ rz -bye
2)解压
[apps@dyh51 svr]$ unzip sonar-runner-dist-2.4.zip
3)修改配置文件
[apps@dyh51 svr]$ cd sonar-runner-2.4/conf/
[apps@dyh51 conf]$ vi sonar-runner.properties
sonar.host.url=http://192.168.110.51:9000
sonar.jdbc.url=jdbc:mysql://192.168.110.51:3306/sonar?useUnicode=true&characterEncoding=utf8
sonar.jdbc.username=apps
sonar.jdbc.password=123456
sonar.sourceEncoding=UTF-8
sonar.login=admin
sonar.password=admin
下面通过sonarqube检查一个python和一个java项目代码
4.1.1 创建python项目
1)创建测试文件
在window上客户端上创建一个目录python_project,作为python项目目录,在里面新建一个test.py文件,内容如下
def f1(x,y):
return x + y
def f2(x,y):
return x + y
def f3(x,y):
return x + y
a = f1(3,4)
b = f2(2,5)
print(a, b)
2)提交到svn
4.1.2创建jenkins项目
1)新建项目,选择自由风格的软件项目
2)配置项目,源码管理选择svn,输入地址及账号密码
URL: svn://192.168.110.51/project1/python_project/
3)构建内容填下面这段,点击保存
/apps/svr/sonar-runner-2.4/bin/sonar-runner -e -X \
-Dsonar.projectKey=python:project1 \
-Dsonar.projectName=py1 \
-Dsonar.sources=$WORKSPACE/src/ \
-Dsonar.language=py \
-Dsonar.sourceEncoding=UTF-8
4)点击构建
5)查看控制台输出
6)在sonarqube页面上查看代码检查结果
点进去可以看到问题详细信息
4.1.3 创建java项目
1)使用eclipse创建一个maven项目,项目结构如下,提交到SVN
2)在jenkins上新建一个maven任务
3)配置项目,源码选择SVN
svn://192.168.110.51/project1/abc /
5) 使用maven构建源码,并添加一段shell脚本,点击保存
/apps/svr/sonar-runner-2.4/bin/sonar-runner -e -X \
-Dsonar.projectKey=java:project1 \
-Dsonar.projectName=java1 \
-Dsonar.sources=$WORKSPACE/checkout/src/ \
-Dsonar.language=java \
-Dsonar.sourceEncoding=UTF-8 \
-Dsonar.java.binaries=$WORKSPACE/checkout/target/classes/
6)点击构建,查看输出日志
7)在sonarqube页面上查看检查结果
8)点进去看详细信息