开发辅助技术

Jenkins:使用SonarQube实现代码审查

2020-06-04  本文已影响0人  Anbang713

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 非流水线项目

# 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!");
    }
}

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: 'xxx@163.com'
      )
    }
   }
}
上一篇下一篇

猜你喜欢

热点阅读