部署SonarQube代码检测服务以及jenkins实现代码自动测试、自动部
1.SonarQube部署前的内核参数等配置以及Java环境配置
1)修改内核参数配置,使满足环境要求
[root@sonarqube ~]# vim /etc/sysctl.conf vm.max_map_count=262144 fs.file-max=65536 [root@sonarqube ~]# sysctl -p #生效修改的内核参数 …… vm.max_map_count = 262144 fs.file-max = 65536
2)修改本机安全策略参数限制
[root@sonarqube ~]# vim /etc/security/limits.conf …… 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)提前定义好数据的相关路径以及数据库启动用户
[root@sonarqube ~]# mkdir /data/mysql #数据库的相关数据保存路径 [root@sonarqube ~]# mkdir /data/binlog/bin #二进制日志存放目录 [root@sonarqube ~]# chown mysql.mysql -R /data/binlog [root@sonarqube ~]# chown mysql.mysql -R /data/mysql [root@sonarqube ~]# useradd -s /sbin/nologin/ mysql -u 1234
2)下载msyql源码压缩文件
[root@sonarqube ~]# ls /apps/mysql/ mariadb-10.2.23-linux-x86_64.tar.gz
3)解压并创建软连接
[root@sonarqube ~]# tar xvf mariadb-10.2.23-linux-x86_64.tar.gz [root@sonarqube ~]# ln -sv mariadb-10.2.23-linux-x86_64 mysql
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)建议 修改主机域名解析,将修改的主机名解析到本地本地回环
[root@sonarqube ~]# vim /etc/hosts 127.0.0.1 sonarqube localhost localhost.localdomain localhost4 localhost4.localdomain4 ……
7)执行数据库的初始化
[root@sonarqube scripts]# pwd /apps/mysql/mysql/scripts [root@sonarqube scripts]# ls mysql_install_db [root@sonarqube scripts]# mysql_install_db --user=mysql --datadir=/data/mysql
8)启动mysql服务
[root@sonarqube support-files]# pwd /apps/mysql/mysql/support-files [root@sonarqube support-files]# bash mysql.server start
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)下载源码压缩包
[root@sonarqube ~]# ls /apps/sonarqube sonarqube-6.7.7.zip
2)解压sonarqube源码包,并创建软连接
[root@sonarqube sonarqube]# unzip sonarqube-6.7.7.zip [root@sonarqube sonarqube]# ln -sv sonarqube-6.7.7 sonarqube ‘sonarqube’ -> ‘sonarqube-6.7.7’
3)创建用户以及更改目录的属主属组
[root@sonarqube sonarqube]# useradd sonarqube [root@sonarqube sonarqube]# chown sonarqube.sonarqube -R /apps/sonarqube/
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)下载扫描器到指定目录下
[root@sonarqube sonarscanner]# pwd /apps/sonarscanner [root@sonarqube sonarscanner]# ll -h total 41M -rw-r--r-- 1 root root 41M Jul 22 19:31 sonar-scanner-cli-4.0.0.1744-linux.zip
3)解压扫描器二进制源码并创建软连接
[root@sonarqube sonarscanner]# unzip sonar-scanner-cli-4.0.0.1744-linux.zip [root@sonarqube sonarscanner]# ls sonar-scanner-4.0.0.1744-linux sonar-scanner-cli-4.0.0.1744-linux.zip [root@sonarqube sonarscanner]# ln -sv sonar-scanner-4.0.0.1744-linux sonar-scanner ‘sonar-scanner’ -> ‘sonar-scanner-4.0.0.1744-linux’
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)准备一个测试代码压缩包并解压到当前目录下
[root@sonarqube code-test]# pwd /apps/code-test [root@sonarqube code-test]# ll -h total 6.5M -rw-r--r-- 1 root root 6.5M Nov 21 2018 sonar-examples-master.zip [root@sonarqube code-test]# unzip 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://[email protected]': 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://[email protected]': 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代码测试结果
相关推荐
sakurayz 2020-06-16
yuanyuan 2020-06-14
yuanyuan 2020-06-14
hevenue 2020-06-04
yieryi 2020-06-02
yuanyuan 2020-05-17
huaqiangli 2020-05-14
yangliuhbhd 2020-05-11
一片荷叶 2020-05-09
静水楼台 2020-05-09
huaqiangli 2020-04-27
yieryi 2020-04-10
郭宇 2020-02-14
xiaodaiwang 2020-02-02
xiaodaiwang 2020-01-09
leonranri 2019-12-16
xiaodaiwang 2019-11-02
chwnpp 2018-01-05
冯冯领队 2019-10-27
sakurayz 2019-10-24
sakurayz 2019-04-05
BigBirds 2019-09-12
huaqiangli 2019-08-05
wesaylove 2017-06-19
静水楼台 2019-07-24
mydownloador 2019-07-24
BORISHOU 2019-07-23
yuanyuan 2016-11-25
sakurayz 2015-07-25
骑苍蝇过大海 2015-06-19
BigBirds 2019-06-28
sakurayz 2019-06-28