SpringBoot+Docker+Git+Jenkins实现简易的持续集成和持续部署

·  阅读 2459

努力了这么久,但凡有点儿天赋,也该有些成功的迹象了。

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08ece49e85b~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08ece49e85b~tplv-t2oaga2asx-image.image

前言

本篇文章引导你使用Jenkins部署SpringBoot项目,同时使用DockerGit实现简单的持续集成和持续部署。(项目地址:sso-merryyou

流程图如下:

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08ecec13118~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08ecec13118~tplv-t2oaga2asx-image.image

  1. push代码到Github触发WebHook。(因网络原因,本篇使用gitee
  2. Jenkins从仓库拉去代码
  3. mavem构建项目
  4. 代码静态分析
  5. 单元测试
  6. build镜像
  7. push镜像到镜像仓库(本篇使用的镜像仓库为网易镜像仓库)
  8. 更新服务

Jenkins安装

下载jenkins

jenkins.io/download/下载对应的jenkins

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08ece224c97~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08ece224c97~tplv-t2oaga2asx-image.image

初始化密码

访问本地:http://localhost:8080输入密码

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08ecf073885~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08ecf073885~tplv-t2oaga2asx-image.image

选择插件

进入用户自定义插件界面,选择第二个(因为我们本次构建使用的为Pipelines

勾选与Pipelines相关的插件

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08ecf7c5cfa~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08ecf7c5cfa~tplv-t2oaga2asx-image.image

等待插件安装完成

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08ece9cde31~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08ece9cde31~tplv-t2oaga2asx-image.image

配置用户名和密码

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08f1531b92e~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08f1531b92e~tplv-t2oaga2asx-image.image

全局配置

系统管理-》全局工具配置 配置Git,JDK和Maven

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08f2c5d17a6~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08f2c5d17a6~tplv-t2oaga2asx-image.image

安全配置

系统管理-》全局安全配置

  • 勾选Allow anonymous read access
  • 取消防止跨站点请求伪造

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08f3bc5548d~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08f3bc5548d~tplv-t2oaga2asx-image.image

新建任务

新建任务-》流水线

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08f811f33a1~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08f811f33a1~tplv-t2oaga2asx-image.image

构建脚本

勾选触发远程构建 (WebHooks触发地址),填写简单的Pipeline script

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08f3d2e4f8f~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08f3d2e4f8f~tplv-t2oaga2asx-image.image

#!groovy
pipeline{
	agent any

	stages {

		stage('test'){
			steps {
				echo "hello world"
			
			}
		}			
	}
}
复制代码

测试脚本

立即构建

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08f820b3e6a~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08f820b3e6a~tplv-t2oaga2asx-image.image

控制台输出

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08f8a7f8f45~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08f8a7f8f45~tplv-t2oaga2asx-image.image

gitee集成WebHooks

添加SSH公匙

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08f9ae83e02~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08f9ae83e02~tplv-t2oaga2asx-image.image

配置WebHooks

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08fc5f47587~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08fc5f47587~tplv-t2oaga2asx-image.image

使用natapp实现内网穿透

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08ff16a83a9~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08ff16a83a9~tplv-t2oaga2asx-image.image

修改脚本

修改Pipeline script

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08fcfa320d8~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08fcfa320d8~tplv-t2oaga2asx-image.image

#!groovy
pipeline{
	agent any
	//定义仓库地址
	environment {
		REPOSITORY="https://gitee.com/merryyou/sso-merryyou.git"
	}

	stages {

		stage('获取代码'){
			steps {
				echo "start fetch code from git:${REPOSITORY}"
				//清空当前目录
				deleteDir()
				//拉去代码	
				git "${REPOSITORY}"
			}
		}

		stage('代码静态检查'){
			steps {
				//伪代码检查
				echo "start code check"
			}
		}		

		stage('编译+单元测试'){
			steps {
				echo "start compile"
				//切换目录
				dir('sso-client1') {
					//重新打包
					bat 'mvn -Dmaven.test.skip=true -U clean install'
				}
			}
		}

		stage('构建镜像'){
			steps {
				echo "start build image"
				dir('sso-client1') {
					//build镜像
					bat 'docker build -t hub.c.163.com/longfeizheng/sso-client1:1.0 .'
					//登录163云仓库
					bat 'docker login -u longfei_zheng@163.com -p password hub.c.163.com'
					//推送镜像到163仓库
					bat 'docker push hub.c.163.com/longfeizheng/sso-client1:1.0'
				}
			}
		}

		stage('启动服务'){
			steps {
				echo "start sso-merryyou"
				//重启服务
				bat 'docker-compose up -d --build'
			}
		}				

	}
}
复制代码

Pipeline的几个基本概念:

  • Stage: 阶段,一个Pipeline可以划分为若干个Stage,每个Stage代表一组操作。注意,Stage是一个逻辑分组的概念,可以跨多个Node。
  • Node: 节点,一个Node就是一个Jenkins节点,或者是Master,或者是Agent,是执行Step的具体运行期环境。
  • Step: 步骤,Step是最基本的操作单元,小到创建一个目录,大到构建一个Docker镜像,由各类Jenkins Plugin提供。

更多Pipeline语法参考:pipeline 语法详解

测试

docker-compose up -d 启动服务

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08ff98750c0~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed08ff98750c0~tplv-t2oaga2asx-image.image

访问http://sso-taobao:8083/client1登录

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed0901a263fde~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed0901a263fde~tplv-t2oaga2asx-image.image

修改内容效果如下:

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed0901be331ad~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed0901be331ad~tplv-t2oaga2asx-image.image

更多效果图

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed0904b7afdd6~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed0904b7afdd6~tplv-t2oaga2asx-image.image

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed090724c6893~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/22/162ed090724c6893~tplv-t2oaga2asx-image.image

代码下载

推荐文章

  1. Java创建区块链系列
  2. Spring Security源码分析系列
  3. Spring Data Jpa 系列
  4. 【译】数据结构中关于树的一切(java版)

https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/17/162d1b4f7821b3ec~tplv-t2oaga2asx-image.image
https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2018/4/17/162d1b4f7821b3ec~tplv-t2oaga2asx-image.image

🙂🙂🙂关注微信小程序java架构师历程 上下班的路上无聊吗?还在看小说、新闻吗?不知道怎样提高自己的技术吗?来吧这里有你需要的java架构文章,1.5w+的java工程师都在看,你还在等什么?

分类:
后端
分类:
后端