持續(xù)交付-Jenkinsfile 語法
實(shí)現(xiàn) Pipeline 功能的腳本語言叫做 Jenkinsfile,由 Groovy 語言實(shí)現(xiàn)。Jenkinsfile 一般是放在項(xiàng)目根目錄,隨項(xiàng)目一起受源代碼管理軟件控制,無需像創(chuàng)建"自由風(fēng)格"項(xiàng)目一樣,每次可能需要拷貝很多設(shè)置到新項(xiàng)目,提供了一些直接的好處:
Pipeline 上的代碼審查/迭代
Pipeline 的審計(jì)跟蹤
Pipeline 的唯一真實(shí)來源,可以由項(xiàng)目的多個(gè)成員查看和編輯
Pipeline 支持:Declarative(在 Pipeline 2.5 中引入)和 Scripted Pipeline 兩種格式。兩者都支持建立 Pipeline,兩者都可以用于在 Web UI 中定義一個(gè)流水線 Jenkinsfile,將 Jenkinsfile 文件創(chuàng)建并檢查到源代碼控制庫中通常被認(rèn)為是最佳做法。
Declared Pipeline
Declared Pipeline樹
Declared Pipeline 必須包含在固定格式 Pipeline {} 塊內(nèi),每個(gè)聲明語句必須獨(dú)立一行,行尾無需使用分號(hào)。塊( blocks{} )只能包含章節(jié)(Sections),指令(Directives),步驟(Steps)或賦值語句。
塊 blocks{}
由大括號(hào)括起來的語句,如 Pipeline{},Section{},parameters{},script{}
章節(jié)(Sections)
章節(jié)中通常包含一個(gè)或多個(gè)指令或步驟。如 agent 、post、stages、steps
指令(Directives)
environment、options、parameters、triggers(觸發(fā))、stage、tools、when
節(jié)點(diǎn)(agent)
必須存在,agent 必須在 Pipeline 塊內(nèi)的頂層定義,但 stage 內(nèi)是否使用是可選的
參數(shù):any/none/label/node/docker/dockerfile
常用選項(xiàng) label/cuetomWorkspace/reuseNode
示例:
agent { label 'my-label' }
agent {
?
??node {
? ?
?? ?label 'my-label'
? ?
?? ?customWorkspace '/some/other/path'
?
?}
}?
?agent {?
?? ?docker {
? ?
?? ?image 'nginx:1.12.2'
? ??
? ?label 'my-label'
? ??
? ?args '-v /tmp:/tmp'
??
?}
?}
構(gòu)建后(post)
不是必須,用于 Pipeline 的最外層或者 stage{} 中,主要用于表達(dá) Jenkins 完成構(gòu)建動(dòng)作之后需要做的事情。
示例:
pipeline {?
? ?agent any
? ?
stages {
? ??
? ?stage('Example'){
? ?
?? ? ? ?steps {
? ??
? ? ? ? ? ?echo 'Hello world'
??
? ? ? ? ?}
? ?
?? ?}
?
??}
??
?post {
? ?
?? ?always {
? ? ?
?? ? ?echo 'say goodbay'
? ?
?? ?}
?
??}?
}
階段集(stages)
必須存在,包括順序執(zhí)行的一個(gè)或多個(gè) stage 命令,在 Pipeline 內(nèi)僅能使用一次,通常位于 agent/options 后面。
步驟(steps)
必須存在,steps 位于 stage 指令塊內(nèi)部,包括一個(gè)或多個(gè) step。僅有一個(gè) step 的情況下可以忽略關(guān)鍵字 step 及其{}。
環(huán)境(environment)
不是必須,environment 定義了一組全局的環(huán)境變量鍵值對(duì),存在于 pipeline {} 或者 stage 指令內(nèi)。執(zhí)行特殊方法 credentials()可以獲取 Jenkins 中預(yù)定義的憑證明文內(nèi)容。
示例:
environment {CC='clang'}?
environment {AN_ACCESS_KEY = credentials('my-prefined-secret-text')}?
steps {sh 'printenv'}
選項(xiàng)(options)
不是必須,預(yù)定義 Pipeline 專有的配置信息,僅可定義一次
示例:
pipeline {
? ?
agent any
?
?options{
? ??
? ?timeout(time:1,unit: 'HOURS')
?
??}
}
參數(shù)(parameters)
不是必須, 定義參數(shù)化構(gòu)建的參數(shù)可選參數(shù), 參數(shù)類型 booleanParam,choice,file,text,password,run,string
示例:
parameters {
? ?
? ?string(name: 'PERSON', defaultValue: 'Jenkins', description: '輸入的文本參數(shù)')
?
?? ? ?choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')
?}
觸發(fā)器(rtiggers)
不是必須,定義 Pipeline 被自動(dòng)觸發(fā)的方式選項(xiàng) cron、pollSCM、upstream
示例:
triggers {
?
?? ? cron('0 8 * * 1-5')?
}
Script Pipeline
Script Pipeline語句樹
一個(gè) Script Pipeline 可以劃分成若干個(gè) Stage,每個(gè) Stage 代表一組操作,例如 Build,Test;Node 代表 Jenkins 節(jié)點(diǎn),比如 Master, Slave 這樣的節(jié)點(diǎn);Step 是最基本的操作單元,在對(duì)應(yīng) Node 節(jié)點(diǎn)上執(zhí)行的動(dòng)作語句直接寫在 node {} 中。

