- 聲明式流水線
- 腳本化流水線
pipeline { agent any stages { stage('Example') { steps { echo 'Hello World' } } } }
腳本化流水線
node { stage('Example') { if (env.BRANCH_NAME == 'master') { echo 'I only execute on the master branch' } else { echo 'I execute elsewhere' } } }
聲明性Pipeline
- Pipeline的頂層必須是塊,具體來說是:pipeline { }
- 沒有分號作爲語句分隔符。每個聲明必須在自己的一行
- 塊只能包含章節, 指令,步驟或賦值語句。
- 屬性引用語句被視爲無參數方法調用。所以例如,輸入被視爲input()
Sections
代理 agent
是否必填 | 是 |
參數 | 如下面所描述的 |
允許出現在 | 在頂級pipeline塊和每個stage塊中。 |
參數
- any - 在任何可用的代理上執行Pipeline或stage。例如:agent any
- none - 當在pipeline塊的頂層應用時,將不會爲整個Pipeline運行分配全局代理,並且每個stage部分將需要包含其自己的agent部分。例如:agent none
- label - 使用提供的標籤在Jenkins環境中可用的代理上執行Pipeline或stage性執行。例如:agent { label 'my-defined-label' }
- node - agent { node { label 'labelName' } }行爲和 agent { label 'labelName' }相同,但node允許其他選項(如customWorkspace)。
- docker - 用給定的容器執行Pipeline,或stage,將被動態地提供一個預先配置成基於Docker-based Pipelines的節點,或和label參數匹配的任選節點。 docker還可以接受一個args參數,可以直接將參數傳遞給docker run命令。例如:agent { docker 'maven:3-alpine' }或
agent { docker { image 'maven:3-alpine' label 'my-defined-label' args '-v /tmp:/tmp' } }
-
dockerfile -使用從Dockerfile源存儲庫中包含的容器構建容器來執行Pipeline或stage 。爲了使用此選項,Jenkinsfile必須從Multibranch Pipeline或Pipeline from SCM加載。通常Dockerfile在源碼庫的根路徑:agent { dockerfile true }。如果Dockerfile在其他目錄中建立,請使用以下dir選項:agent { dockerfile { dir 'someSubDir' } }。您可以使用additionalBuildArgs選項將其他參數傳遞給docker build ...命令,如agent { dockerfile { additionalBuildArgs '--build-arg foo=bar' } }。
常用選項
發佈 post
是否必填 | 否 |
參數 | 沒有 |
允許出現在 | 在頂級pipeline塊和每個stage塊中。 |
條件
Jenkinsfile (Declarative Pipeline) pipeline { agent any stages { stage('Example') { steps { echo 'Hello World' } } } post { always { echo 'I will always say Hello again!' } } }
-
通常,該post部分應放在Pipeline末端
-
post條件塊包含的步驟和steps中的步驟用法相同
stage stages
是否必填 | 是 |
參數 | 沒有 |
允許出現在 | 在pipeline內只有一次。 |
pipeline { agent any stages { stage('Example') { steps { echo 'Hello World' } } } }
步驟 steps
是否必填
|
是
|
參數
|
沒有
|
允許出現在
|
每個stage塊內
|
例如
Jenkinsfile (Declarative Pipeline) pipeline { agent any stages { stage('Example') { steps { echo 'Hello World' } } } }
指令 Directives
環境 environment
是否必填 | 否 |
參數 | 沒有 |
允許出現在 | 在pipeline塊內或stage內 |
例如:
Jenkinsfile (Declarative Pipeline) pipeline { agent any environment { CC = 'clang' } stages { stage('Example') { environment { AN_ACCESS_KEY = credentials('my-prefined-secret-text') } steps { sh 'printenv' } } } }
- environment頂級pipeline塊中使用的指令將適用於Pipeline中的所有步驟
- 在一個stage中定義的environment指令只將給定的環境變量應用於該stage中的步驟
- environment具有一個幫助方法credentials(),可用於在Jenkins環境中通過其標識符訪問預定義的憑據
選項 options
是否必填 | 否 |
參數 | 沒有 |
允許出現在 | 在pipeline塊內,只有一次 |
可用選項
-
buildDiscarder持久化工件和控制檯輸出,用於保存Pipeline最近幾次運行的數據。例如:options { buildDiscarder(logRotator(numToKeepStr: '1')) }
-
checkoutToSubdirectory在工作區的子目錄中執行源代碼檢出。例如:options { checkoutToSubdirectory('foo') }
-
disableConcurrentBuilds不允許並行執行Pipeline。可用於防止同時訪問共享資源等。例如:options { disableConcurrentBuilds() }
-
preserveStashes保留已完成構建的存儲,用於stage重新啓動。例如:options { preserveStashes() }保存最近完成的構建中的stash,或者options { preserveStashes(5) }保留最近五個完成的構建中的stash。
-
quietPeriod設置管道的靜默期(以秒爲單位),覆蓋全局默認值。例如:options { quietPeriod(30) }
-
retry如果失敗,請按指定的次數重試整個管道。例如:options { retry(3) }
-
skipDefaultCheckout在agent指令中默認跳過源代碼檢出。例如:options { skipDefaultCheckout() }
-
skipStagesAfterUnstable一旦構建狀態進入了“不穩定”狀態,就跳過stage。例如:options { skipStagesAfterUnstable() }
-
timeout設置Pipeline運行的超時時間,之後Jenkins應該中止Pipeline。例如:options { timeout(time: 1, unit: 'HOURS') }
-
timestamps當執行時,預處理由Pipeline生成的所有控制檯輸出運行時間。例如:options { timestamps() }
例如: 指定一個小時的全局執行超時,之後Jenkins將中止Pipeline運行
Jenkinsfile (Declarative Pipeline) pipeline { agent any options { timeout(time: 1, unit: 'HOURS') } stages { stage('Example') { steps { echo 'Hello World' } } } }
-
skipDefaultCheckout默認情況下,在agent指令中跳過檢查源代碼管理中的代碼。例如:options { skipDefaultCheckout() }
-
timeout設置此stage的超時時間,之後Jenkins應該中止該stage。例如:options { timeout(time: 1, unit: 'HOURS') }
-
retry如果失敗,請重試此stage指定次數。例如:options { retry(3) }
-
timestamps當執行時,預處理由Pipeline生成的所有控制檯輸出運行時間。例如:options { timestamps() }
-
例如:爲stageExample指定一小時的執行超時,之後Jenkins將中止Pipeline運行。
pipeline { agent any stages { stage('Example') { options { timeout(time: 1, unit: 'HOURS') } steps { echo 'Hello World' } } } }
參數 parameters
是否必填 | 否 |
參數 | 沒有 |
允許出現在 | 在pipeline塊內,只有一次 |
可用參數
-
string字符串類型的參數,例如: parameters { string(name: 'DEPLOY_ENV', defaultValue: 'staging', description: '') }
-
文本一個text參數,可以包含多行,例如: parameters { text(name: 'DEPLOY_TEXT', defaultValue: 'One\nTwo\nThree\n', description: '') }
-
booleanParam一個布爾參數,例如: parameters { booleanParam(name: 'DEBUG_BUILD', defaultValue: true, description: '') }
-
choice選擇參數,例如: parameters { choice(name: 'CHOICES', choices: ['one', 'two', 'three'], description: '') }
-
file一個文件參數,指定用戶在計劃構建時要提交的文件,例如: parameters { file(name: 'FILE', description: 'Some file to upload') }
-
password密碼參數,例如: parameters { password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'A secret password') }
Jenkinsfile (Declarative Pipeline) pipeline { agent any parameters { string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?') text(name: 'BIOGRAPHY', defaultValue: '', description: 'Enter some information about the person') booleanParam(name: 'TOGGLE', defaultValue: true, description: 'Toggle this value') choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something') password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'Enter a password') file(name: "FILE", description: "Choose a file to upload") } stages { stage('Example') { steps { echo "Hello ${params.PERSON}" echo "Biography: ${params.BIOGRAPHY}" echo "Toggle: ${params.TOGGLE}" echo "Choice: ${params.CHOICE}" echo "Password: ${params.PASSWORD}" } } } }
觸發器 triggers
需要
|
沒有
|
參數
|
沒有
|
允許
|
只有一次,在pipeline塊內。
|
-
cron接受一個cron風格的字符串來定義Pipeline應重新觸發的常規間隔,例如: triggers { cron('H 4/* 0 0 1-5') }
-
pollSCM接受一個cron風格的字符串來定義Jenkins應該檢查新的源更改的常規間隔。如果存在新的更改,則Pipeline將被重新觸發。例如:triggers { pollSCM('H 4/* 0 0 1-5') }
Jenkinsfile (Declarative Pipeline) pipeline { agent any triggers { cron('H 4/* 0 0 1-5') } stages { stage('Example') { steps { echo 'Hello World' } } } }
Jenkins cron 語法
分鐘 | 小時 | DOM | 月 | DOW |
一小時內的分鐘數(0-59) | 一天中的小時(0-23) | 每月的某一天(1-31) | 月(1-12) | 星期幾(0-7),其中0和7是星期日。 |
- * 指定所有有效值
- M-N 指定一系列值
- M-N/X或者按照指定範圍或整個有效範圍的*/X間隔步長X
- A,B,…,Z 枚舉多個值
階段 stage
是否必填 | 至少一個 |
參數 | 一個必填參數,一個用於stage名稱的字符串 |
允許出現在 | 在stages內 |
例如:Jenkinsfile (Declarative Pipeline)
pipeline { agent any stages { stage('Example') { steps { echo 'Hello World' } } } }
工具 tools
是否必填
|
否
|
參數
|
沒有
|
允許出現在
|
在pipeline塊或stage塊內
|
支持的工具
- maven
- jdk
- gradle
Jenkinsfile (Declarative Pipeline) pipeline { agent any tools { maven 'apache-maven-3.0.1' } stages { stage('Example') { steps { sh 'mvn --version' } } } }
輸入 input
配置選項
-
message必填。這將在用戶提交input時呈現給用戶。
-
id這是一個可選的標識符input。默認爲stage名稱。
-
okinput表單上“ok”按鈕的可選文本。
-
submitter允許提交此input選項的用戶或外部組名列表,用逗號分隔。默認允許任何用戶。
-
submitterParameter要使用submitter名稱設置的環境變量的名稱,可選(如果存在)。
-
parameters用於提示提供的可選參數列表。有關更多信息,請參閱參數
Jenkinsfile (Declarative Pipeline) pipeline { agent any stages { stage('Example') { input { message "Should we continue?" ok "Yes, we should." submitter "alice,bob" parameters { string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?') } } steps { echo "Hello, ${PERSON}, nice to meet you." } } } }
條件 when
是否必填 | 否 |
參數 | 沒有 |
允許出現在 | 在stage指令內 |
內置條件
-
branch當正在構建的分支與給出的分支模式匹配時執行stage,例如:when { branch 'master' }。請注意,這僅適用於多分支Pipeline。
-
environment當指定的環境變量設置爲給定值時執行stage,例如: when { environment name: 'DEPLOY_TO', value: 'production' }
-
expression當指定的Groovy表達式求值爲true時執行stage,例如: when { expression { return params.DEBUG_BUILD } }
-
not當嵌套條件爲false時執行stage。必須包含一個條件。例如:when { not { branch 'master' } }
-
allOf當所有嵌套條件都爲真時,執行舞臺。必須至少包含一個條件。例如:when { allOf { branch 'master'; environment name: 'DEPLOY_TO', value: 'production' } }
-
anyOf當至少一個嵌套條件爲真時執行舞臺。必須至少包含一個條件。例如:when { anyOf { branch 'master'; branch 'staging' } }
Jenkinsfile (Declarative Pipeline) pipeline { agent any stages { stage('Example Build') { steps { echo 'Hello World' } } stage('Example Deploy') { when { branch 'production' } steps { echo 'Deploying' } } } } Jenkinsfile (Declarative Pipeline) pipeline { agent any stages { stage('Example Build') { steps { echo 'Hello World' } } stage('Example Deploy') { when { branch 'production' environment name: 'DEPLOY_TO', value: 'production' } steps { echo 'Deploying' } } } } Jenkinsfile (Declarative Pipeline) pipeline { agent any stages { stage('Example Build') { steps { echo 'Hello World' } } stage('Example Deploy') { when { allOf { branch 'production' environment name: 'DEPLOY_TO', value: 'production' } } steps { echo 'Deploying' } } } } Jenkinsfile (Declarative Pipeline) pipeline { agent any stages { stage('Example Build') { steps { echo 'Hello World' } } stage('Example Deploy') { when { branch 'production' anyOf { environment name: 'DEPLOY_TO', value: 'production' environment name: 'DEPLOY_TO', value: 'staging' } } steps { echo 'Deploying' } } } } Jenkinsfile (Declarative Pipeline) pipeline { agent any stages { stage('Example Build') { steps { echo 'Hello World' } } stage('Example Deploy') { when { expression { BRANCH_NAME ==~ /(production|staging)/ } anyOf { environment name: 'DEPLOY_TO', value: 'production' environment name: 'DEPLOY_TO', value: 'staging' } } steps { echo 'Deploying' } } } }
順序執行的stage(Sequential Stages)
pipeline { agent none stages { stage('Non-Sequential Stage') { agent { label 'for-non-sequential' } steps { echo "On Non-Sequential Stage" } } stage('Sequential') { agent { label 'for-sequential' } environment { FOR_SEQUENTIAL = "some-value" } stages { stage('In Sequential 1') { steps { echo "In Sequential 1" } } stage('In Sequential 2') { steps { echo "In Sequential 2" } } } } } }
並行的stage(Parallel)
pipeline { agent any stages { stage('Non-Parallel Stage') { steps { echo 'This stage will be executed first.' } } stage('Parallel Stage') { when { branch 'master' } failFast true parallel { stage('Branch A') { agent { label "for-branch-a" } steps { echo "On Branch A" } } stage('Branch B') { agent { label "for-branch-b" } steps { echo "On Branch B" } } stage('Branch C') { agent { label "for-branch-c" } stages { stage('Nested 1') { steps { echo "In stage Nested 1 within Branch C" } } stage('Nested 2') { steps { echo "In stage Nested 2 within Branch C" } } } } } } } }
步驟 Steps
腳本 script
Jenkinsfile (Declarative Pipeline) pipeline { agent any stages { stage('Example') { steps { echo 'Hello World' script { def browsers = ['chrome', 'firefox'] for (int i = 0; i < browsers.size(); ++i) { echo "Testing the ${browsers[i]} browser" } } } } } }
Scripted Pipeline
Flow Control
Jenkinsfile (Scripted Pipeline) node { stage('Example') { if (env.BRANCH_NAME == 'master') { echo 'I only execute on the master branch' } else { echo 'I execute elsewhere' } } }
Jenkinsfile (Scripted Pipeline) node { stage('Example') { try { sh 'exit 1' } catch (exc) { echo 'Something failed, I should sound the klaxons!' throw } } }
Steps
與Groovy的區別
語法比較
pipeline { agent none stages { stage('11111111') { steps { echo "11111111" } } stage('22222') { stages { stage('222222aaaaa1') { steps { echo "222222aaaaa1" } } stage('In 222222aaaa2') { steps { echo "222222aaaa2" } } } } stage('33333') { steps { echo "33333" } } } }
pipeline { agent any stages { stage('Non-Parallel Stage') { steps { echo 'This stage will be executed first.' } } stage('Parallel Stage') { failFast true parallel { stage('並行一') { steps { echo "並行一" } } stage('並行二') { steps { echo "並行二" } } stage('並行三') { stages { stage('Nested 1') { steps { echo "In stage Nested 1 within Branch C" } } stage('Nested 2') { steps { echo "In stage Nested 2 within Branch C" } } } } } } } } pipeline { agent any stages { stage('Non-Parallel Stage') { steps { echo 'This stage will be executed first.' } } stage('Parallel Stage') { failFast true parallel { stage('並行一') { steps { echo "並行一" } } stage('並行二') { steps { echo "並行二" } } stage('並行三') { stages { stage('Nested 1') { steps { echo "In stage Nested 1 within Branch C" } } stage('Nested 2') { steps { echo "In stage Nested 2 within Branch C" } } } } } } } }
pipeline { agent any stages { stage('flow control') { steps { script { if ( 10 == 10) { println "pass" }else { println "failed" } } } } } }