Devops关键工具及技术(三)—基于Pipeline的Git+Maven+Junit持续集成

在上一篇文章Devops关键工具及技术(二)—Jenkins2.0 Pipeline中,我们介绍了Jenkins2.0中Pipeline的一些基本用法,接下来我们将会利用Pipeline展开Git+Maven+Junit持续集成的运用。

首先我们需要一个持续集成的代码工程,笔者也建了一个基于Spring-boot的Java web工程。Github地址为:https://github.com/zbbkeepgoing/springboot-demo。

下面先介绍一下代码的结构以便我们持续集成以及后续更多功能的学习。

springboot-demo代码工程

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

  • 工程结构
[root@localhost Springboot-demo]# tree src
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   //单元测试类

15 directories, 5 files
  • 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;
	}
}

持续集成

我们还是使用Devops关键工具及技术(一)—Jenkins 容器化中的Jenkins来配置Pipeline。

  • 准备工作

如果代码库是私有的,需要鉴权才能pull下来,那我们需要在Jenkins中配置一个Credential。Username为Github的名字,Password为Personal Access Key。这样Jenkins的Git就可以直接拉Github的代码了,当然也可以通过ssh key的方式。目前我们用Personal Access Key。

Credential
Devops关键工具及技术(三)—基于Pipeline的Git+Maven+Junit持续集成_第1张图片

Username
在这里插入图片描述

Password
Devops关键工具及技术(三)—基于Pipeline的Git+Maven+Junit持续集成_第2张图片
Devops关键工具及技术(三)—基于Pipeline的Git+Maven+Junit持续集成_第3张图片
Devops关键工具及技术(三)—基于Pipeline的Git+Maven+Junit持续集成_第4张图片

  • Pipeline

Pipeline内容

内容也可以在Github中找到
https://github.com/zbbkeepgoing/pipeline-sample

pipeline {
    agent none     //不指定Agent,后续每个Stage再指定
    stages {
       stage('Preparation') {    //指定Stage名称Preparation
            agent { node { label 'master' } }    //指定Preparation的Stage执行的Agent为master(主节点)
            steps {
               checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'binbin', url: 'https://github.com/zbbkeepgoing/springboot-demo.git']]])   //Git的Checkout操作,拉去最新的代码
            }
        }
         
        stage('Build') {      //指定Stage名称Build
            agent { node { label 'master' } }  //指定Preparation的Stage执行的Agent为master(主节点)
            steps {
                dir(env.WORKSPACE){   //指定后续Step的操作在Jenkins Workspace的目录下
                  sh "mvn clean install"   //Maven构建操作
                  junit allowEmptyResults: true, keepLongStdio: true, testResults: 'target/surefire-reports/*.xml'  //Junit插件收集单元测试结果
                  sh "mv target/sample-0.0.1-SNAPSHOT.jar target/sample.jar"    //重命名文件,目前可以无需关心
                }
            }
        }
    }
}

新建Pipeline
Devops关键工具及技术(三)—基于Pipeline的Git+Maven+Junit持续集成_第5张图片
执行Pipeline

Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] stage
[Pipeline] { (Preparation)
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/CI
[Pipeline] {
[Pipeline] checkout
 > git rev-parse --is-inside-work-tree # timeout=10
......
Commit message: "Rename mian.yml to main.yml"
 > git rev-list --no-walk 76c01188ae3f7497796e2238bd91e28b7629cd12 # timeout=10
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/CI
[Pipeline] {
[Pipeline] dir
Running in /var/jenkins_home/workspace/CI
[Pipeline] {
[Pipeline] sh
[CI] Running shell script
+ mvn clean install
[INFO] Scanning for projects...
......
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 26.005 sec - in com.dxc.ddccloud.demo.DemoControllerTests
2018-10-04 15:12:55.909  INFO 4406 --- [       Thread-2] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@68e965f5: startup date [Thu Oct 04 15:12:33 UTC 2018]; root of context hierarchy

Results :

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

[INFO] 
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ sample ---
[INFO] Building jar: /var/jenkins_home/workspace/CI/target/sample-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.5.9.RELEASE:repackage (default) @ sample ---
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ sample ---
[INFO] Installing /var/jenkins_home/workspace/CI/target/sample-0.0.1-SNAPSHOT.jar to /var/jenkins_home/.m2/repository/com/dxc/ddccloud/sample/0.0.1-SNAPSHOT/sample-0.0.1-SNAPSHOT.jar
[INFO] Installing /var/jenkins_home/workspace/CI/pom.xml to /var/jenkins_home/.m2/repository/com/dxc/ddccloud/sample/0.0.1-SNAPSHOT/sample-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 47.582 s
[INFO] Finished at: 2018-10-04T15:12:59+00:00
[INFO] Final Memory: 29M/70M
[INFO] ------------------------------------------------------------------------
[Pipeline] junit
Recording test results
[Pipeline] sh
[CI] Running shell script
+ mv target/sample-0.0.1-SNAPSHOT.jar target/sample.jar
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
Finished: SUCCESS

Junit结果展示
Devops关键工具及技术(三)—基于Pipeline的Git+Maven+Junit持续集成_第6张图片
Devops关键工具及技术(三)—基于Pipeline的Git+Maven+Junit持续集成_第7张图片

以上就是基于Git+Maven+Junit的持续集成内容,后面文章将介绍基于SonarQube进行静态代码扫描。

你可能感兴趣的:(Devops)