DevOps之Pipeline集成junit、jacoco、SonarQube(二)

一、准备工作

1、准备一个持续集成的代码工程

工程下载地址:

Github地址为:https://github.com/zbbkeepgoing/springboot-demo

2、springboot-demo代码工程介绍

整个Web工程有一个Index页面,上面有两个按钮,分别对应两个接口,其中一个接口直接返回信息,另外一个接口则是内存中请求一次延时1s,最大延时为10s。而对应Index会有一个接口,所以Web工程一共有3个接口。延时接口主要是为了后续性能测试

工程结构

└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── dxc
    │   │           └── ddccloud
    │   │               └── demo
    │   │                   ├── controller
    │   │                   │   └── DemoController.java  #控制器,接口定义类
    │   │                   └── DemoApplication.java   #启动类
    │   └── resources
    │       ├── application.properties #配置文件
    │       └── templates
    │           └── index.html  #首页Index
    └── test
        └── java
            └── com
                └── dxc
                    └── ddccloud
                        └── demo
                            └── DemoControllerTests.java #单元测试类

②DemoController.java

package com.dxc.ddccloud.demo.controller;

import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class DemoController {

    public int tmp = 0;
    
    @RequestMapping("/")   #首页接口
    public ModelAndView index(ModelAndView mv) {
        mv.setViewName("index");
        mv.addObject("requestname","This request is IndexApi");
        return mv;
    }
    
    @RequestMapping("/rightaway")  #立即返回接口
    public ModelAndView returnRightAway(ModelAndView mv) {
        mv.setViewName("index");
        mv.addObject("requestname","This request is RightawayApi");
        return mv;
    }
    
    @RequestMapping("/sleep")   #延时接口
    public ModelAndView returnSleep(ModelAndView mv) throws InterruptedException {
        Thread.sleep(tmp*1000);
        if(tmp < 10) {
            tmp++;
        }
        mv.setViewName("index");
        mv.addObject("requestname","This request is SleepApi"+",it will sleep "+ tmp +"s !");
        return mv;
    }
}

3、下载代码工程放到公司内部私有仓库GitLab

①安装Junit插件

 ②安装jacoco插件

③安装SonarQube Scanne

说明:上述插件具体怎么配置,请自行爬楼参考以前的博客

二、新建Pipeline工程springboot-demo

 1、Pipeline内容

