🏆(不要錯過!)【CI/CD技術專題】「Jenkins實戰系列」(4)jenkins+pipeline構建自動化部署
前提引言
-
Jenkins的精髓是Pipeline(流水線技術),那為什麼要用Pipeline呢?實現自動化構建,其中Pipeline能夠將以前project中的配置資訊以steps的方式放在一個腳本里,將原本獨立運行於單個或者多個節點的任務連接起來,實現單個任務難以完成的複雜流程,形成流水式發布,構建步驟視圖化。
-
簡單來說,Pipeline適用的場景更廣泛,能勝任更複雜的發布流程。舉個例子,job構建工作在master節點,自動化測試腳本在slave節點,而不同節點的執行處理通過Pipeline可以。
基本概念
-
【Stage: 階段】,一個Pipeline可以劃分為若干個Stage,每個Stage代表一組操作。注意,Stage是一個邏輯分組的概念,可以跨多個Node。
-
【Node: 節點】,一個Node就是一個Jenkins節點,或者是Master,或者是slave,是執行Step的具體運行期環境。
-
【Step: 步驟】,Step是最基本的操作單元,小到創建一個目錄,大到構建一個Docker鏡像,由各類Jenkins Plugin提供。
Pipeline配置
新建一個「流水線」的job

配置Pipeline腳本

Pipeline也支援Poll SCM

pipline流水線流程

PIpeline語法
- Pipeline支援兩種語法:Declarative Pipeline(在Pipeline 2.5中引入,結構化方式) 和 Scripted Pipeline,兩者都支援建立連續輸送的Pipeline。
共同點
兩者都是pipeline程式碼的持久實現,都能夠使用pipeline內置的插件或者插件提供的steps,兩者都可以利用共享庫擴展。
不同點:
-
兩者不同之處在於語法和靈活性。Declarative pipeline對用戶來說,語法更嚴格,有固定的組織結構,更容易生成程式碼段,使其成為用戶更理想的選擇。
-
但是Scripted pipeline更加靈活,因為Groovy本身只能對結構和語法進行限制,對於更複雜的pipeline來說,用戶可以根據自己的業務進行靈活的實現和擴展。
下面舉例介紹兩種語法的使用
Declarative Pipeline
windows環境腳本案例
pipeline {
agent any //在可用的節點運行
stages{
stage ('Prepare'){
steps{
//清空發布目錄
bat '''if exist D:\\publish\\LoginServiceCore (rd/s/q D:\\publish\\LoginServiceCore)
if exist C:\\Users\\Administrator\\.nuget (rd/s/q C:\\Users\\Administrator\\.nuget) exit'''
}
}
//拉取git程式碼倉庫
stage ('Checkout'){
steps{
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'c6d98bbd-5cfb-4e26-aa56-f70b054b350d',
url: '//xxx/xxx/xxx']]])
}
}
//構建
stage ('Build'){
steps{
bat '''cd "D:\\Program Files (x86)\\Jenkins\\workspace\\LoginServiceCore\\LoginApi.Hosting.Web"
dotnet restore
dotnet build
dotnet publish --configuration Release --output D:\\publish\\LoginServiceCore'''
}
}
//部署
stage ('Deploy'){
steps{
bat '''cd D:\\PipelineScript\\LoginServiceCore
python LoginServiceCore.py'''
}
}
//自動化測試(python程式碼實現)
stage ('Test'){
steps{
bat'''cd D:\\PipelineScript\\LoginServiceCore
python LoginServiceCoreApitest.py'''
}
}
}
}
Scripted Pipeline
node('master') { //master節點運行,以下stage也可指定節點
stage 'Prepare' //清空發布目錄
bat '''if exist D:\\publish\\LoginServiceCore (rd/s/q D:\\publish\\LoginServiceCore)
if exist C:\\Users\\Administrator\\.nuget (rd/s/q C:\\Users\\Administrator\\.nuget)
exit'''
//拉取git程式碼倉庫
stage 'Checkout'
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'c6d98bbd-5cfb-4e26-aa56-f70b054b350d',
url: '//xxx/xxx/xxx']]])
//構建
stage 'Build'
bat '''cd "D:\\Program Files (x86)\\Jenkins\\workspace\\LoginServiceCore\\LoginApi.Hosting.Web"
dotnet restore
dotnet build
dotnet publish --configuration Release --output D:\\publish\\LoginServiceCore'''
//部署
stage 'Deploy'
bat '''
cd D:\\PipelineScript\\LoginServiceCore
python LoginServiceCore.py
'''
//自動化測試(python程式碼實現)
stage 'Test'
bat'''
cd D:\\PipelineScript\\LoginServiceCore
python LoginServiceCoreApitest.py
'''
}
Pipeline Docker腳本示例
node{
// 程式碼檢出
stage('get Code') {
git credentialsId: 'git-credentials-id', url: '//192.168.19.250/libo/test.git'
}
// 鏡像中進行單元測試
stage('unit testing'){
// 啟動golnag:1.7並在golang內編譯程式碼
docker.image('golang:1.7').inside {
sh './script/unittest.sh'
}
}
// 鏡像中程式碼構建
stage('Build'){
def confFilePath = 'conf/app.conf'
def config = readFile confFilePath
writeFile file: confFilePath, text: config
// 啟動golnag:1.7並在golang內編譯程式碼
docker.image('golang:1.7').inside {
sh './script/build.sh'
}
}
// 編譯鏡像並push到倉庫
def imagesName = '192.168.18.250:5002/ufleet/uflow:v0.9.1.${BUILD_NUMBER}'
stage('Image Build And Push'){
docker.withRegistry('//192.168.18.250:5002', 'registry-credentials-id') {
docker.build(imagesName).push()
}
}
// 啟動剛運行的容器
stage('deploy iamegs'){
// 需要刪除舊版本的容器,否則會導致埠佔用而無法啟動。
try{
sh 'docker rm -f cicdDemo'
}catch(e){
// err message
}
docker.image(imagesName).run('-p 9091:80 --name cicdDemo')
}
}
git操作認證
withCredentials([usernamePassword(credentialsId: '<credentials-id>', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
sh '''
printf "machine github.com\nlogin $GIT_USERNAME\n password $GIT_PASSWORD" >> ~/.netrc
// continue script as necessary working with git repo...
'''
}
git拉取程式碼
checkout scm: ([
$class: 'GitSCM',
userRemoteConfigs: [[credentialsId: '******',url: ${project_url}]],
branches: [[name: 'refs/tags/${project_tag}']]
])

