Linux環境安裝SonaQube,Jenkins使用SonaQube代碼審查

一、SonaQube 介紹

1、SonarQube 特性

多語言的平臺: 支持超過20種編程語言,包括Java、Python、C#、C/C++、JavaScript等常用語言。
自定義規則: 用戶可根據不同項目自定義Quality Profile以及Quality Gates。
豐富的插件: SonarQube 擁有豐富的插件,從而擁有強大的可擴展性。
持續集成: 通過對某項目的持續掃描,可以對該項目的代碼質量做長期的把控,並且預防新增代碼中的不嚴謹和冗餘。
質量門: 在掃描代碼後可以通過對“質量門”的比對判定此次“構建”的結果是否通過,質量門可以由用戶定義,由多維度判定是否通過。

2、在項目中一般流程爲:

(1) 項目人員開發代碼。
(2) 將代碼推送到持久化倉庫,如 Git。
(3) Jenkins 進行代碼拉取,然後利用 SonarQube 掃描器進行掃描分析代碼信息。
(4) 將分析結果等信息上傳至 SonarQube Server 服務器進行分類處理。
(5) SonarQube 將分析結果等信息持久化到數據庫,如 Mysql。
(6) 開發人員訪問 SonarQube UI 界面訪問,查看掃描出的結果信息進行項目優化。

Jenkins 如何與 SonarQube 集成圖分析

在這裏插入圖片描述

二、Linux 環境安裝 SonaQube

這裏用的是 sonarqube-7.6 的版本。官網提供的版本是 sonarqube-8.2官網安裝鏈接,或自定義版本 各版本選裝鏈接

1、安裝 SonarQube 前需要先安裝 Mysql 數據庫

這裏是使用 Yum 安裝 Mysql,版本 MySQL/5.7.27

yum localinstall  http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm -y
yum install mysql-community-server -y
yum install mysql-community-devel -y
啓動數據庫
systemctl enable mysqld
systemctl start mysqld

修改數據庫密碼,授權用戶連接登錄

初始化前密碼在此路徑下
grep 'temporary password' /var/log/mysqld.log
修改 mysql root 密碼
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
授權用戶遠程登錄
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
刷新
flush privileges;

連接數據庫,創建 sonar 數據庫
mysql> create database sonar;

2、下載並配置 SonaQube

wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.6.zip
unzip sonarqube-7.6.zip
mv sonarqube-7.6 /usr/local/sonar
useradd sonar 創建sonar用戶,必須sonar用於啓動,否則報錯
chown -R sonar. /usr/local/sonar 更改sonar目錄及文件權限

修改sonar配置文件
vim /usr/local/sonar/conf/sonar.properties
修改的內容如下:
sonar.jdbc.username=root
sonar.jdbc.password=123456
去掉註釋
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
注意:sonar默認監聽9000端口,如果9000端口被佔用,需要更改。這裏改爲9900
sonar.web.port=9900

訪問 SonaQube :http://localhost:9900,默認賬戶: admin/admin
添加 token
在這裏插入圖片描述

三、Jenkins 安裝 SonaQube 插件和環境配置

1、安裝SonarQube Scanner插件

在這裏插入圖片描述

2、添加新憑證

在 Jenkins 中添加新的憑證,類型爲 Secret text,Secret 就是剛創建的 token 值
添加憑證的操作可參考:Jenkins使用手冊-憑證管理
在這裏插入圖片描述

3、Jenkins進行SonarQube配置

Manage Jenkins -> Configure System -> SonarQube servers
名字可以隨便命名,這裏命名爲:jenkins,名字後續會用到
在這裏插入圖片描述
Manage Jenkins -> Global Tool Configuration
選擇自動安裝,再選擇安裝的版本即可,,名字爲:sonar-scanner ,後續會用到
在這裏插入圖片描述

4、SonaQube 關閉審查結果上傳到SCM功能

在這裏插入圖片描述

四、項目添加 SonaQube 代碼審查

審查的步驟:
在這裏插入圖片描述

SonaQube 代碼審查 ,流水線項目或者非流水線項目

常用的項目類型可參考文檔:Jenkins項目常用三種構建類型風格詳解

(1)在項目添加 SonaQube 代碼審查:(非流水線項目)

添加構建步驟 選擇Execute SonaQube Scanner:

在這裏插入圖片描述
Analysis properties 代碼片段

sonar.projectKey=demo_free_style_passwd
sonar.projectName=demo_free_style_passwd
sonar.projectVersion=1.0
sonar.sources=.
sonar.exclusions=**/test/**,**/target/**
sonar.java.source=1.8
sonar.java.target=1.8
sonar.sourceEncoding=UTF-8

構建完成後可看到對應的代碼審查結果
在這裏插入圖片描述

(2)在項目添加 SonaQube 代碼審查:(流水線項目)

1、項目根目錄下,創建 sonar-project.properties 文件

在這裏插入圖片描述

# 項目在 SonarQube 的唯一標識,不能重複
sonar.projectKey=web_demo_Pipeline_key
# 項目名稱 
sonar.projectName=web_demo_Pipeline_key
# 項目版本
sonar.projectVersion=1.0
# 項目語言,例如 Java、C#、PHP 等
# sonar.language: 
# 需要掃描的項目源代碼目錄,點代表是掃描根下面的全部,也可以指定目錄“例如:/src”
sonar.sources=.
#  編譯後 test下或target下 文件目錄
sonar.exclusions=**/test/**,**/target/**
sonar.java.source=1.8
sonar.java.target=1.8
# sonar.sourceEncoding: 編碼方式
sonar.sourceEncoding=UTF-8
2、修改Jenkinsfile,加入SonarQube代碼審查階段

腳本的路徑在根下:
在這裏插入圖片描述
腳本的命令:

pipeline {
   agent any
   stages {
      stage('pull code') {
         steps {
            checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'ed3147fc-c68b-418c-a479-15dd80710051', url: '[email protected]:arosendeng/rosen-jenkins.git']]])
         }
      }
      stage('build projeck') {
         steps {
            sh label: '', script: 'mvn clean package'
         }
      }
    stage('SonarQube Code detection') {
         steps{
            script {
                scannerHome = tool 'sonar-scanner'
                    }
            withSonarQubeEnv('jenkins') {
                sh "${scannerHome}/bin/sonar-scanner"
                    }
                }
            }
      stage('push code') {
         steps {
             deploy adapters: [tomcat9(credentialsId: 'b1ca6678-8339-43bb-a0da-c3935c509dc2', path: '', url: 'http://192.168.66.102:8080/')], contextPath: null, war: 'target/*.war'
         }
      }      
   }
    post {
        always { //構建完成後郵件通知
            emailext body: '${FILE,path="src/email.html"}', subject: '構建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}', to: '[email protected]'
                }
        }
}
3、到SonarQube的UI界面查看審查結果

在這裏插入圖片描述

以上就是 SonaQube 代碼審查 的操作,主要針對飛流水線和流水線的操作

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章