目录
1、安装SonarQube
2、实现代码审查
1、安装SonarQube
SonarQube简介:是一个用于管理代码质量的开放平台,可以快速的定位代码中潜在的或者明显的错误。目前支持java,C#,C/C++,Python,PL/SQL,Cobol,JavaScrip,Groovy等二十几种编程语言的代码质量管理与检测,底层使用elasticsearch作为代码检索工具。
官网:https://www.sonarqube.org/
环境需求:JDK1.8、MySQL5.7、SonarQube6.7.4
按要求安装MySQL,安装SonarQube,在MySQL创建sonar数据库
安装MySQL5.7
[root@jenkins ~]# yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake[root@jenkins ~]# useradd -s /sbin/nologin mysql[root@jenkins ~]# tar zxvf mysql-5.7.17.tar.gz -C /opt/[root@jenkins ~]# tar zxvf boost_1_59_0.tar.gz -C /usr/local/[root@jenkins ~]# cd /usr/local/[root@jenkins ~]# mv boost_1_59_0 boost[root@jenkins ~]# cd mysql-5.7.17/[root@jenkins mysql-5.7.17~]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DSYSConFDIR=/etc -DSYSTEMD_PID_DIR=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNObase_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/usr/local/mysql/data -DWITH_BOOST=/usr/local/boost -DWITH_SYSTEMD=1[root@jenkins mysql-5.7.17~]# make -j4 && make install[root@jenkins mysql-5.7.17~]# chown -R mysql.mysql /usr/local/mysql/
[root@jenkins mysql-5.7.17~]# vim /etc/my.cnf[client]port = 3306default-character-set=utf8socket = /usr/local/mysql/mysql.sock[mysql]port = 3306default-character-set=utf8socket = /usr/local/mysql/mysql.sock[mysqld]user = mysqlbasedir = /usr/local/mysqldatadir = /usr/local/mysql/dataport = 3306character_set_server=utf8pid-file = /usr/local/mysql/mysqld.pidsocket = /usr/local/mysql/mysql.sockserver-id = 1sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
[root@jenkins mysql-5.7.17~]# chown mysql:mysql /etc/my.cnf[root@jenkins mysql-5.7.17~]# vim /etc/profile...export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH #末行添加[root@jenkins mysql-5.7.17~]# source /etc/profile #刷新配置生效[root@jenkins mysql-5.7.17~]# cd /usr/local/mysql[root@jenkins mysql~]# bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data[root@jenkins mysql~]# cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/[root@jenkins mysql~]# systemctl daemon-reload[root@jenkins mysql~]# systemctl start mysqld[root@jenkins mysql~]# systemctl enable mysqld[root@jenkins mysql~]# mysqladmin -u root -p password "abc123" [root@jenkins mysql~]# mysql -u root -p[root@jenkins mysql~]# grant all privileges on *.* to 'root'@'%' identified by 'abc123' with grant option;
数据库安装完成后,在MySQL创建sonar数据库
安装SonarQube
[root@jenkins ~]# unzip sonarqube-6.7.4.zip #解压[root@jenkins ~]# mkdir /opt/sonar #创建目录[root@jenkins ~]# mv sonarqube-6.7.4testtargettesttarget/**sonar.java.source=1.8sonar.java.target=1.8# Encoding of the source code、Default is default system encodingsonar.sourceEncoding=UTF-8
修改Jenkinsfile,加入SonarQube代码审查阶段
stage('code checking') { steps { script { //引入了sonarqube-scanner工具 scannerHome = tool 'sonar-scanner' } //引入了sonarqube服务器系统环境 withSonarQubeEnv('sonarqube') { sh "${scannerHome}/bin/sonar-scanner" } } }
把更改后的sonar-project.properties和Jenkinsfile进行提交
开始构建web_demo_pipeline