SpringBoot+Docker+Git+Jenkins實現簡易的持續整合和持續部署

鄭龍飛發表於2018-04-22

努力了這麼久,但凡有點兒天賦,也該有些成功的跡象了。

https://user-gold-cdn.xitu.io/2018/4/22/162ed08ece49e85b?w=2461&h=1500&f=jpeg&s=278007
https://user-gold-cdn.xitu.io/2018/4/22/162ed08ece49e85b?w=2461&h=1500&f=jpeg&s=278007

前言

本篇文章引導你使用Jenkins部署SpringBoot專案,同時使用DockerGit實現簡單的持續整合和持續部署。(專案地址:sso-merryyou

流程圖如下:

https://user-gold-cdn.xitu.io/2018/4/22/162ed08ecec13118?w=1103&h=591&f=png&s=140477
https://user-gold-cdn.xitu.io/2018/4/22/162ed08ecec13118?w=1103&h=591&f=png&s=140477

  1. push程式碼到Github觸發WebHook。(因網路原因,本篇使用gitee
  2. Jenkins從倉庫拉去程式碼
  3. mavem構建專案
  4. 程式碼靜態分析
  5. 單元測試
  6. build映象
  7. push映象到映象倉庫(本篇使用的映象倉庫為網易映象倉庫)
  8. 更新服務

Jenkins安裝

下載jenkins

jenkins.io/download/下載對應的jenkins

https://user-gold-cdn.xitu.io/2018/4/22/162ed08ece224c97?w=867&h=635&f=png&s=45316
https://user-gold-cdn.xitu.io/2018/4/22/162ed08ece224c97?w=867&h=635&f=png&s=45316

初始化密碼

訪問本地:http://localhost:8080輸入密碼

https://user-gold-cdn.xitu.io/2018/4/22/162ed08ecf073885?w=1128&h=525&f=png&s=83432
https://user-gold-cdn.xitu.io/2018/4/22/162ed08ecf073885?w=1128&h=525&f=png&s=83432

選擇外掛

進入使用者自定義外掛介面,選擇第二個(因為我們本次構建使用的為Pipelines

勾選與Pipelines相關的外掛

https://user-gold-cdn.xitu.io/2018/4/22/162ed08ecf7c5cfa?w=1237&h=774&f=png&s=128554
https://user-gold-cdn.xitu.io/2018/4/22/162ed08ecf7c5cfa?w=1237&h=774&f=png&s=128554

等待外掛安裝完成

https://user-gold-cdn.xitu.io/2018/4/22/162ed08ece9cde31?w=1206&h=726&f=png&s=109708
https://user-gold-cdn.xitu.io/2018/4/22/162ed08ece9cde31?w=1206&h=726&f=png&s=109708

配置使用者名稱和密碼

https://user-gold-cdn.xitu.io/2018/4/22/162ed08f1531b92e?w=944&h=630&f=png&s=55631
https://user-gold-cdn.xitu.io/2018/4/22/162ed08f1531b92e?w=944&h=630&f=png&s=55631

全域性配置

系統管理-》全域性工具配置 配置Git,JDK和Maven

https://user-gold-cdn.xitu.io/2018/4/22/162ed08f2c5d17a6?w=1583&h=533&f=png&s=19199
https://user-gold-cdn.xitu.io/2018/4/22/162ed08f2c5d17a6?w=1583&h=533&f=png&s=19199

安全配置

系統管理-》全域性安全配置

  • 勾選Allow anonymous read access
  • 取消防止跨站點請求偽造

https://user-gold-cdn.xitu.io/2018/4/22/162ed08f3bc5548d?w=852&h=465&f=png&s=19060
https://user-gold-cdn.xitu.io/2018/4/22/162ed08f3bc5548d?w=852&h=465&f=png&s=19060

新建任務

新建任務-》流水線

https://user-gold-cdn.xitu.io/2018/4/22/162ed08f811f33a1?w=1088&h=550&f=png&s=57453
https://user-gold-cdn.xitu.io/2018/4/22/162ed08f811f33a1?w=1088&h=550&f=png&s=57453

構建指令碼

勾選觸發遠端構建 (WebHooks觸發地址),填寫簡單的Pipeline script

https://user-gold-cdn.xitu.io/2018/4/22/162ed08f3d2e4f8f?w=1001&h=524&f=png&s=31334
https://user-gold-cdn.xitu.io/2018/4/22/162ed08f3d2e4f8f?w=1001&h=524&f=png&s=31334

#!groovy
pipeline{
	agent any

	stages {

		stage('test'){
			steps {
				echo "hello world"
			
			}
		}			
	}
}
複製程式碼

測試指令碼

立即構建

https://user-gold-cdn.xitu.io/2018/4/22/162ed08f820b3e6a?w=820&h=503&f=png&s=44503
https://user-gold-cdn.xitu.io/2018/4/22/162ed08f820b3e6a?w=820&h=503&f=png&s=44503

控制檯輸出

https://user-gold-cdn.xitu.io/2018/4/22/162ed08f8a7f8f45?w=839&h=463&f=png&s=41369
https://user-gold-cdn.xitu.io/2018/4/22/162ed08f8a7f8f45?w=839&h=463&f=png&s=41369

gitee整合WebHooks

新增SSH公匙

https://user-gold-cdn.xitu.io/2018/4/22/162ed08f9ae83e02?w=1013&h=512&f=png&s=34530
https://user-gold-cdn.xitu.io/2018/4/22/162ed08f9ae83e02?w=1013&h=512&f=png&s=34530

配置WebHooks

https://user-gold-cdn.xitu.io/2018/4/22/162ed08fc5f47587?w=990&h=391&f=png&s=32552
https://user-gold-cdn.xitu.io/2018/4/22/162ed08fc5f47587?w=990&h=391&f=png&s=32552

使用natapp實現內網穿透

https://user-gold-cdn.xitu.io/2018/4/22/162ed08ff16a83a9?w=628&h=256&f=png&s=9641
https://user-gold-cdn.xitu.io/2018/4/22/162ed08ff16a83a9?w=628&h=256&f=png&s=9641

修改指令碼

修改Pipeline script

https://user-gold-cdn.xitu.io/2018/4/22/162ed08fcfa320d8?w=947&h=416&f=png&s=21496
https://user-gold-cdn.xitu.io/2018/4/22/162ed08fcfa320d8?w=947&h=416&f=png&s=21496

#!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://user-gold-cdn.xitu.io/2018/4/22/162ed08ff98750c0?w=1334&h=181&f=png&s=17109
https://user-gold-cdn.xitu.io/2018/4/22/162ed08ff98750c0?w=1334&h=181&f=png&s=17109

訪問http://sso-taobao:8083/client1登入

https://user-gold-cdn.xitu.io/2018/4/22/162ed0901a263fde?w=714&h=274&f=png&s=36013
https://user-gold-cdn.xitu.io/2018/4/22/162ed0901a263fde?w=714&h=274&f=png&s=36013

修改內容效果如下:

https://user-gold-cdn.xitu.io/2018/4/22/162ed0901be331ad?w=1228&h=582&f=gif&s=2457081
https://user-gold-cdn.xitu.io/2018/4/22/162ed0901be331ad?w=1228&h=582&f=gif&s=2457081

更多效果圖

https://user-gold-cdn.xitu.io/2018/4/22/162ed0904b7afdd6?w=1626&h=878&f=png&s=50206
https://user-gold-cdn.xitu.io/2018/4/22/162ed0904b7afdd6?w=1626&h=878&f=png&s=50206

https://user-gold-cdn.xitu.io/2018/4/22/162ed090724c6893?w=1204&h=655&f=png&s=73595
https://user-gold-cdn.xitu.io/2018/4/22/162ed090724c6893?w=1204&h=655&f=png&s=73595

程式碼下載

推薦文章

  1. Java建立區塊鏈系列
  2. Spring Security原始碼分析系列
  3. Spring Data Jpa 系列
  4. 【譯】資料結構中關於樹的一切(java版)

https://user-gold-cdn.xitu.io/2018/4/17/162d1b4f7821b3ec?w=301&h=330&f=png&s=78572
https://user-gold-cdn.xitu.io/2018/4/17/162d1b4f7821b3ec?w=301&h=330&f=png&s=78572

???關注微信小程式java架構師歷程 上下班的路上無聊嗎?還在看小說、新聞嗎?不知道怎樣提高自己的技術嗎?來吧這裡有你需要的java架構文章,1.5w+的java工程師都在看,你還在等什麼?

相關文章