sonarqube6.7安裝及配置

部署:

1.先爲sonar創建mysql數據庫

mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci; 
mysql> CREATE USER 'sonar' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY '***';
mysql> FLUSH PRIVILEGES;

2.啓動sonarqube服務

#docker run -d --name sonar --restart=always -p 9000:9000 -p 9092:9092 \

            -e SONARQUBE_JDBC_USERNAME=sonar -e SONARQUBE_JDBC_PASSWORD=**** \
             -e SONARQUBE_JDBC_URL=jdbc:mysql://10.103.27.177:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance\
                      -v /server/sonarqube:/opt/sonarqube sonarqube
3.訪問:

IP:9000

4.在maven項目中使用

   

mvn sonar:sonar \
-Dsonar.host.url=IP:9000 \
-Dsonar.login=b5b3cd724171df9d6fb2efb3f9c5e2d4724789f6



配置:

1.代理

如果本地計算機訪問外網需要代理,sonar更新插件也需要配置代理,配置方法如下:

進入sonar安裝目錄/conf目錄,打開sonar.properties配置文件,找到update center的配置,配置如下

     

# UPDATE CENTER
# Update Center requires an internet connection to request http://update.sonarsource.org
# It is enabled by default.將這一項設置成true
sonar.updatecenter.activate=true

# HTTP proxy (default none)配置代理ip地址和端口
http.proxyHost=172.30.XX.XX
http.proxyPort=8080

# NT domain name if NTLM proxy is used
#http.auth.ntlm.domain=

# SOCKS proxy (default none)
#socksProxyHost=
#socksProxyPort=


# proxy authentication. The 2 following properties are used for HTTP and SOCKS proxies.配置登錄計算機的用戶名和密碼
http.proxyUser=test
http.proxyPassword=test

2.與ldap集成

  修改/opt/sonar/config/sonar.propertie文件,添加如下:

# LDAP configuration
# General Configuration
sonar.security.realm=LDAP
sonar.security.savePassword=true
ldap.url=ldap://IP:389
ldap.bindDn=cn=admin,dc=example,dc=org
ldap.bindPassword=****

# User Configuration
ldap.user.baseDn=ou=People,dc=example,dc=org
#ldap.user.request=(&(objectClass=user)(sAMAccountName={login}))
ldap.user.request=(&(objectClass=inetOrgPerson)(cn={login})) //注意這裏要看自己的ldap中是用的uid還是cn
ldap.user.realNameAttribute=cn
ldap.user.emailAttribute=mail

# Group Configuration
#ldap.group.baseDn=ou=Groups,dc=sonarsource,dc=com
ldap.group.request=(&(objectClass=groupOfNames)(member={dn}))
ldap.group.idAttribute=cn
#sonar.log.level=DEBUG         //生產使用的時候註釋掉以免影響性能

 參考:https://docs.sonarqube.org/display/PLUG/LDAP+Plugin


3.使用

  .設置規則

  在系統菜單----質量配置中選擇適合自己公司的規則或新創建一套規則(也可以通過拷貝已有的規則)

  .設置質量門,如圖

   

  意思是當有阻斷問題的大於0個則報錯,當覆蓋率小於80%則報錯

  分析週期開始時,如果只添加或修改很少代碼,就很難達到期望的代碼覆蓋或重複級別。爲了避免質量閾失效,如果工作量不大時,只有新代碼超過20行時,纔會計算新代碼重複讀和覆蓋率的質量閾條件。


其中指標參數說明:

 可靠性比率的計算方法
A = 0 Bug 最高等級A,表示代碼無bug
B = at least 1 Minor Bug 代碼只要有一個次要bug,等級就爲B
C = at least 1 Major Bug 只要包含一個重要bug,等級將爲C
D = at least 1 Critical Bug 只要有一個嚴重bug,等級評估爲D
E = at least 1 Blocker Bug 只要有一個最高等級的阻斷級別的bug,可靠性評估爲E,最低級別。
A = 0 Vulnerability 沒有漏洞時,項目評估爲最高級別A
B = at least 1 Minor Vulnerability 只要包含一個次要漏洞,項目評估爲級別B
C = at least 1 Major Vulnerability 只要包含一個重要漏洞,項目評估爲級別C
D = at least 1 Critical Vulnerability 只要包含一個嚴重漏洞,評估爲D
E = at least 1 Blocker Vulnerability 只要包含一個阻斷漏洞,項目評估爲最低級別E

