Jenkins:使用SonarQube實現代碼審查

1. SonarQube簡介

在這裏插入圖片描述

SonarQube 是一個用於管理代碼質量的開放平臺,可以快速的定位代碼中潛在的或者明顯的錯誤。目前支持java、C#、C/C++、Python等二十幾種編程語言的代碼質量管理與檢測。

官網地址:https://www.sonarqube.org/

2. 安裝SonarQube

SonarQube的環境要求如下:

軟件 服務器 版本
JDK 192.168.1.20 1.8
MySQL 192.168.1.20 5.6
SonarQube 192.168.1.20 7.4

嗯,JDK和MySQL的安裝不是本篇文章的重點,請自行完成安裝。

(1)下載SonarQube壓縮包,下載地址:https://www.sonarqube.org/downloads/

(2)解壓,並設置權限

# 1.解壓
unzip sonarqube-7.4.zip
# 2.剪切到/usr/local/sonarqube
mv sonarqube-7.4/* /usr/local/sonarqube
# 3.創建sonar用戶
useradd sonar
# 4.更改sonarqube目錄及文件權限
chown -R sonar. /usr/local/sonarqube

(3)修改sonar配置文件

vim /usr/local/sonarqube/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

(4)啓動sonarqube

cd /usr/local/sonarqube
# 啓動
su sonar ./bin/linux-x86-64/sonar.sh start
# 查看狀態
su sonar ./bin/linux-x86-64/sonar.sh status
# 停止
su sonar ./bin/linux-x86-64/sonar.sh stop
# 查看日誌
tail -f logs/sonar.logs

注意:sonar默認端口是9000,如果9000端口被佔用,需要更改。

(5)訪問sonarqube

瀏覽器訪問http://192.168.1.20:9000,默認賬戶:admin/admin。

在這裏插入圖片描述

登錄成功之後,提示你創建一個token,後續跟Jenkins集成時要用到該token。

在這裏插入圖片描述

3. Jenkins集成

(1)安裝SonarQube Scanner插件

在這裏插入圖片描述

(2)添加SonarQube憑證

在這裏插入圖片描述

(3)SonarQube服務地址配置

路徑:Manage Jenkins->Configure System->SonarQube servers

在這裏插入圖片描述

(4)配置SonarQube Scanner

路徑:Manage Jenkins->Global Tool Configuration

在這裏插入圖片描述

(5)關閉SonarQube的SCM功能

在這裏插入圖片描述

4. 代碼審查

環境準備好之後,接下來我們就要通過Jenkins集成SonarQube實現代碼審查了。

4.1 非流水線項目

在這裏插入圖片描述

  • Analysis properties內容如下:
# must be unique in a given SonarQube instance
sonar.projectKey=demo
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=demo
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set.
sonar.sources=.
sonar.exclusions=**/test/**,**/target/**
sonar.java.source=1.8
sonar.java.target=1.8
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
  • 測試代碼
public class SonarQubeTest {

    public static void main(String[] args) {
        // 無效代碼
        String str = "這是一行無效代碼,不知道SonarQube能不能檢查出來!";
        // 異常代碼
        int a = 1/0;
        // 控制檯輸出
        System.out.println("Hello SonarQube!");
    }
}
  • 開始構建,並查看SonarQube代碼審查結果

在這裏插入圖片描述

4.2 流水線項目

說完非流水線項目如何集成SonarQube實現代碼審查,我們再來看看流水線項目的集成過程。

(1)項目根目錄下,創建sonar-project.properties文件,內容如下:

# must be unique in a given SonarQube instance
sonar.projectKey=demo
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=demo
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set.
sonar.sources=.
sonar.exclusions=**/test/**,**/target/**
sonar.java.source=1.8
sonar.java.target=1.8
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8

(2)修改Jenkinsfile,加入SonarQube代碼審查結果

pipeline {
 agent any
 stages {
   stage('拉取代碼') {
    steps {
      checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '8a039ab1-9d39-49a2-888b-03dbe9ee60e1', url: 'http://192.168.1.19:82/test-group/demo.git']]])
    }
   }
   stage('編譯構建') {
    steps {
      sh label: '', script: 'mvn clean package'
    }
   }
   stage('SonarQube代碼審查') {
    steps{
      script {
        scannerHome = tool 'SonarQube-Scanner'
      }
      withSonarQubeEnv('SonarQube7.4') {
        sh "${scannerHome}/bin/sonar-scanner"
      }
    }
   }
   stage('部署測試') {
       steps {
         echo '部署測試'
       }
      }
  }
  post {
    always {
      emailext(
      subject: '構建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',
      body: '${FILE,path="email.html"}',
      to: '[email protected]'
      )
    }
   }
}
——End——
更多精彩分享,可掃碼關注微信公衆號哦。

在這裏插入圖片描述

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