sonarqube部署

代码质量检测SonarQube

  1. SonarQube基于java开发,需安装open JDK8版本
  2. SonarQube需要依赖MySQL数据库,至少5.6版本以上
  3. SonarQube的小型实例至少4G内存,如果大型实例需要16G内存
服务器准备

10.0.0.103 Sonar 1核2G 20G硬盘(实验环境)
安装软件
yum ‐y install git java unzip wget

安装数据库
下载mysqlyum源
https://dev.mysql.com/downloads/repo/yum/

图片.png
图片.png

将下载好的rpm包传送到sonar服务器中并安装

rpm -ivh mysql80-community-release-el7-3.noarch.rpm

更改默认安装版本为5.6 enabled=1

vim /etc/yum.repos.d/mysql-community.repo
图片.png
下一步安装MySQL5.6
[root@sona ~]yum install mysql-community-server -y
Redirecting to /bin/systemctl start mysql.service #mysql端口为3306
[root@sona ~]netstat -tnulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1147/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1346/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      2081/mysqld         
tcp6       0      0 :::22                   :::*                    LISTEN      1147/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1346/master
配置数据库密码
mysqladmin -uroot password 123
创建sonar库
[root@sona ~]mysql -uroot -p123 -e "CREATE DATABASE sonar DEFAULT CHARACTER SET utf8;"
Warning: Using a password on the command line interface can be insecure.
[root@sona ~]mysql -uroot -p123 -e "show databases;"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sonar              |
+--------------------+
SonarQube服务安装

下载sonar 并使用sonar用户启动(sonarqube需用铺哦哦那个用户启动,使用管理员用户是无法启动的)
https://www.sonarqube.org/downloads/

[root@sona ~]unzip sonarqube-7.0.zip  -d /usr/local/
[root@sona ~]cd /usr/local/
[root@sona local]ll
总用量 0
drwxr-xr-x.  2 root root   6 4月  11 2018 bin
drwxr-xr-x.  2 root root   6 4月  11 2018 etc
drwxr-xr-x.  2 root root   6 4月  11 2018 games
drwxr-xr-x.  2 root root   6 4月  11 2018 include
drwxr-xr-x.  2 root root   6 4月  11 2018 lib
drwxr-xr-x.  2 root root   6 4月  11 2018 lib64
drwxr-xr-x.  2 root root   6 4月  11 2018 libexec
drwxr-xr-x.  2 root root   6 4月  11 2018 sbin
drwxr-xr-x.  5 root root  49 11月 15 12:31 share
drwxr-xr-x  11 root root 141 2月   2 2018 sonarqube-7.0
drwxr-xr-x.  2 root root   6 4月  11 2018 src
[root@sona local]mv sonarqube-7.0/ sonarqube
[root@sona local]useradd sonar
[root@sona local]chown -R sonar.sonar sonarqube/
[root@sona local]ll sonarqube/
总用量 12
drwxr-xr-x 8 sonar sonar  136 2月   2 2018 bin
drwxr-xr-x 2 sonar sonar   50 2月   2 2018 conf
-rw-r--r-- 1 sonar sonar 7651 2月   2 2018 COPYING
drwxr-xr-x 2 sonar sonar   24 2月   2 2018 data
drwxr-xr-x 7 sonar sonar  150 2月   2 2018 elasticsearch
drwxr-xr-x 4 sonar sonar   40 2月   2 2018 extensions
drwxr-xr-x 9 sonar sonar  138 2月   2 2018 lib
drwxr-xr-x 2 sonar sonar    6 2月   2 2018 logs
drwxr-xr-x 2 sonar sonar   24 2月   2 2018 temp
drwxr-xr-x 9 sonar sonar 4096 2月   2 2018 web
配置sonar连接本地数据库
vim /usr/local/sonarqube/conf/sonar.properties
图片.png
使用普通用户启动sonarqube
[root@sona local]su - sonar -c "/usr/local/sonarqube/bin/linux-x86-64/sonar.sh start"
Starting SonarQube...
Failed to start SonarQube.

此时显示无法启动,需要安装jdk

[root@sona ~]rpm -ivh jdk-8u181-linux-x64.rpm
[root@sona ~]su - sonar -c "/usr/local/sonarqube/bin/linux-x86-64/sonar.sh start"
Starting SonarQube...
Started SonarQube.

启动后的端口为9000使用浏览器直接访问http://10.0.0.103:9000/用户名admin 密码admin
生成一个tokin,只显示一次记录好 jenkins: 4a922e4a7c5515274d5ee79bdece8287d8ed5acc


