• 有手就行10——Jenkins+SonarQube代码审查


    有手就行10——Jenkins+SonarQube代码审查

     

     

    Jenkins+SonarQube代码审查(1) - 安装SonarQube

    Jenkins+SonarQube代码审查(2) - 实现代码审查

     

     

    Jenkins+SonarQube代码审查(1) - 安装SonarQube

    SonarQube是一个用于管理代码质量的开放平台,可以快速的定位代码中潜在的或者明显的错误。

    目前 支持java,C#,C/C++,Python,PL/SQL,Cobol,JavaScrip,Groovy等二十几种编程语言的代码质量管理与检测,

    底层使用elasticsearch作为代码检索工具。

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

     

    实验环境:(与jenkins 在同一台服务器)主要是快

    软件

    服务器

    版本

    JDK

    20.0.0.30

    1.8

    MySQL

    20.0.0.30

    5.7

    SonarQube

    20.0.0.30

    6.7.4

     

    SonarQube        

    1)安装MySQL(已完成)不过多讲解。重点:需要授权 不然后面sonar起不来

    2)安装SonarQube

    在MySQL创建sonar数据库

     

     

     

    下载sonar压缩包:

    https://www.sonarqube.org/downloads/

    解压sonar,并设置权限

    1
    2
    3
    4
    5
    6
    yum install unzip               #(已装)
    unzip sonarqube-6.7.4.zip       #解压
    mkdir /opt/sonar             #创建目录
    mv sonarqube-6.7.4/* /opt/sonar     #移动文件
    useradd sonar              #创建sonar用户,必须sonar用于启动,否则报错
    chown -R sonar.  /opt/sonar      #更改sonar目录及文件权限 

    修改sonar配置文件:

    1
    2
    3
    4
    5
    6
    cd /opt/sonar
    vim sonar/conf/sonar.properties<br>
    内容如下:
    sonar.jdbc.username=root
    sonar.jdbc.password=abc123
    sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar? useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs= maxPerformance&useSSL=false   (取消注释即可)

    注意:sonar默认监听9000端口,如果9000端口被占用,需要更改。 启动sonar(注意:切换sonar用户

    1
    2
    3
    4
    5
    cd /opt/sonar<br>
    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:

    http://20.0.0.30:9000

     

    默认账户:admin/admin 创建token

     

     进去输入名称默认生成一个密钥

     

    lvbu: 5013051625e85359eca937815c59a2da393707a5Jenkins整合会使用此密钥

    token要记下来!!!

     

     

    Jenkins+SonarQube代码审查(2) - 实现代码审查

     

     

     

    安装SonarQube Scanner插件

     

     

    安装SonarQube

     

     

     

     

    添加SonarQube凭证

     

     

    Jenkins进行SonarQube配置

    Manage Jenkins->Configure System->SonarQube servers

     

     

     

     

     

    在项目添加SonaQube代码审查(非流水线项目)

    以自由风格为例:打开

     

     

     

     

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    # must be unique in a given SonarQube instance
    sonar.projectKey=web_demo_freestyle
    # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
    sonar.projectName=web_demo_freestyle
    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

      

    然后构建:

    sonarqube服务器上刷新,查看结果

     

     

     

     

     

    测试错误代码

    新建Javaresource目录

     

     

     

     

     

    配置pom.xml文件添加对servlet的依赖

     

     

     

    1
    2
    3
    4
    5
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
    </dependency>

      

     

    新建编写Servlet文件

     

     

     创建名称:com.lvbu.HelloServlet

    内容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    ackage com.lvbu;
     
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
     
    public class HelloServlet extends HttpServlet {
     
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            super.doPost(req,resp);
        }
     
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //模拟错误代码
            int i = 100/0;
     
            //模拟代码冗余
            int j = 100;
            j = 200;
     
            resp.getWriter().write("hello Servlet");
        }
    }

      

     

    然后代码提交:

     

     

     

     

     

    然后构建:看结果:

     

     

     

     其中可能会报错:

      

    解决方法:

     

     

     

     

     

     

     

     

     

    然后再次提交项目加构建:

     

     代码检查后就可以了!

     

    在项目添加SonaQube代码审查(流水线项目)

    1) 项目根目录下,创建sonar-project.properties文件

     

    # must be unique in a given SonarQube instance
    sonar.projectKey=web_demo_lsx
    # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
    sonar.projectName=web_demo_lsx
    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('pull code') {
    steps {
    checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: 'd5bb0e98-15f2-477f-8db7-2c33ecc6c644', url: 'git@20.0.0.20:niuma/web_demo.git']]])
    }
    }
    stage('code checking') {
    steps {
    script {
    //引入了sonarqube-scanner工具
    scannerHome = tool 'sonar-scanner'
    }
    //引入了sonarqube服务器系统环境
    withSonarQubeEnv('sonarqube') {
    sh "${scannerHome}/bin/sonar-scanner"
    }
    }
    }
    stage('build project') {
    steps {
    sh 'mvn clean package'
    }
    }
    stage('deploy item') {
    steps {
    deploy adapters: [tomcat8(credentialsId: '38dcb730-8901-41bb-b8d0-d1500aa9cf79', path: '', url: 'http://20.0.0.40:8080/')], contextPath: null, war: 'target/*.war'
    }
    }
    }
    post {
    always {
    emailext(
    subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',body: '${FILE,path="email.html"}', to: '1321519531@qq.com'
    )
    }
    }
    }

      

     

    把更改后的sonar-project.propertiesJenkinsfile进行提交

     

     

    然后开始构建:

     

     

     

    查看测试结果:

     

     

     

    邮件通知也会收到:

     

     

  • 相关阅读:
    【react】使用useEffect操作dom
    学习笔记18--自动驾驶智能化指标评测体系(上)
    echarts柱状图TOP排名
    KubeSphere 集群配置 NFS 存储解决方案-收藏版
    wpa_supplicant与用户态程序的交互分析
    非对称加密(RSA)详解
    【152.乘积最大子数组】
    Python selenium自动化操作Chrome浏览器
    可移植性测试包括哪些
    21天学会C++:Day9----初识类与对象
  • 原文地址:https://www.cnblogs.com/lvrui/p/15897082.html