共计 9084 个字符,预计需要花费 23 分钟才能阅读完成。
1.SonarQube 部署前的内核参数等配置以及 Java 环境配置
1)修改内核参数配置,使满足环境要求
[ | ]|
vm.max_map_count=262144 | |
fs.file-max=65536 | |
[ | ]|
…… | |
vm.max_map_count = 262144 | |
fs.file-max = 65536 |
2)修改本机安全策略参数限制
[ | ]|
…… | |
sonarqube - nofile 65536 | |
sonarqube - nproc 2048 |
3)配置 java 环境(至少 jdk- 8 或 openjdk- 8 版本)
[root@sonarqube ~]# ls /usr/local/src/ | |
jdk-8u211-linux-x64.tar.gz | |
[root@sonarqube src]# tar xvf jdk-8u211-linux-x64.tar.gz | |
[root@sonarqube src]#ln -sv jdk1.8.0_211 jdk | |
[root@sonarqube src]#ln -sv jdk | |
[root@sonarqube src]# ln -sv jdk/bin/* /usr/bin | |
[root@sonarqube src]# vim /etc/profile | |
…… | |
export export LANG="en_US.utf-8" | |
export JAVA_HOME=/usr/local/jdk | |
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH | |
export CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar |
2. 数据库准备(sonarqube7.9 版本不再支持 MySQL,这里以 sonarqube6.7 为例,并要求至少 mysql5.6)
1)提前定义好数据的相关路径以及数据库启动用户
[ | ]|
[ | ]|
[ | ]|
[ | ]|
[ | ]
2)下载 msyql 源码压缩文件
[ | ]|
mariadb-10.2.23-linux-x86_64.tar.gz |
3)解压并创建软连接
[ | ]|
[ | ]
4)创建可执行二进制文件的软连接
[root@sonarqube ~]# ln -sv /apps/mysql/mysql/bin/* /usr/sbin/
5)修改 mysql 启动脚本文件
[root@sonarqube ~]# vim /apps/mysql/mysql/support-files/mysql.server | |
…… | |
basedir=/apps/mysql/mysql #定义 mysql 的安装运行目录 | |
datadir=/data/mysql #定义 mysql 数据库文件保存目录 | |
…… |
6)建议 修改主机域名解析,将修改的主机名解析到本地本地回环
[ | ]|
127.0.0.1 sonarqube localhost localhost.localdomain localhost4 localhost4.localdomain4 | |
…… |
7) 执行数据库的初始化
[ | ]|
/apps/mysql/mysql/scripts | |
[ | ]|
mysql_install_db | |
[ | ]
8)启动 mysql 服务
[ | ]|
/apps/mysql/mysql/support-files | |
[ | ]
9)查看 3306 端口是否启动
[root@sonarqube ~]# ss -tnlp | |
State Recv-Q Send-Q Local Address:Port Peer Address:Port | |
…… | |
LISTEN 0 80 :::3306 :::* | |
users:(("mysqld",pid=9865,fd=18)) | |
…… |
10)创建 sonarqube 数据库并授权(字符集 utf8)
MariaDB [(none)]> create database sonarqube default character set utf8 collate utf8_general_ci; | |
MariaDB [(none)]> grant all on sonarqube.* to sonarqube@'192.168.1.31' identified by '123456'; |
11)测试 sonarqube 用户是否可以登录数据库
[root@sonarqube ~]# mysql -usonarqube -p123456 -h192.168.1.31 | |
Welcome to the MariaDB monitor. Commands end with ; or \g. | |
Your MariaDB connection id is 12 | |
Server version: 10.2.23-MariaDB-log MariaDB Server | |
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. | |
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. | |
MariaDB [(none)]> | |
MariaDB [(none)]> show databases; | |
+--------------------+ | |
| Database | | |
+--------------------+ | |
| information_schema | | |
| sonarqube | | |
| test | | |
+--------------------+ | |
3 rows in set (0.00 sec) |
3.sonarqube-6.7 的安装配置
1)下载源码压缩包
[ | ]|
sonarqube-6.7.7.zip |
2)解压 sonarqube 源码包, 并创建软连接
[ | ]|
[ | ]
3)创建用户以及更改目录的属主属组
[ | ]|
[ | ]
4)配置 sonarqube 配置文件
[root@sonarqube conf]# su - sonarqube | |
Last login: Mon Jul 29 13:48:33 CST 2019 on pts/1 | |
[sonarqube@sonarqube ~]$ cd /apps/sonarqube/sonarqube | |
[sonarqube@sonarqube sonarqube]$ grep '^[a-Z]' conf/sonar.properties | |
sonar.jdbc.username=sonarqube | |
sonar.jdbc.password=123456 | |
sonar.jdbc.url=jdbc:mysql://192.168.1.31:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false | |
sonar.web.host=0.0.0.0 | |
sonar.web.port=9000 |
5)启动 sonarqube 服务
[sonarqube@sonarqube sonarqube]$ ./bin/linux-x86-64/sonar.sh start | |
Starting SonarQube... | |
Started SonarQube. |
6)查看监听端口
[sonarqube@sonarqube sonarqube]$ ss -tnlp | |
State Recv-Q Send-Q Local Address:Port Peer Address:Port | |
LISTEN 0 128 *:111 *:* | |
LISTEN 0 128 *:22 *:* | |
LISTEN 0 100 127.0.0.1:25 *:* | |
LISTEN 0 1 127.0.0.1:32000 *:* | |
users:(("java",pid=10447,fd=4)) | |
LISTEN 0 25 :::9000 :::* | |
users:(("java",pid=10544,fd=116)) | |
LISTEN 0 128 ::ffff:127.0.0.1:9001 :::* | |
users:(("java",pid=10467,fd=100)) | |
LISTEN 0 80 :::3306 :::* | |
LISTEN 0 128 :::111 :::* | |
LISTEN 0 128 :::22 :::* | |
LISTEN 0 100 ::1:25 :::* | |
LISTEN 0 50 ::ffff:127.0.0.1:9092 :::* | |
users:(("java",pid=10544,fd=148)) |
7)浏览器访问 sonarqube
8)登录的用户名及密码均默认为 admin
9)成功登录 sonarqube 的 web 页面
4. 安装中文插件,显示为中文界面
1)点击 administration→Marketplace,直接搜索中文插件
2)在点击安装 install
3)重新启动服务
[sonarqube@sonarqube linux-x86-64]$ ./sonar.sh restart
4)再次访问时,显示中文界面
5)查看安装的中文包插件
[sonarqube@sonarqube sonarqube]$ ll extensions/plugins/ | |
…… | |
-rw-r--r-- 1 sonarqube sonarqube 38307 Jul 29 15:05 sonar-l10n-zh-plugin-1.19.jar | |
…… |
6)安装 Python、java、php 等开发语言插件,才能扫描相关语言代码
5. 部署扫描器 sonar-scanner(sonarqube 通过扫描器扫描代码)
1)scanner 二进制×××地址
https://binaries.sonarsource.com/Distribution/sonar-scanner-cli
2)下载扫描器到指定目录下
[ | ]|
/apps/sonarscanner | |
[ | ]|
total 41M | |
-rw-r--r-- 1 root root 41M Jul 22 19:31 sonar-scanner-cli-4.0.0.1744-linux.zip |
3)解压扫描器二进制源码并创建软连接
[ | ]|
[ | ]|
sonar-scanner-4.0.0.1744-linux sonar-scanner-cli-4.0.0.1744-linux.zip | |
[ | ]
4)修改配置文件
[root@sonarqube sonar-scanner]# vim conf/sonar-scanner.properties | |
…… | |
#----- Default SonarQube server | |
sonar.host.url=http://192.168.1.31:9000 | |
#----- Default source code encoding | |
sonar.sourceEncoding=UTF-8 |
5)准备一个测试代码压缩包并解压到当前目录下
[ | ]|
/apps/code-test | |
[ | ]|
total 6.5M | |
-rw-r--r-- 1 root root 6.5M Nov 21 2018 sonar-examples-master.zip | |
[ | ]
6)切换到测试源码的路径下
[root@sonarqube php-sonar-runner]# pwd | |
/apps/code-test/sonar-examples-master/projects/languages/php/php-sonar-runner | |
[root@sonarqube php-sonar-runner]# ll | |
total 12 | |
-rw-r--r-- 1 root root 453 Jul 25 2016 README.md | |
-rw-r--r-- 1 root root 331 Jul 25 2016 sonar-project.properties | |
drwxr-xr-x 2 root root 22 Jul 25 2016 src | |
-rw-r--r-- 1 root root 272 Jul 25 2016 validation.txt |
7)默认生成的代码配置文件
[root@sonarqube php-sonar-runner]# cat sonar-project.properties | |
# Required metadata | |
sonar.projectKey=org.sonarqube:php-simple-sq-scanner #自定义额项目 key | |
sonar.projectName=PHP :: Simple Project :: SonarQube Scanner #项目的名称 | |
sonar.projectVersion=1.0 #项目的版本号 | |
# Comma-separated paths to directories with sources (required) | |
sonar.sources=src #源码路径 | |
# Language | |
sonar.language=php #源码的类型为 PHP 代码 | |
# Encoding of the source files | |
sonar.sourceEncoding=UTF-8 #编码的格式 |
8)在当前的代码路径下执行扫描
[root@sonarqube php-sonar-runner]# /apps/sonarscanner/sonar-scanner/bin/sonar-scanner | |
…… | |
tly specify it. | |
INFO: Calculating CPD for 1 file | |
INFO: CPD calculation finished | |
INFO: Analysis report generated in 213ms, dir size=34 KB | |
INFO: Analysis reports compressed in 42ms, zip size=10 KB | |
INFO: Analysis report uploaded in 3108ms | |
INFO: ANALYSIS SUCCESSFUL, you can browse http://192.168.1.31:9000/dashboard/index/org.sonarqube:php-simple-sq-scanner | |
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report | |
INFO: More about the report processing at http://192.168.1.31:9000/api/ce/task?id=AWw9AEUVHii-ILef0qYq | |
INFO: Task total time: 17.226 s | |
INFO: ------------------------------------------------------------------------ | |
INFO: EXECUTION SUCCESS | |
INFO: ------------------------------------------------------------------------ | |
INFO: Total time: 29.467s | |
INFO: Final Memory: 7M/115M | |
INFO: ------------------------------------------------------------------------ |
9)web 端查看扫描后生成的记录
6.jenkins 配置 sonarqube-scanner 并实现 jenkins 代码的自动测试、自动部署
1)安装扫描器插件
2)点击系统设置设置 sonarqube 服务
3)添加 sonarqube 服务并自定义服务名称以及 url 地址
4)自动安装 scanner 扫描器
5)若已安装 scanner 扫描器则无需自动安装,直接添加扫描器的工作目录即可
6)在 jenkins 创建一个新项目 code-test-job
7)配置此项目的 configure
指定 gitlab 仓库源、认证方式
构建触发的口令秘钥
代码扫描的配置参数,其指定检测代码的类型、编码格式、路径等
8)在 gitlab 服务器中对的系统钩子添加 jenkins 认证地址,以实现自动触发 jenkins 的项目构建
9)克隆 gitlab 仓库代码并修改代码,再上传至 gitlab 仓库触发 jenkins 项目构建
指定克隆 develop 分支代码
root@Ubuntu1804:/data/src# git clone -b develop http://192.168.1.30/jie/web-page.git | |
Cloning into 'web-page'... | |
Username for 'http://192.168.1.30': jie | |
Password for 'http://jie@192.168.1.30': | |
remote: Enumerating objects: 30, done. | |
remote: Counting objects: 100% (30/30), done. | |
remote: Compressing objects: 100% (13/13), done. | |
remote: Total 30 (delta 4), reused 27 (delta 4) | |
Unpacking objects: 100% (30/30), done. |
添加一个 php 源码文件到克隆的目录下
root@ubuntu1804:/data/src/web-page# mv /data/Math.php ./ | |
root@ubuntu1804:/data/src/web-page# ls | |
index.html Math.php |
代码添加至本地暂存区
root@ubuntu1804:/data/src/web-page# git add ./*
代码提交至工作区并指定提交版本号以便多次提交的区分
root@ubuntu1804:/data/src/web-page# git commit -m 'v11' | |
[develop 9e106d4] v11 | |
1 file changed, 214 insertions(+) | |
create mode 100644 Math.php |
代码上传至 gitlab 代码库
root@ubuntu1804:/data/src/web-page# git push | |
Username for 'http://192.168.1.30': jie | |
Password for 'http://jie@192.168.1.30': | |
Counting objects: 3, done. | |
Delta compression using up to 4 threads. | |
Compressing objects: 100% (3/3), done. | |
Writing objects: 100% (3/3), 1.75 KiB | 893.00 KiB/s, done. | |
Total 3 (delta 0), reused 0 (delta 0) | |
remote: | |
remote: To create a merge request for develop, visit: | |
remote: http://192.168.1.30/jie/web-page/merge_requests/new?merge_request%5Bsource_branch%5D=develop | |
remote: | |
To http://192.168.1.30/jie/web-page.git | |
3fb434f..9e106d4 develop -> develop |
10)在 gitlab 服务端查看代码已提交成功
11)提交代码到 gitlab 后自动触发 jenkins 项目构建
12)查看项目构建成功的控制台信息输出
13)查看 sonarqube-server 服务器记录下来的 php 代码测试结果
:
正文完
星哥玩云-微信公众号