图片.png
图片.png
图片.png
配置管理sonar 安装插件

1.安装中文汉化插件
在页面上找到administrator > Marketplace > 搜索框 chinese,出现一个 chinese pack 然后点击install,安装完点击
restart 重启生效
2.默认已安装C JAVA Python PHP JS 等代码质量分析工具,如果一个项目使用了JAVA CSS JS HTML,默认情况下sonar
只会检测JAVA,JS等代码的漏洞和bug,如果安装了HTML、CSS等插件,则会检测该项目代码中JAVA JS HTML CSS代码的漏洞和bug

手动安装插件

[root@sona ~]cd /usr/local/sonarqube/extensions/
[root@sona extensions]mv plugins/ pluginsbak
[root@sona extensions]ll
总用量 4
drwxr-xr-x 2 sonar sonar    6 11月 30 15:56 downloads
drwxr-xr-x 3 sonar sonar   20 2月   2 2018 jdbc-driver
drwxr-xr-x 2 sonar sonar 4096 11月 30 15:55 pluginsbak
[root@sona extensions]rz -E
rz waiting to receive.
[root@sona extensions]ll
总用量 44056
drwxr-xr-x 2 sonar sonar        6 11月 30 15:56 downloads
drwxr-xr-x 3 sonar sonar       20 2月   2 2018 jdbc-driver
drwxr-xr-x 2 sonar sonar     4096 11月 30 15:55 pluginsbak
-rw-r--r-- 1 root  root  45106788 11月 17 20:49 sonar_plugins.tar.gz
[root@sona extensions]tar xf sonar_plugins.tar.gz 
[root@sona extensions]ll
总用量 44060
drwxr-xr-x 2 sonar sonar        6 11月 30 15:56 downloads
drwxr-xr-x 3 sonar sonar       20 2月   2 2018 jdbc-driver
drwxr-xr-x 2 sonar sonar     4096 10月 24 2019 plugins
drwxr-xr-x 2 sonar sonar     4096 11月 30 15:55 pluginsbak
-rw-r--r-- 1 root  root  45106788 11月 17 20:49 sonar_plugins.tar.gz
[root@sona extensions]ll plugins/
总用量 46668
-rw-r--r-- 1 sonar sonar      92 2月   2 2018 README.txt
-rw-r--r-- 1 sonar sonar 1460815 1月  29 2018 sonar-csharp-plugin-6.7.1.4347.jar
-rw-r--r-- 1 sonar sonar 7015434 10月 24 2019 sonar-css-plugin-1.1.1.1010.jar
-rw-r--r-- 1 sonar sonar 1618672 1月  29 2018 sonar-flex-plugin-2.3.jar
-rw-r--r-- 1 sonar sonar 1726692 10月 24 2019 sonar-html-plugin-3.0.1.1444.jar
-rw-r--r-- 1 sonar sonar 8901184 10月 24 2019 sonar-java-plugin-5.6.0.15032.jar
-rw-r--r-- 1 sonar sonar 3373769 1月  29 2018 sonar-javascript-plugin-4.0.0.5862.jar
-rw-r--r-- 1 sonar sonar   39062 10月 24 2019 sonar-l10n-zh-plugin-1.20.jar
-rw-r--r-- 1 sonar sonar 2774137 2月   2 2018 sonar-php-plugin-2.12.1.3018.jar
-rw-r--r-- 1 sonar sonar 1509434 2月   2 2018 sonar-python-plugin-1.9.0.2010.jar
-rw-r--r-- 1 sonar sonar 3625962 1月  29 2018 sonar-scm-git-plugin-1.3.0.869.jar
-rw-r--r-- 1 sonar sonar 6680471 1月  29 2018 sonar-scm-svn-plugin-1.6.0.860.jar
-rw-r--r-- 1 sonar sonar 1663416 2月   2 2018 sonar-typescript-plugin-1.5.0.2122.jar
-rw-r--r-- 1 sonar sonar 7368250 1月  29 2018 sonar-xml-plugin-1.4.3.1027.jar

重启sonarqube

[root@sona extensions]su - sonar -c "/usr/local/sonarqube/bin/linux-x86-64/sonar.sh restart"
Stopping SonarQube...
SonarQube was not running.
Starting SonarQube...
Started SonarQube.
jenkins服务器测试推送html代码至SonarQube检测 下载客户端

客户端其他版本下载地址搜索对应版本 例如:SonarScanner 4.0
https://docs.sonarqube.org/7.9/analysis/scan/sonarscanner/
在jenkins中安装客户端SonarScanner

