首页 Jenkins教程代码质量测试工具SonarQube安装配置

代码质量测试工具:SonarQube

jenkins服务器部署扫描器:sonar-scanner

运维派隶属马哥教育旗下专业运维社区,是国内成立最早的IT运维技术社区,欢迎关注公众号:yunweipai
领取学习更多免费Linux云计算、Python、Docker、K8s教程关注公众号:马哥linux运维

基础环境依赖

数据库环境依赖
SonarQube 7.9的数据库要求

https://docs.sonarqube.org/7.9/requirements/requirements/

再支持MySQL,选择PostgreSQL

SonarQube 6.7的数据库要求

https://docs.sonarqube.org/6.7/Requirements.html

数据库要使用5.6版本,不支持5.5的版本

代码质量测试工具SonarQube安装配置插图

java环境依赖
SonarQube 7.9的环境要求

不再支持 java 8

代码质量测试工具SonarQube安装配置插图(1)

范例:安装 openjdk-11-jdk

[root@SonarQube-Server ~]#apt -y install openjdk-11-jdk
[root@SonarQube-Server ~]#java -version
openjdk version "11.0.6" 2020-01-14
OpenJDK Runtime Environment (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1)
OpenJDK 64-Bit Server VM (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1, mixed mode, sharing)
SonarQube 6.7的环境要求

代码质量测试工具SonarQube安装配置插图(2)

系统及内核参数

按官网说明修改配置

代码质量测试工具SonarQube安装配置插图(3)

必须修改内核限制,否则在启动时会报以下错误

代码质量测试工具SonarQube安装配置插图(4)

创建用户和修改内核配置

#使用普通账户启动sonarqube
[root@SonarQube-Server ~]#useradd -s /bin/bash -m sonarqube 
[root@SonarQube-Server ~]#vim /etc/sysctl.conf
vm.max_map_count=262144
fs.file-max=65536

[root@SonarQube-Server ~]# vim /etc/security/limits.conf
sonarqube  -  nofile  65536
sonarqube  -  nproc  4096
root       -  nofile  65536   
root       -  nproc  4096
硬件依赖

CPU/内存/磁盘

部署SonarQube

数据库准备
PostgreSQL 数据库
[root@SonarQube-Server ~]#apt -y install postgresql
......
syncing data to disk ... ok
Success. You can now start the database server using:
    /usr/lib/postgresql/10/bin/pg_ctl -D /var/lib/postgresql/10/main -l logfile start
Ver Cluster Port Status Owner    Data directory              Log file
10  main    5432 down   postgres /var/lib/postgresql/10/main /var/log/postgresql/postgresql-10-main.log
update-alternatives: using /usr/share/postgresql/10/man/man1/postmaster.1.gz to provide /usr/share/man/man1/postmaster.1.gz (postmaster.1.gz) in auto mode
Setting up postgresql (10+190ubuntu0.1) ...############################################################################...] 
Processing triggers for ureadahead (0.100.0-20) ...############################################################################...] 
Processing triggers for systemd (237-3ubuntu10.3) ...

[root@SonarQube-Server ~]#systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
   Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
   Active: active (exited) since Thu 2020-03-12 13:36:36 CST; 3min 30s ago
 Main PID: 21726 (code=exited, status=0/SUCCESS)
    Tasks: 0 (limit: 1084)
   CGroup: /system.slice/postgresql.service

Mar 12 13:36:36 SonarQube-Server systemd[1]: Starting PostgreSQL RDBMS...
Mar 12 13:36:36 SonarQube-Server systemd[1]: Started PostgreSQL RDBMS.

[root@SonarQube-Server ~]#id postgres
uid=112(postgres) gid=116(postgres) groups=116(postgres),115(ssl-cert)

