Centos7搭建SonarQube

Centos7搭建代码质量管理平台SonarQube

SonarQube是当前比较热门的代码质量管理平台,平台开源,支持多种语言。

搭建之前

首先,要确保当前安装服务器已经安装了jdk(最好1.8+),以及MySQL数据库
安装jdk:

1
yum install java-1.8.0-openjdk* -y

安装MySQL

1
2
3
4
5
6
7
8
9
10
11
下载源包:
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

安装mysql源:
yum localinstall mysql57-community-release-el7-8.noarch.rpm

检查是否安装成功:
yum repolist enabled | grep "mysql.*-community.*"

安装
yum install mysql-community-server

下载安装包

sonarqube下载地址
sonar-runner下载地址
sonar-scanner下载扫描器地址

搭建SonarQube

首先:SonarQube是服务器端,它主要有两个功能:1.分析源代码;2.因为它内嵌了Apache模块,所以提供Web端的界面访问。
SonarQube Runner是一个利用SonarQube服务端分析代码的命令行工具,可以把它简单理解为客户端。
所以,为了安装和调试方便,建议SonarQube和SonarQube Runner都下载。

创建sonar数据库

mysql -uroot -p进入控制台,创建sonar用户及数据库:

1
2
3
4
5
6
7
8
9
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE USER 'sonar' IDENTIFIED BY 'Sonar_1234';

GRANT ALL PRIVILEGES ON `sonar`.* TO 'sonar'@'%' IDENTIFIED BY 'Sonar_1234';

GRANT ALL PRIVILEGES ON `sonar`.* TO 'sonar'@'localhost' IDENTIFIED BY 'Sonar_1234';

FLUSH PRIVILEGES;

安装SonarQube

进入安装目录,这边用的是/usr/localrz(yum install lrzsz)将下载的sonarqube-6.7.5.zip,sonar-runner-dist-2.4.zip,sonar-scanner-cli-3.0.3.778-linux.zip上传到服务器,解压sonarqube-6.7.5.zip

1
unzip sonarqube-6.7.5.zip

顺便其他两个也解压了:

1
2
3
unzip sonar-runner-dist-2.4.zip

unzip sonar-scanner-cli-3.0.3.778-linux.zip

配置环境变量

vim /etc/profile,到文件底部,添加环境变量:

1
2
3
4
export SONAR_HOME=/usr/local/sonarqube-6.7.5
export SONAR_RUNNER_HOME=/usr/local/sonar-runner-2.4
PATH=$PATH:$SONAR_HOME/bin:$SONAR_RUNNER_HOME/bin
export PATH

生效配置:source /etc/profile

验证:

1
sonar-runner -v
1
2
3
4
[root@localhost workflow]# sonar-runner -v
SonarQube Runner 2.4
Java 1.8.0_181 Oracle Corporation (64-bit)
Linux 3.10.0-862.9.1.el7.x86_64 amd64

修改配置文件

配置SonarQube

进入配置文件mulu:cd /usr/local/sonarqube-6.7.5/conf
修改配置文件:vim sonar.properties,修改内容如下:

1
2
3
4
5
6
7
8
9
sonar.jdbc.username=sonar				(第16行)
sonar.jdbc.password=Sonar_1234 (第17行)

sonar.jdbc.url=jdbc:mysql://127.0.0.1:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false (第26行)


sonar.web.host=192.168.0.91 (第105行)

sonar.web.port=9000 (第111行)
配置 sonar-runner

进入目录cd /usr/local/sonar-runner-2.4/conf,修改配置文件:vim sonar-runner.properties
修改内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#Configure here general information about the environment, such as SonarQube DB details for example
2 #No information about specific project should appear here
3
4 #----- Default SonarQube server
5 sonar.host.url=http://192.168.0.91:9000
6
7 #----- PostgreSQL
8 #sonar.jdbc.url=jdbc:postgresql://localhost/sonar
9
10 #----- MySQL
11 sonar.jdbc.url=jdbc:mysql://127.0.0.1:3306/sonar?useUnicode=true&characterEncoding=utf8
12
13 #----- Oracle
14 #sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE
15
16 #----- Microsoft SQLServer
17 #sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor
18
19 #----- Global database settings
20 sonar.jdbc.username=sonar
21 sonar.jdbc.password=Sonar_1234
22
23 #----- Default source code encoding
24 #sonar.sourceEncoding=UTF-8
25
26 #----- Security (when 'sonar.forceAuthentication' is set to 'true')
27 sonar.login=admin
28 sonar.password=admin

启动sonarqube

sonarqube需要普通用户才能启动,不能使用root用户,这个比较好办,新建用户和组,更改属组就可以:

1
2
useradd -g elasticsearch elasticsearch	(前一个elasticsearch是组,后一个是用户)
chown -R elasticsearch:elasticsearch sonarqube-6.7.5
修改elasticsearch配置

cd /usr/local/sonarqube-6.7.5/elasticsearch/config,修改配置为:

1
2
3
4
5
6
#
network.host: 192.168.0.91
#
# Set a custom port for HTTP:
#
http.port: 9200
启动

cd /usr/local/sonarqube-6.7.5/bin/linux-x86-64
./sonar.sh start
启动完成~

项目部署

在没有用到jenkins以及git情况下,只有手动将代码上传到服务器进行打分,后期再弄CI,这边介绍的也就是最简单的上传代码的形式。
首先,有一个项目需要sonar进行分析,将代码上传到sonar服务器自定义存放代码的位置,例如/root/sourceCode,上传的项目为QATest,然后在项目根目录创建sonar-runner的配置文件sonar-project.properties

1
vim sonar-project.properties

内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# must be unique in a given SonarQube instance
sonar.projectKey=my:project
# this is the name displayed in the SonarQube UI
sonar.projectName=My project
sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set.
# If not set, SonarQube starts looking for source code from the directory containing
# the sonar-project.properties file.
sonar.sources=.

# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

保存退出。

启动sonar-runner:sonar-runner -e -X(打印详细日志)

进入SonarQube查看分析结果:
http://192.168.0.91:9000/projects(账密:admin/admin)

文章目录
  1. 搭建之前
    1. 下载安装包
  2. 搭建SonarQube
    1. 创建sonar数据库
    2. 安装SonarQube
    3. 配置环境变量
    4. 修改配置文件
      1. 配置SonarQube
      2. 配置 sonar-runner
    5. 启动sonarqube
      1. 修改elasticsearch配置
      2. 启动
  3. 项目部署
|