流程控制語句
如同傳統(tǒng)的腳本語言一樣,Script Pipeline 是從上至下順序執(zhí)行,可以使用 Groovy 表達(dá)式進(jìn)行流程控制,如 if/else 語句通過邏輯條件判斷來對(duì)流程進(jìn)行控制:
node {
??
?stage('Example') {
??
? ? ?if (env.BRANCH_NAME == 'master') {
? ?
?? ? ? ?echo 'I only execute on the master branch'
?
?? ? ?} else {
? ? ??
? ? ?echo 'I execute elsewhere'
? ?
?? ?}
?
??}
}
異常處理語句
Script Pipeline 腳本流程控制的另一種方式是異常處理機(jī)制。當(dāng)任何一個(gè)步驟因各種原因而出現(xiàn)異常時(shí),都必須在代碼中使用 try/catch/finally 語句塊進(jìn)行異常捕獲,并通過預(yù)先設(shè)定代碼進(jìn)行處理,保證腳本能夠順利執(zhí)行:
stage('Error Handling') {
?
?node{
??
? ? ?echo "This is test demo for the error handling"
??
? ? ?try {
? ? ? ?
?? ?echo "This is in the try block."
? ? ? ?
? ?sh 'exit 1'
? ? ?
??}catch (exc) {
? ? ?
?? ? ?echo "Something failed, I'm in the catch block."
? ?
?? ?}finally {
?
?? ? ? ? ?echo "Finally, I'm in the finally block."
??
? ? ?}
?
??}?
}
Jenkinsfile 中調(diào)用plugin功能
在 Jenkinsfile 中除了可以使用常規(guī)的邏輯、流程控制,還可以調(diào)用 Jenkins 的插件功能。下面用如下實(shí)例進(jìn)行說明。
Email Extension插件
Email Extension 是 Jenkins 中的一個(gè)外部插件,用來發(fā)送郵件,從 Jenkins 的 Plugin Manager 中進(jìn)行安裝。在 Pipeline 中通過代碼調(diào)用可以觸發(fā)該插件的運(yùn)行,實(shí)現(xiàn)發(fā)送郵件的功能。
基礎(chǔ)配置
在安裝好 Email Extension 插件之后,首先需要選擇一個(gè)準(zhǔn)備用來發(fā)送 Jenkins 通知郵件的郵箱,發(fā)件郵箱的具體參數(shù)要提前知曉(通常參數(shù)可以從郵箱的配置參數(shù)信息頁面上查到);之后要在 Manage Jenkins → Configure System 中將對(duì)應(yīng)參數(shù)填寫該插件的對(duì)應(yīng)配置項(xiàng)中,必須配置的參數(shù)如下:
SMTP server:smtp 服務(wù)地址
SMTP port:smtp 端口號(hào)
Use SMTP Authentication:?jiǎn)⒂?smtp 安全校驗(yàn)
User Name:發(fā)件人用戶名
Password:發(fā)件人密碼
Default Recipients:默認(rèn)收件人
其他的參數(shù)可以根據(jù)自己的需要進(jìn)行配置,Email Extension 配置參考截圖如下:

Pipeline代碼
pipeline{
?
?agent {
? ? ?
??label 'master'
??
?}
? ?stages{
? ? ?
??stage('發(fā)送郵件測(cè)試') {
?
?? ? ? ? ?steps{
? ? ? ? ??
? ? ?echo 'Test Email'
? ?
?? ? ? ?}
? ??
? ?}
? ?
}
? ?
post {
? ?
?? ?always {
? ? ??
? ? ?emailext body: '$DEFAULT_CONTENT', recipientProviders: [[$class: 'RequesterRecipientProvider']], subject: '$DEFAULT_SUBJECT'
??
? ? ?}
??
?}?
}
Declared Pipeline 的入門學(xué)習(xí)難度相對(duì)不高,這種類似我們?cè)谧鲎詣?dòng)化測(cè)試時(shí)所接觸的關(guān)鍵字驅(qū)動(dòng)模式,只要理解其定義好的關(guān)鍵詞,按要求填充數(shù)據(jù)即可。
雖然這種方式入門容易,但靈活性欠缺。相比之下 script Pipeline 的好處就是靈活,好封裝,易于大規(guī)模使用,但需要有一定的編程功底。