node('build-slave01') {
    gitlabCommitStatus(name:"Commit Build"){
  stage('拉取代码'){
     git credentialsId: 'd1627e89-1967-458e-84a8-07c9a9599957', url: 'git@10.0.0.56:springboot/springboot-demo.git'  #私有仓库地址
  }
}

    stage('Build'){
        dir(env.WORKSPACE){
        sh "mvn clean compile"  #maven编译
        sh "mvn package"        #maven打包
        }
    }
    
    stage('Junit'){
        dir(env.WORKSPACE){
        junit allowEmptyResults: true, keepLongStdio: true, testResults: 'target/surefire-reports/*.xml' #Junit插件收集单元测试结果
        }
    }
    
    stage('jacoco'){
        dir(env.WORKSPACE){
        jacoco exclusionPattern: 'src/main/java', inclusionPattern: '**/classes'  #jacoco
        }
    }
    
    //这里需要注意的配置
    //  Path to exec files: **/jacoco.exec 可执行文件路径
    //  Path to class directories: 这个配置的是源代码编译后的字节码目录,也就是classes目录不是test-classes目录,如果有多个可以指定多个
    //  Path to source directories: 这个配置的是源代码的目录,也就是src/main/java目录,如果有多个可以指定多个
    
     stage('SonarQube') {
        withSonarQubeEnv('SonarQube') {
            sh "${SONAR_SCANNER_HOME}/bin/sonar-scanner \
            -Dsonar.projectKey=springboot-demo \
            -Dsonar.projectName=springboot-demo \
            -Dsonar.sources=.\
            -Dsonar.host.url=SonarQube地址 \
            -Dsonar.language=java \
            -Dsonar.sourceEncoding=UTF-8 \
        }
    }
}

2、Jacoco配置

这里需要注意的配置:

  • Path to exec files: **/jacoco.exec 可执行文件路径
  • Path to class directories: 这个配置的是源代码编译后的字节码目录,也就是classes目录不是test-classes目录,如果有多个可以指定多个
  • Path to source directories: 这个配置的是源代码的目录,也就是src/main/java目录,如果有多个可以指定多个

3、执行Pipeline

①Junit执行日志

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.896 sec - in com.dxc.ddccloud.demo.DemoControllerTests
2019-06-02 10:57:23.072  INFO 18094 --- [       Thread-2] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@6497b078: startup date [Sun Jun 02 10:57:20 CST 2019]; root of context hierarchy

Results :

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0

②JaCoCo执行日志

[JaCoCo plugin] Collecting JaCoCo coverage data...
[JaCoCo plugin] **/**.exec;**/classes;**/src/main/java; locations are configured
[JaCoCo plugin] Number of found exec files for pattern **/**.exec: 0
[JaCoCo plugin] Saving matched execfiles:  
[JaCoCo plugin] Saving matched class directories for class-pattern: **/classes: 
[JaCoCo plugin]  - /app/idc/apps/jenkins/work/workspace/springboot-demo/target/classes 2 files
[JaCoCo plugin] Saving matched source directories for source-pattern: **/src/main/java: 
[JaCoCo plugin] Source Inclusions: **/*.java
[JaCoCo plugin] Source Exclusions: 
[JaCoCo plugin] - /app/idc/apps/jenkins/work/workspace/springboot-demo/src/main/java 2 files
[JaCoCo plugin] Loading inclusions files..
[JaCoCo plugin] inclusions: [**/classes]
[JaCoCo plugin] exclusions: [src/main/java]
[JaCoCo plugin] Thresholds: JacocoHealthReportThresholds [minClass=0, maxClass=0, minMethod=0, maxMethod=0, minLine=0, maxLine=0, minBranch=0, maxBranch=0, minInstruction=0, maxInstruction=0, minComplexity=0, maxComplexity=0]
[JaCoCo plugin] Publishing the results..
[JaCoCo plugin] Loading packages..
[JaCoCo plugin] Done.
[JaCoCo plugin] Overall coverage: class: 100, method: 100, line: 100, branch: 100, instruction: 100

③SonarQube执行日志

INFO: SonarQube Scanner 2.8
INFO: Java 1.8.0_11 Oracle Corporation (64-bit)
INFO: Linux 3.10.0-514.el7.x86_64 amd64
INFO: User cache: /root/.sonar/cache
INFO: Publish mode
INFO: Load global settings
INFO: Load global settings (done) | time=129ms
INFO: Server id: 5A0D13D1-AWNtCf_MnAfLtVTozgug
WARN: Property 'sonar.jdbc.url' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
WARN: Property 'sonar.jdbc.username' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
WARN: Property 'sonar.jdbc.password' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
INFO: User cache: /root/.sonar/cache
INFO: Load plugins index
INFO: Load plugins index (done) | time=54ms
INFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2.
INFO: SonarQube server 6.7.6
INFO: Default locale: "en_US", source code encoding: "UTF-8"
INFO: Process project properties
INFO: Load project repositories
INFO: Load project repositories (done) | time=46ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=35ms
INFO: Load active rules
INFO: Load active rules (done) | time=328ms
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=23ms
INFO: Project key: springboot-demo
INFO: -------------  Scan springboot-demo
INFO: Load server rules
INFO: Load server rules (done) | time=47ms
INFO: Base dir: /app/idc/apps/jenkins/work/workspace/springboot-demo
INFO: Working dir: /app/idc/apps/jenkins/work/workspace/springboot-demo/.sonar

4、效果展示

①Junit单元测试

②SonarQube展示

 

 

posted @ 2019-06-03 16:26  闫新江  阅读(2948)  评论(0编辑  收藏  举报