#默认监听在127.0.0.1的5432端口,修改监听地址
[root@SonarQube-Server ~]#ss -ntlp|grep post
LISTEN   0         128               127.0.0.1:5432             0.0.0.0:*        users:(("postgres",pid=23337,fd=8))                                            
LISTEN   0         128                   [::1]:5432                [::]:*        users:(("postgres",pid=23337,fd=7)) 

[root@SonarQube-Server ~]#vim /etc/postgresql/10/main/postgresql.conf 
listen_addresses = '*' 或者 '0.0.0.0'
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
host    all             all             0.0.0.0/0               md5 

#开启远程访问
[root@SonarQube-Server ~]#vim /etc/postgresql/10/main/pg_hba.conf
[root@SonarQube-Server ~]#systemctl restart postgresql       
[root@SonarQube-Server ~]#ss -ntl|grep 5432
LISTEN   0         128                 0.0.0.0:5432             0.0.0.0:*       
LISTEN   0         128                    [::]:5432                [::]:* 

#使用postgres用户登录(PostgresSQL安装后会自动创建postgres用户,无密码)
[root@SonarQube-Server ~]#su - postgres
postgres@SonarQube-Server:~#登录postgresql数据库
postgres@SonarQube-Server:~ psql -U postgres
psql (10.12 (Ubuntu 10.12-0ubuntu0.18.04.1))
Type "help" for help.

#创建用户和数据库并授权
postgres=# CREATE DATABASE sonarqube;
CREATE DATABASE
postgres=# CREATE USER sonar WITH ENCRYPTED PASSWORD '123456';
CREATE ROLE
postgres=# GRANT ALL PRIVILEGES ON DATABASE sonarqube TO sonar;
GRANT
postgres=# ALTER DATABASE sonarqube OWNER TO sonar;
ALTER DATABASE
postgres=# ALTER USER postgres WITH ENCRYPTED PASSWORD '123456';
ALTER ROLE
postgres=# \q

#监听地址
root@sonarqube:~# /etc/postgresql/10/main/postgresql.conf 
59:listen_addresses = '*'

root@sonarqube:~# grep -n  "^[a-Z]" /etc/postgresql/10/main/pg_hba.conf
85:local   all             postgres                                peer
90:local   all             all                                     peer
92:host    all             all             0.0.0.0/0               md5
94:host    all             all             ::1/128                 md5
97:local   replication     all                                     peer
98:host    replication     all             127.0.0.1/32            md5
99:host    replication     all             ::1/128                 md5
MySQL 数据库
[root@SonarQube-Server ~]#apt-get install mysql-server mysql-client
[root@SonarQube-Server ~]#vim /etc/mysql/mysql.conf.d/mysqld.cnf #配置文件路径
[root@SonarQube-Server ~]#mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26-0ubuntu0.18.04.1 (Ubuntu)
Copyright (c) 2000, 2019, 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.
#创建数据库默认编码utf-8并授权
mysql> create database sonar default character set utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON sonar.* TO 'sonar'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

#测试sonar账户连接mysql
[root@client ~]# mysql -usonar -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.26-0ubuntu0.18.04.1 (Ubuntu)
Copyright (c) 2000, 2019, 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> show databases;
+--------------------+
| Database      |
+--------------------+
| information_schema |
| sonar       |
+--------------------+

2 rows in set (0.00 sec)
解压sonarqube并配置文件
[root@SonarQube-Server ~]#cd /usr/local/src
[root@SonarQube-Server src]#unzip sonarqube-7.9.2.zip
[root@SonarQube-Server src]#ln -s /usr/local/src/sonarqube-7.9.2 /usr/local/sonarqube
[root@SonarQube-Server ~]#chown -R sonarqube.sonarqube /usr/local/sonarqube/
[root@SonarQube-Server ~]#ls /usr/local/sonarqube
bin  conf  COPYING  data  elasticsearch  extensions  lib  logs  temp  web
[root@SonarQube-Server ~]#ls /usr/local/sonarqube/conf/
sonar.properties  wrapper.conf  

