SonarQube 之 gitlab-plugin 配合 gitlab-ci 完成每次 commit

總體架構圖

image.png

具體配置過程

安裝sonarqubehtml

  第一步:java

sonarqube下載地址:http://www.sonarqube.org/downloads/解壓後移動到/usr/local目錄下mysql

解壓後壓縮包如圖:linux

image.png

第二部:配置環境變量git

image.png

第三步:配置sonar.propertiessql

[root@localhost local]# vim sonarqube-6.7.5/conf/sonar.properties docker

image.png

3.啓動SonarQube (重點,這塊最tm坑了)shell

 SonarQube 啓動不能用root啓動,因此新建一個用戶。必須先啓動elasticsearch,才能在啓動sonar。sonar文件目錄裏面有json

 elasticsearch這個目錄。(這塊好好看着,我在這坑了兩天。)vim

image.png

[esuser@localhost elasticsearch]# ./bin/elasticsearch -d     -d表明後臺運行

netstat   -nultp  查看端口,能看到9200和9300證實就成功了。

訪問:http://192.168.6.178:9200              能給你一個json的文件。

image.png

啓動sonar(用esuser啓動)

[esuser@localhost sonarqube-6.7.5]# ./bin/linux-x86-64/sonar.sh start

果能訪問,先按插件在sonar,要否則沒插件不行。

image.png

image.png

安裝SonarQube Runner和配置

下載sonar-scanner包
//解壓文件
//進入文件
//編輯文件
[root@localhost local]#unzip  sonar-scanner-cli-3.0.3.778-linux.zip
[root@localhost local]#mv sonar-scanner-cli-3.0.3.778-linux  sonar-scanner
[root@localhost local]# cd sonar-scanner
//修改配置文件
[root@localhost sonar-scanner]# vim conf/sonar-scanner.properties
#Configure here general information about the environment, such as SonarQube DB details for example
#No information about specific project should appear here
 
 
 
#----- Default source code encoding
sonar.sourceEncoding=UTF-8
 
sonar.host.url=http://192.168.6.178:9000
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:mysql://192.168.6.178:3306/sonar?useUnicode=true&characterEncoding=utf8
sonar.login=admin
sonar.password=admin

爲項目建立sonar-project.properties文件

在項目的根目錄下建立文件

image.png

#Configure here general information about the environment, such as SonarQube server connection details for example
#No information about specific project should appear here

#----- Default SonarQube server
sonar.host.url=http://localhost:9000
#----- Default source code encoding
#sonar.sourceEncoding=UTF-8
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
dStatements=true&useConfigs=maxPerformance
sonar.jdbc.username=root
sonar.jdbc.password=246367
sonar.sorceEncoding=UTF-8
sonar.login=admin
sonar.password=admin
 //開始掃描,
[root@localhost sell]# /usr/local/sonar-scanner/bin/sonar-scanner

參考:http://www.javashuo.com/article/p-ylaigrns-z.html

image.png

配置使用說明

一、配置 GitLab 通用帳戶

image.png

二、配置 sonar-gitlab-plugin 插件

下載sonar-gitlab-plugin-4.0.0.jar到sonarqube-7.4\extensions\plug×××中

image.png

admin 登陸 SonarQube,點擊 配置 —> 通用配置 —> GitLab —> Reporting —> 設置 GitLab User Token 以及 GitLab url。  

image.png

sonar-scanner安裝

下載

sudo curl --output /usr/local/bin/gitlab-runner https://gitlab-ci-multi-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-ci-multi-runner-darwin-amd64

2)給gitlab-runner賦可執行權限  

sudo chmod +x /usr/local/bin/gitlab-runner

3)註冊runner  

# gitlab-runner register

Please enter the gitlab-ci coordinator URL:
# 示例:http://gitlab.alibaba-inc.com/ci
Please enter the gitlab-ci token for this runner:
# xxxxxx
Please enter the gitlab-ci description for this runner:
# 示例:qd_api_runner
Please enter the gitlab-ci tags for this runner (comma separated):
# 示例:analyze
Whether to run untagged builds [true/false]:
# truePlease enter the executor: docker, parallels, shell, kubernetes, docker-ssh, ssh, virtualbox, docker+machine, docker-ssh+machine:# docker
Please enter the default Docker image (e.g. ruby:2.1):
# maven:3-jdk-8

說明: 一、gitlab ci的地址以及token,從你要配置該runner到哪一個項目,就去gitlab下該項目首頁右側設置—》CI/CD Pipelines—》Specific Runners下能夠找到。 二、gitlab-ci tags這個很重要,在項目構建流程yaml文件裏面指定tag,就是匹配使用哪一個tag的runner,這裏我定義了analyze,回頭再配置文件裏面就指定這個tag。 三、executor:執行者能夠有不少種,這裏咱們使用docker,方便構建執行。 四、Docker image:構建Docker image時填寫的image名稱,根據項目代碼語言不一樣,指定不一樣的鏡像。我這裏項目是java語言的,因此我使用官方maven:3-jdk-8鏡像。  

如圖:

image.png

4)安裝並啓動gitlab-runner

gitlab-runner ×××tall
gitlab-runner start

5).檢驗安裝狀態

./bin/sonar-scanner -h

成功:

image.png

定義項目構建流程

項目的構建流程是由項目根目錄的.gitlab-ci.yml文件控制的,關於gitlab-ci詳細的配置文檔能夠查看 這裏 , 如下是一個簡單的Java Maven項目的例子.gitlab-ci.yml:  

stages:
  - analyze
sonar_analyze:
  stage: analyze
  script:
    - bash /home/free-sonar-scanner-3.2-linux/bin/sonar-scanner
  tags:
    - analyze

咱們提交該文件到gitlab對應項目上去。

git add .gitlab-ci.yml
git commit -m "Add .gitlab-ci.yml"
git push origin

這個時候,咱們從該項目的Pipelines選項卡下能夠看到,有正在運行的剛新建的analyze的這個runner的pipelines了。點擊進去能夠看到控制檯實時輸出日誌

image.png

每次提出commit的時候都會觸發:

image.png

而後sonarqube界面中會有對應的數據:

image.png

添加阿里插件

製做sonar插件

  1. https://mvnrepository.com中查詢sonar-pmd-plugin獲取

    image.png

而後下載對應的jar------sonar-pmd-plugin-3.2.1.jar:

複製對應的jar到

image.png


2.將生成的插件放到/extensions/plug×××

3.重啓sonar,需切到非root帳號,先啓動es服務, ./elasticsearch -d  再啓動sonar服務./sonar.sh start

4.查看sonar和es服務日誌是否正常

image.png


image.png

5.sonar規則增長了p3c,完工。

參考:https://www.jianshu.com/p/f1bddb5e235f

參考:https://cloud.tencent.com/developer/article/1010595?from=10680

https://www.jianshu.com/p/1e59cd5cddb5

https://cloud.tencent.com/developer/article/1010612

https://www.jianshu.com/p/78ec15d2f332

http://192.168.10.59:9000/coding_rules?languages=java&open=squid%3AS2757

相關文章
相關標籤/搜索