部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

部署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 /usr/local/src/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    
[root@sonarqube src]# source /etc/profile  #將java環境變量生效

2.數據庫準備(sonarqube7.9版本再也不支持mysql,以sonarqube6.7爲例,並要求至少mysql5.6以上,這裏使用mysql5.7版本)

1)官網下載mysql社區版配置yum源倉庫的rpm包(下載前須要註冊oracle帳號)

https://dev.mysql.com/downloads/repo/yum/

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署
部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

2)安裝mysql的yum倉庫源

[root@sonarqube mysql.repos]# ls
mysql80-community-release-el7-3.noarch.rpm
[root@sonarqube mysql.repos]# rpm -ivh mysql80-community-release-el7-3.noarch.rpm 
warning: mysql80-community-release-el7-3.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
     1:mysql80-community-release-el7-3  ################################# [100%]

3)查看安裝的yum倉庫源

root@sonarqube yum.repos.d]# ls
CentOS-Base.repo  epel.repo  mysql-community.repo  mysql-community-source.repo

4)查看yum子倉庫中被禁用的和啓用的mysql版本,默認最新版 MySQL 8.0是開啓的(yum安裝時也是默認最新版),其餘版本默認禁用

[root@sonarqube yum.repos.d]# yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community   disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community   disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - disabled
mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Community   disabled
mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community - disabled
mysql-connectors-community/x86_64  MySQL Connectors Community    enabled:    118
mysql-connectors-community-source  MySQL Connectors Community -  disabled
mysql-tools-community/x86_64       MySQL Tools Community         enabled:     95
mysql-tools-community-source       MySQL Tools Community - Sourc disabled
mysql-tools-preview/x86_64         MySQL Tools Preview           disabled
mysql-tools-preview-source         MySQL Tools Preview - Source  disabled
mysql55-community/x86_64           MySQL 5.5 Community Server    disabled
mysql55-community-source           MySQL 5.5 Community Server -  disabled
mysql56-community/x86_64           MySQL 5.6 Community Server    disabled
mysql56-community-source           MySQL 5.6 Community Server -  disabled
mysql57-community/x86_64           MySQL 5.7 Community Server    disabled
mysql57-community-source           MySQL 5.7 Community Server -  disabled
mysql80-community/x86_64           MySQL 8.0 Community Server    enabled:    129
mysql80-community-source           MySQL 8.0 Community Server -  disabled

5)yum-config-manager命令依賴於yum-utils.noarch包

[root@sonarqube yum.repos.d]# yum install yum-utils.noarch

6)下載mysql-5.7版本,則需啓用子倉庫的mysql-5.7版本,並將 MySQL 8.0版子倉庫禁用,讓yum安裝默認下載5.7版本

[root@sonarqube yum.repos.d]# yum-config-manager --enable mysql57-community
[root@sonarqube yum.repos.d]# yum-config-manager --disable mysql80-community

7)再次查看子倉庫源,發現mysql-5.7版本子倉庫源已啓用

[root@sonarqube yum.repos.d]# yum repolist all | grep mysql
mysql55-community/x86_64           MySQL 5.5 Community Server    disabled
mysql55-community-source           MySQL 5.5 Community Server -  disabled
mysql56-community/x86_64           MySQL 5.6 Community Server    disabled
mysql56-community-source           MySQL 5.6 Community Server -  disabled
mysql57-community/x86_64           MySQL 5.7 Community Server    enabled:    364
mysql57-community-source           MySQL 5.7 Community Server -  disabled
mysql80-community/x86_64           MySQL 8.0 Community Server    disabled
mysql80-community-source           MySQL 8.0 Community Server -  disabled

8)下載mysql,此時默認下載mysql-5.7版本

[root@sonarqube ~]# yum install mysql mysql-server -y

9)修改mysql數據庫默認數據存儲路徑,準備msyql數據指定的存儲目錄

[root@sonarqube ~]# mkdir /data/mysql  #數據存儲目錄
[root@sonarqube ~]# mkdir /data/binlog/bin -pv  #二進制日誌存儲目錄
[root@sonarqube ~]# mkdir /data/socket/    # 套接字目錄

10)分別受權

[root@sonarqube ~]# chown mysql.mysql -R /data/mysql
[root@sonarqube ~]#  chown mysql.mysql -R /data/logbin/
[root@sonarqube ~]#  chown mysql.mysql -R /data/socket/

11)查看目錄權限

[root@sonarqube ~]# ll /data
total 0
drwxr-xr-x 3 mysql mysql  52 Aug  2 00:15 binlog
drwxr-x--- 5 mysql mysql 314 Aug  2 00:16 mysql
drwxr-xr-x 2 mysql mysql   6 Aug  2 00:19 socket

12)mysql配置文件修改

[root@sonarqube ~]# grep '^[a-z]' /etc/my.cnf
log_bin=/data/binlog/bin
server_id=1
datadir=/data/mysql
socket=/data/socket/mysql.sock
……

13)啓動服務

[root@sonarqube ~]# systemctl restart mysqld

14)查看啓動的端口服務

[root@sonarqube ~]# ss -tnlp
State       Recv-Q Send-Q                      Local Address:Port                                     Peer Address:Port              
LISTEN      0      128                                     *:22                                                  *:*                   users:(("sshd",pid=3710,fd=3))
LISTEN      0      100                             127.0.0.1:25                                                  *:*                   users:(("master",pid=3991,fd=13))
LISTEN      0      80                                     :::3306                                               :::*                   users:(("mysqld",pid=22395,fd=24))
LISTEN      0      128                                    :::22                                                 :::*                   users:(("sshd",pid=3710,fd=4))
LISTEN      0      100                                   ::1:25                                                 :::*                   users:(("master",pid=3991,fd=14))

