Jenkins:使用SonarQube实现代码审查

1. SonarQube简介

在这里插入图片描述

SonarQube 是一个用于管理代码质量的开放平台,可以快速的定位代码中潜在的或者明显的错误。目前支持java、C#、C/C++、Python等二十几种编程语言的代码质量管理与检测。

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

2. 安装SonarQube

SonarQube的环境要求如下:

软件服务器版本
JDK192.168.1.201.8
MySQL192.168.1.205.6
SonarQube192.168.1.207.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: 'xxx@163.com'
      )
    }
   }
}
——End——
更多精彩分享,可扫码关注微信公众号哦。

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值