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

1、準備工做

一、準備一個持續集成的代碼工程

工程下載地址:html

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

二、springboot-demo代碼工程介紹

整個Web工程有一個Index頁面,上面有兩個按鈕,分別對應兩個接口,其中一個接口直接返回信息,另一個接口則是內存中請求一次延時1s,最大延時爲10s。而對應Index會有一個接口,因此Web工程一共有3個接口。延時接口主要是爲了後續性能測試java

工程結構

└── 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;
    }
}

三、下載代碼工程放到公司內部私有倉庫GitLab

①安裝Junit插件

 ②安裝jacoco插件

③安裝SonarQube Scanne

說明:上述插件具體怎麼配置,請自行爬樓參考之前的博客node

2、新建Pipeline工程springboot-demo

 一、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 \
        }
    }
}

二、Jacoco配置

這裏須要注意的配置:git

  • Path to exec files: **/jacoco.exec 可執行文件路徑
  • Path to class directories: 這個配置的是源代碼編譯後的字節碼目錄,也就是classes目錄不是test-classes目錄,若是有多個能夠指定多個
  • Path to source directories: 這個配置的是源代碼的目錄,也就是src/main/java目錄,若是有多個能夠指定多個

三、執行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

四、效果展現

①Junit單元測試

②SonarQube展現

 

相關文章
相關標籤/搜索