15)因爲套接字路徑已改,直接進入數據庫是默認是指定套接字路徑/var/lib/mysql/mysql.sock,需指定更改後的路徑

[root@sonarqube ~]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

16)指定更改後的路徑,還須要輸入數據庫臨時密碼

[root@sonarqube ~]# mysql -S /data/socket/mysql.sock
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

17)數據庫啓動後會在日誌文件中生成一個臨時密碼,輸入臨時密碼便可登陸數據庫

[root@sonarqube ~]# grep 'temporary password' /var/log/mysqld.log
2019-08-01T16:29:12.727126Z 1 [Note] A temporary password is generated for root@localhost: ?B9v5Eg7eost
[root@sonarqube ~]# mysql -S /data/socket/mysql.sock -p?B9v5Eg7eost   #進入數據庫訪問
……
Server version: 5.7.27-log

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>

18)再修改密碼替代臨時密碼,便可訪問數據中信息(密碼至少8個字符以上且必須包含有字母、數字、字符,至少含有一個大寫字母)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Jie?123456';
Query OK, 0 rows affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

19)建立sonarqube數據庫並受權sonarque用戶

mysql> create database sonarqube default character set utf8 collate utf8_general_ci;
mysql> grant all on sonarqube.* to sonarqube@'192.168.1.31' identified by 'Jie?123456
Query OK, 0 rows affected, 1 warning (0.00 sec)

20)用sonarqube用戶登陸mysql數據庫測試是否能進入數據庫

[root@sonarqube ~]# mysql -usonarqube -pJie?123456 -S /data/socket/mysql.sock -h192.168.1.31
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 5
Server version: 5.7.27-log MySQL Community Server (GPL)

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 |
| sonarqube          |
+--------------------+
2 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=Jie?123456  #登陸數據庫的密碼
sonar.jdbc.url=jdbc:mysql://192.168.1.31:3306/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false      
#jdbc:mysql://192.168.1.31:3306/sonarqube 中的sonarqube爲建立的數據庫名稱
sonar.web.host=0.0.0.0   #sonarqube服務監聽本機全部ip
sonar.web.port=9000   #sonarqube服務啓動時監聽的端口

5)啓動sonarqube服務

[sonarqube@sonarqube sonarqube]$ ./bin/linux-x86-64/sonar.sh  start
Starting SonarQube...
Started SonarQube.

6)在查看sonarqube服務啓動狀態,發現沒法啓動,去查看日誌文件中的報錯

[sonarqube@sonarqube sonarqube]$ ./bin/linux-x86-64/sonar.sh status
SonarQube is not running.

7)查看sonarqube服務的日誌文件,發現報錯是因爲內存分配不足致使服務沒法啓動,

[root@sonarqube logs]# tail -30 sonar.log
......
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000e0000000, 89653248, 0) failed; error='Cannot allocate memory' (errno=12)
......

8)關閉虛擬機,將內存調大,至少3G以上,在從新啓動sonaqube服務並查看運行狀態

[sonarqube@sonarqube sonarqube]$ ./bin/linux-x86-64/sonar.sh  start  啓動服務
Starting SonarQube...
Started SonarQube.
[sonarqube@sonarqube sonarqube]$ ./bin/linux-x86-64/sonar.sh status  #發現服務已啓動
SonarQube is running (5380).

9)查看監聽的9000端口

[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))

10)瀏覽器訪問sonarqube

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

11)登陸的用戶名及密碼均默認爲admin

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

12)成功登陸sonarqube的web頁面

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

4.安裝中文插件,顯示爲中文界面

1)點擊administration→Marketplace,直接搜索中文插件

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

2)在點擊安裝install

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

3)從新啓動服務

[sonarqube@sonarqube linux-x86-64]$ ./sonar.sh restart

4)再次訪問時,顯示中文界面

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

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等開發語言插件,才能掃描相關語言代碼

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署
部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署
部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

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端查看掃描後生成的記錄

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

6.jenkins配置sonarqube-scanner並實現jenkins代碼的自動測試、自動部署

1)安裝掃描器插件

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

2)點擊系統設置設置sonarqube服務

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

3)添加sonarqube服務並自定義服務名稱以及url地址

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

4)自動安裝scanner掃描器

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

5)若已安裝scanner掃描器則無需自動安裝,直接添加掃描器的工做目錄便可

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

6)在jenkins建立一個新項目code-test-job

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

7)配置此項目的configure

指定gitlab倉庫源、認證方式

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

構建觸發的口令祕鑰

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

代碼掃描的配置參數,其指定檢測代碼的類型、編碼格式、路徑等

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

8)在gitlab服務器中對的系統鉤子添加jenkins認證地址,以實現自動觸發jenkins的項目構建

部署SonarQube代碼檢測服務以及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服務端查看代碼已提交成功

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

11)提交代碼到gitlab後自動觸發jenkins項目構建

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

12)查看項目構建成功的控制檯信息輸出

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

13)查看sonarqube-server服務器記錄下來的php代碼測試結果

部署SonarQube代碼檢測服務以及jenkins實現代碼自動測試、自動部署

相關文章
相關標籤/搜索