#修改配置
[root@SonarQube-Server ~]#vim /usr/local/sonarqube/conf/sonar.properties 
sonar.jdbc.username=sonar
sonar.jdbc.password=123456
sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube
sonar.web.host=0.0.0.0
sonar.web.port=9000
启动sonarqube

sonar需要调用elasticsearch,而且默认需要使用普通用户启动,如果以root启动会报错

[root@SonarQube-Server sonarqube]#bin/linux-x86-64/sonar.sh  start
Starting SonarQube...
Started SonarQube.
[root@SonarQube-Server sonarqube]#cat logs/sonar.log 
--> Wrapper Started as Daemon
Launching a JVM...
Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
  Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.

2020.03.12 16:33:37 INFO  app[][o.s.a.AppFileSystem] Cleaning or creating temp directory /usr/local/src/sonarqube-7.9.2/temp
2020.03.12 16:33:37 INFO  app[][o.s.a.es.EsSettings] Elasticsearch listening on /127.0.0.1:9001
2020.03.12 16:33:37 INFO  app[][o.s.a.ProcessLauncherImpl] Launch process[[key='es', ipcIndex=1, logFilenamePrefix=es]] from [/usr/local/src/sonarqube-7.9.2/elasticsearch]: /usr/local/src/sonarqube-7.9.2/elasticsearch/bin/elasticsearch
2020.03.12 16:33:37 INFO  app[][o.s.a.SchedulerImpl] Waiting for Elasticsearch to be up and running
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
2020.03.12 16:33:38 INFO  app[][o.e.p.PluginsService] no modules loaded
2020.03.12 16:33:38 INFO  app[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.transport.Netty4Plugin]
2020.03.12 16:33:53 WARN  app[][o.s.a.p.AbstractManagedProcess] Process exited with exit value [es]: 1
2020.03.12 16:33:53 INFO  app[][o.s.a.SchedulerImpl] Process[es] is stopped
2020.03.12 16:33:53 INFO  app[][o.s.a.SchedulerImpl] SonarQube is stopped
<-- Wrapper Stopped 
[root@SonarQube-Server sonarqube]#cat logs/es.log

代码质量测试工具SonarQube安装配置插图(5)

以sonarqube用户身份启动

[root@SonarQube-Server ~]#su - sonarqube
sonarqube@SonarQube-Server:~/usr/local/sonarqube/bin/linux-x86-64/sonar.sh  start
Starting SonarQube...
Started SonarQube.
sonarqube@SonarQube-Server:~ 

验证日志