[root@Jenkins ~]rz -E
rz waiting to receive.
[root@Jenkins ~]ll
总用量 41404
-rw-------. 1 root root     1579 11月 15 15:22 anaconda-ks.cfg
drwxr-xr-x  3 root root       62 11月 28 18:19 git_data
-rw-r--r--  1 root root 42392358 11月 17 21:11 sonar-scanner-cli-4.0.0.1744-linux.zip
[root@Jenkins ~]unzip sonar-scanner-cli-4.0.0.1744-linux.zip -d /usr/local/
[root@Jenkins ~]cd /usr/local/
[root@Jenkins local]ll
总用量 0
drwxr-xr-x. 2 root root  6 4月  11 2018 bin
drwxr-xr-x. 2 root root  6 4月  11 2018 etc
drwxr-xr-x. 2 root root  6 4月  11 2018 games
drwxr-xr-x. 2 root root  6 4月  11 2018 include
drwxr-xr-x. 2 root root  6 4月  11 2018 lib
drwxr-xr-x. 2 root root  6 4月  11 2018 lib64
drwxr-xr-x. 2 root root  6 4月  11 2018 libexec
drwxr-xr-x. 2 root root  6 4月  11 2018 sbin
drwxr-xr-x. 5 root root 49 11月 15 15:12 share
drwxr-xr-x  6 root root 51 6月  25 2019 sonar-scanner-4.0.0.1744-linux
drwxr-xr-x. 2 root root  6 4月  11 2018 src
[root@Jenkins local]mv sonar-scanner-4.0.0.1744-linux/ sonar
[root@Jenkins local]ll
总用量 0
drwxr-xr-x. 2 root root  6 4月  11 2018 bin
drwxr-xr-x. 2 root root  6 4月  11 2018 etc
drwxr-xr-x. 2 root root  6 4月  11 2018 games
drwxr-xr-x. 2 root root  6 4月  11 2018 include
drwxr-xr-x. 2 root root  6 4月  11 2018 lib
drwxr-xr-x. 2 root root  6 4月  11 2018 lib64
drwxr-xr-x. 2 root root  6 4月  11 2018 libexec
drwxr-xr-x. 2 root root  6 4月  11 2018 sbin
drwxr-xr-x. 5 root root 49 11月 15 15:12 share
drwxr-xr-x  6 root root 51 6月  25 2019 sonar
drwxr-xr-x. 2 root root  6 4月  11 2018 src

sonar-scanner命令路径如下,使用命令需要加路径/usr/local/sonar/bin/

[root@Jenkins local]ll sonar/bin/
总用量 8
-rwxr-xr-x 1 root root 1771 6月  25 2019 sonar-scanner
-rwxr-xr-x 1 root root  610 6月  25 2019 sonar-scanner-debug
[root@Jenkins local]/usr/local/sonar/bin/sonar-scanner

将sonar-scanner 命令路径写入PATH路径,则可直接使用命令

[root@Jenkins local]echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@Jenkins local]vim /etc/profile

export PATH="echo $PATH:/usr/local/sonar/bin/"

[root@Jenkins local]source /etc/profile

此时在jenkins服务器中将代码推送到sonarqube中


图片.png
[root@Jenkins local]cd /var/lib/jenkins/workspace/freestyle_job/
[root@Jenkins freestyle_job]sonar-scanner \
   -Dsonar.projectKey=html \
   -Dsonar.sources=. \
   -Dsonar.host.url=http://10.0.0.103:9000 \
   -Dsonar.login=4a922e4a7c5515274d5ee79bdece8287d8ed5acc

刷新sonarqube页面即可看到监测结果


图片.png
将sonar集成到jenkins中需要安装插件,前面安装包已下载
图片.png

1.jenkins上配置SonarQube服务端
系统管理->系统设置->sonarQube 告诉jenkins SonarQubeServer服务端地址
①Name随意填写
②URL添加SonarQube的地址

③token添加一个secret text 填写之前安装SonarQube的Token
图片.png
图片.png

2.jenkins配置执行sonar-scanner命令的家目录,让jenkins能找到该命令 系统管理->全局工具配置
图片.png

3.配置html项目中增加构建项sonar 上下对换位置,先测试代码后发布到web
图片.png
图片.png
Analysis properties项填写完保存执行,最后在SonarQube中查看结果

sonar.projectName=${JOB_NAME} # 项目在sonarqube上的显示名称
sonar.projectKey=html # 项目的唯一表示,不能重复

sonar.sources=. # 扫描那个项目的源码
图片.png
图片.png

你可能感兴趣的:(sonarqube部署)