Jenkins的精髓是Pipeline(流水線技術),那爲何要用Pipeline呢?實現自動化構建,其中Pipeline可以將之前project中的配置信息以steps的方式放在一個腳本里,將本來獨立運行於單個或者多個節點的任務鏈接起來,實現單個任務難以完成的複雜流程,造成流水式發佈,構建步驟視圖化。node
簡單來講,Pipeline適用的場景更普遍,能勝任更復雜的發佈流程。舉個例子,job構建工做在master節點,自動化測試腳本在slave節點,而不一樣節點的執行處理經過Pipeline能夠。python
【Stage: 階段】,一個Pipeline能夠劃分爲若干個Stage,每一個Stage表明一組操做。注意,Stage是一個邏輯分組的概念,能夠跨多個Node。git
【Node: 節點】,一個Node就是一個Jenkins節點,或者是Master,或者是slave,是執行Step的具體運行期環境。github
【Step: 步驟】,Step是最基本的操做單元,小到建立一個目錄,大到構建一個Docker鏡像,由各種Jenkins Plugin提供。golang
二者都是pipeline代碼的持久實現,都可以使用pipeline內置的插件或者插件提供的steps,二者均可以利用共享庫擴展。docker
二者不一樣之處在於語法和靈活性。Declarative pipeline對用戶來講,語法更嚴格,有固定的組織結構,更容易生成代碼段,使其成爲用戶更理想的選擇。windows
可是Scripted pipeline更加靈活,由於Groovy自己只能對結構和語法進行限制,對於更復雜的pipeline來講,用戶能夠根據本身的業務進行靈活的實現和擴展。app
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: 'http://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''' } } } }
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: 'http://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 ''' }
node{ // 代碼檢出 stage('get Code') { git credentialsId: 'git-credentials-id', url: 'http://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('http://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') } }
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... ''' }
checkout scm: ([ $class: 'GitSCM', userRemoteConfigs: [[credentialsId: '******',url: ${project_url}]], branches: [[name: 'refs/tags/${project_tag}']] ])