參考:http://www.genshuixue.com/i-cxy/p/15635675


4.與jenkins的pipeline集成

    首先需要在jenkins的系統管理中配置sonar server

pipeline {
    agent  { label 'ci' }

    stages {
         stage('CheckCode') {
            steps {
                git branch: '******', credentialsId: '212d9ef0-c9c2-4bec-bc2f-6cb7065ee4b2', url: 'git@***.git'
            }
        }
 
        stage('靜態檢查') {
           steps {
               echo "starting codeAnalyze with SonarQube......"
               //sonar:sonar.QualityGate should pass
               withSonarQubeEnv('SonarQube') {
                 //固定使用項目根目錄${basedir}下的pom.xml進行代碼檢查
                  withMaven(jdk: 'jdk1.8', maven: 'mvn') {
                    sh "mvn -f pom.xml -U clean compile sonar:sonar -Dsonar.branch=$BRANCH
"
                            }
                 
               }
               script {
               timeout(100) { 
                   //利用sonar webhook功能通知pipeline代碼檢測結果,未通過質量閾,pipeline將會fail
                   def qg = waitForQualityGate() 
                       if (qg.status != 'OK') {
                           error "未通過Sonarqube的代碼質量閾檢查,請及時修改!failure: ${qg.status}"
                       }
                   }
               }
           }
       }
    }
    post {    
    
            failure {
        		emailext body: '${JELLY_SCRIPT,template="static-analysis"}', recipientProviders: [[$class: 'DevelopersRecipientProvider']], subject: '構建通知:$PROJECT_NAME - Build # $BUILD_NUMBER - Failure!'
	        }
	        success {
        	   emailext body: '${JELLY_SCRIPT,template="static-analysis"}', recipientProviders: [[$class: 'DevelopersRecipientProvider']], subject: '構建通知:$PROJECT_NAME - Build # $BUILD_NUMBER - Success!'
        	}
	        aborted {
        		emailext body: '${JELLY_SCRIPT,template="static-analysis"}', recipientProviders: [[$class: 'DevelopersRecipientProvider']], subject: '構建通知:$PROJECT_NAME - Build # $BUILD_NUMBER - Aborted!'
        	}
            
    }

}

5.針對某個分支掃描分析,增量分析,增量報告

    mvn -f pom.xml -U clean compile sonar:sonar 

                -Dsonar.analysis.mode=preview -Dsonar.issuesReport.console.enable=true  

                -Dsonar.branch=$BRANCH_HOME    //經驗證可以實現對分支的掃描分析,結果展示以另外的項目體現在sonar中。

       -Dsonar.analysis.mode=preview //在sonar6.7.1+pipelen中分析正常,也確實是增量分析,但驗證質量時報錯。。。。

其實也可以通過設置質量門“新增阻礙問題”來實現針對增量代碼是否引入了新的阻礙問題,缺點就是也是全量分析(有點小耗時)

參考:http://blog.csdn.net/aixiaoyang168/article/details/77749552

6.配置使用中遇到的問題

  i.

   

       原因:因爲做了通過nginx轉發,根據提示是數據太大,ng默認文件上傳大小支持1M;

       解決:

打開nginx配置文件 nginx.conf, 路徑一般是:/etc/nginx/nginx.conf;

。在http{}段中加入 client_max_body_size20m; 20m爲允許最大上傳的大小(大小可自定義)。

保存後重啓nginx,問題解決。


  ii.構建完報錯

java.lang.IllegalStateException: Unable to parse response fromhttp://sonar.abc.cn//api/ce/task?id=AWEiaX0J_bmqEf43r92X:

解決:修改jenkins系統配置中sonar配置,url爲http://sonar.abc.cn 注意不要加/

  iii.Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (22790518 > 16777216). You can change this value on the server by setting the max_allowed_packet’ variable.

 解決:

     

show variables like '%max_allowed_packet%';
更改mysql 的max_allowed_packet參數,設置 max_allowed_packet = 64M ,然後重啓mysql
[mysqld]
max_allowed_packet=32M
https://dev.mysql.com/doc/refman/5.7/en/packet-too-large.html

 注意:有些報錯是可以通過查看/opt/sonarqube/logs/web.log可以看到的

發佈了140 篇原創文章 · 獲贊 45 · 訪問量 90萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章