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

前言
本篇文章引導你使用Jenkins
部署SpringBoot
專案,同時使用Docker
和Git
實現簡單的持續整合和持續部署。(專案地址:sso-merryyou)
流程圖如下:

push
程式碼到Github
觸發WebHook
。(因網路原因,本篇使用gitee)Jenkins
從倉庫拉去程式碼mavem
構建專案- 程式碼靜態分析
- 單元測試
build
映象push
映象到映象倉庫(本篇使用的映象倉庫為網易映象倉庫)- 更新服務
Jenkins安裝
下載jenkins
從jenkins.io/download/下載對應的jenkins

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

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

等待外掛安裝完成

配置使用者名稱和密碼

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

安全配置
系統管理-》全域性安全配置
- 勾選Allow anonymous read access
- 取消防止跨站點請求偽造

新建任務
新建任務-》流水線

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

#!groovy
pipeline{
agent any
stages {
stage('test'){
steps {
echo "hello world"
}
}
}
}
複製程式碼
測試指令碼
立即構建

控制檯輸出

gitee整合WebHooks
新增SSH公匙

配置WebHooks

使用natapp實現內網穿透

修改指令碼
修改Pipeline script

#!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
啟動服務

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

修改內容效果如下:

更多效果圖


程式碼下載
- github:github.com/longfeizhen…
- gitee:gitee.com/merryyou/ss…
推薦文章

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