[root@SonarQube-Server ~]#tail -n 3 /usr/local/sonarqube/logs/sonar.log
2020.03.12 17:02:18 INFO  app[][o.s.a.ProcessLauncherImpl] Launch process[[key='ce', ipcIndex=3, logFilenamePrefix=ce]] from [/usr/local/src/sonarqube-7.9.2]: /usr/lib/jvm/java-11-openjdk-amd64/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/usr/local/src/sonarqube-7.9.2/temp --add-opens=java.base/java.util=ALL-UNNAMED -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -Dhttp.nonProxyHosts=localhost|127.*|[::1] -cp ./lib/common/*:/usr/local/src/sonarqube-7.9.2/lib/jdbc/postgresql/postgresql-42.2.5.jar org.sonar.ce.app.CeServer /usr/local/src/sonarqube-7.9.2/temp/sq-process13657789573139744918properties
2020.03.12 17:02:39 INFO  app[][o.s.a.SchedulerImpl] Process[ce] is up
2020.03.12 17:02:39 INFO  app[][o.s.a.SchedulerImpl] SonarQube is up
登录到web界面

用浏览器访问地址:http://sonaqueb服务器IP:9000

代码质量测试工具SonarQube安装配置插图(6)

点击有左上角login登录,默认用户名和密码都是admin

代码质量测试工具SonarQube安装配置插图(7)
代码质量测试工具SonarQube安装配置插图(8)

安装中文支持
查看本地已安装插件

插件本地路径用于安装中文插件

[root@SonarQube-Server ~]#ll /usr/local/sonarqube/extensions/plugins/
total 87996
drwxr-xr-x 2 sonarqube sonarqube     4096 Dec  9 13:11 ./
drwxr-xr-x 5 sonarqube sonarqube     4096 Mar 12 17:01 ../
-rw-r--r-- 1 sonarqube sonarqube      224 Dec  9 13:11 README.txt
-rw-r--r-- 1 sonarqube sonarqube   287504 Dec  9 13:11 sonar-auth-github-plugin-1.5.0.870.jar
-rw-r--r-- 1 sonarqube sonarqube  3388540 Dec  9 13:11 sonar-auth-saml-plugin-1.1.0.181.jar
-rw-r--r-- 1 sonarqube sonarqube  4094975 Dec  9 13:11 sonar-csharp-plugin-7.15.0.8572.jar
-rw-r--r-- 1 sonarqube sonarqube  7015434 Dec  9 13:11 sonar-css-plugin-1.1.1.1010.jar
-rw-r--r-- 1 sonarqube sonarqube  1544789 Dec  9 13:11 sonar-flex-plugin-2.5.1.1831.jar
-rw-r--r-- 1 sonarqube sonarqube  3903342 Dec  9 13:11 sonar-go-plugin-1.1.1.2000.jar
-rw-r--r-- 1 sonarqube sonarqube  1727846 Dec  9 13:11 sonar-html-plugin-3.1.0.1615.jar
-rw-r--r-- 1 sonarqube sonarqube    15098 Dec  9 13:11 sonar-jacoco-plugin-1.0.2.475.jar
-rw-r--r-- 1 sonarqube sonarqube  8302745 Dec  9 13:11 sonar-java-plugin-5.13.1.18282.jar
-rw-r--r-- 1 sonarqube sonarqube  6866969 Dec  9 13:11 sonar-javascript-plugin-5.2.1.7778.jar
-rw-r--r-- 1 sonarqube sonarqube  7595999 Dec  9 13:11 sonar-kotlin-plugin-1.5.0.315.jar
-rw-r--r-- 1 sonarqube sonarqube   300503 Dec  9 13:11 sonar-ldap-plugin-2.2.0.608.jar
-rw-r--r-- 1 sonarqube sonarqube  5105268 Dec  9 13:11 sonar-php-plugin-3.2.0.4868.jar
-rw-r--r-- 1 sonarqube sonarqube  2752167 Dec  9 13:11 sonar-python-plugin-1.14.1.3143.jar
-rw-r--r-- 1 sonarqube sonarqube 10036210 Dec  9 13:11 sonar-ruby-plugin-1.5.0.315.jar
-rw-r--r-- 1 sonarqube sonarqube  9202024 Dec  9 13:11 sonar-scala-plugin-1.5.0.315.jar
-rw-r--r-- 1 sonarqube sonarqube  2622236 Dec  9 13:11 sonar-scm-git-plugin-1.8.0.1574.jar
-rw-r--r-- 1 sonarqube sonarqube  7229293 Dec  9 13:11 sonar-scm-svn-plugin-1.9.0.1295.jar
-rw-r--r-- 1 sonarqube sonarqube  2239156 Dec  9 13:11 sonar-typescript-plugin-1.9.0.3766.jar
-rw-r--r-- 1 sonarqube sonarqube  3580236 Dec  9 13:11 sonar-vbnet-plugin-7.15.0.8572.jar
-rw-r--r-- 1 sonarqube sonarqube  2242738 Dec  9 13:11 sonar-xml-plugin-2.0.1.2020.jar

sonarqube@s4:~$ ll /usr/local/sonarqube/extensions/plugins/

total 40476

drwxr-xr-x 2 sonarqube sonarqube 4096 Jul 22 18:06 ./

drwxr-xr-x 5 sonarqube sonarqube 4096 Jul 22 18:07 ../

-rw-r–r– 1 sonarqube sonarqube 92 Apr 16 15:39 README.txt

-rw-r–r– 1 sonarqube sonarqube 2703958 Apr 15 18:38 sonar-csharp-plugin-6.5.0.3766.jar

-rw-r–r– 1 sonarqube sonarqube 1618672 Apr 15 18:38 sonar-flex-plugin-2.3.jar

-rw-r–r– 1 sonarqube sonarqube 6759535 Apr 15 18:38 sonar-java-plugin-4.15.0.12310.jar

-rw-r–r– 1 sonarqube sonarqube 3355702 Apr 15 18:38 sonar-javascript-plugin-3.2.0.5506.jar

-rw-r–r– 1 sonarqube sonarqube 3022870 Apr 15 18:38 sonar-php-plugin-2.11.0.2485.jar

-rw-r–r– 1 sonarqube sonarqube 4024311 Apr 15 18:38 sonar-python-plugin-1.8.0.1496.jar

-rw-r–r– 1 sonarqube sonarqube 3625962 Apr 15 18:38 sonar-scm-git-plugin-1.3.0.869.jar

-rw-r–r– 1 sonarqube sonarqube 6680471 Apr 15 18:38 sonar-scm-svn-plugin-1.6.0.860.jar

-rw-r–r– 1 sonarqube sonarqube 2250667 Apr 15 18:38 sonar-typescript-plugin-1.1.0.1079.jar

-rw-r–r– 1 sonarqube sonarqube 7368250 Apr 15 18:38 sonar-xml-plugin-1.4.3.1027.jar

安装中文语言插件

administration- Marketplace,在后面的搜索框搜索插件chinese,然后点install安装:

代码质量测试工具SonarQube安装配置插图(9)
代码质量测试工具SonarQube安装配置插图(10)

或 在 插 件 目 录 /usr/local/sonarqube/extensions/plugins/ 执行以下命令:

cd  /usr/local/sonarqube/extensions/plugins/
wget https://github.com/SonarQubeCommunity/sonar-l10n-zh/releases/download/sonar-l10n-zh-plugin-1.29/sonar-l10n-zh-plugin-1.29.jar

代码质量测试工具SonarQube安装配置插图(11)

重启sonarquebe

Web界面安装完成插件后或者在插件目录下载插件后需要重启sonarquebe服务生效:或者在web界面重启:

代码质量测试工具SonarQube安装配置插图(12)
代码质量测试工具SonarQube安装配置插图(13)

或者通过命令重新启动

[root@SonarQube-Server ~]#su - sonarqube
sonarqube@SonarQube-Server:~$ /usr/local/sonarqube/bin/linux-x86-64/sonar.sh  restart
Gracefully stopping SonarQube...
SonarQube was not running.
Starting SonarQube...
Started SonarQube.

代码质量测试工具SonarQube安装配置插图(14)

登录后变成中文

代码质量测试工具SonarQube安装配置插图(15)

安装其他插件

Sonarquebe对代码的扫描都基于插件实现,因此要安装扫描的各种开发语言 的插件

默认已安装 Java、Python、Go,Php,javascript,Html 等语言对应的插件

代码质量测试工具SonarQube安装配置插图(16)
代码质量测试工具SonarQube安装配置插图(17)
代码质量测试工具SonarQube安装配置插图(18)

本文链接:http://www.yunweipai.com/35875.html

代码质量测试工具:SonarQube

jenkins服务器部署扫描器:sonar-scanner

网友评论comments

发表评论

邮箱地址不会被公开。

暂无评论

Copyright © 2012-2021 YUNWEIPAI.COM - 运维派 京ICP备16064699号-6
扫二维码
扫二维码
返回顶部