Updated: 2019-08-15git
若是但願經過Webhook觸發multibranch pipeline項目須要安裝 multibranch-scan-webhook-trigger-plugin 插件 安裝完以後,配置界面多出一個Scan by webhook選項github
實際中一個項目的代碼倉庫可能會有不少分支,好比develop,master等。Jenkins 支持建立多分支pipeline的任務。web
新建 "Item" 直接選擇 "Multibranch Pipeline" 便可 Tab中有不少配置項,好比 General,Branch Sources,Build Configuration等正則表達式
配置完成後,Jenkins就會自動執行首次構建,首先掃描全部的分支,若是根據配置的路徑去找Jenkinsfile,找到後就當即執行。express
根據發現的分支數量,好比這裏3個就自動建立了3個pipeline項目,點進去後能夠像pipeline任務同樣進行詳細配置。bash
咱們須要判斷針對不一樣分支作不一樣事情,使用 if else 比較low,不夠優雅ui
stage("deploy to test") {
steps {
script {
if (env.GIT_NAME == 'testing') {
echo 'deploy to test'
}
}
}
}
複製代碼
能夠使用when指令this
stage("deploy to test") {
when {
branch 'testing'
steps {
echo 'deploy to test'
}
}
}
stage("deploy to prod") {
when {
branch 'production'
steps {
echo 'deploy to prod'
}
}
}
複製代碼
when指令容許pipeline根據給定的條件,決定是否執行階段內的步驟。when指令必須至少包含一個條件。when指令除了支持branch判斷條件,還支持多種判斷條件。spa
when {
changeset "**/*.js"
}
複製代碼
when {
environment name: 'DEPLOY_TO', value: 'production'
}
複製代碼
when {
equals expected: 2, actual: currentBuild.number
}
複製代碼
when {
expression {
return env.BRANCH_NAME != 'master'
}
}
複製代碼
when {
tag "release-*"
}
複製代碼
tag 條件支持comparator參數,支持的值以下: -- EQUALS:簡單的文本比較。插件
when {
tag "release-3.1", comparator: "EQUALS"
}
複製代碼
-- GLOB (默認值):Ant風格路徑表達式。因爲是默認值,因此使用時通常省略。完整寫法以下:
when {
tag "release-*", comparator: "GLOB"
}
複製代碼
-- REGEXP:正則表達式。使用方法以下:
when {
tag "release-\\d+", comparator: "REGEXP"
}
複製代碼
tag條件塊很是適合根據tag進行發佈的發佈模式。
以上介紹的都是單條件判斷,when指令還能夠進行多條件組合判斷。
allOf {
branch "master";
environment name: 'DEPLOY_TO', value: 'production'
}
複製代碼
注意,多條件之間使用分號分隔。
anyOf {
branch "master";
branch "staging";
}
複製代碼
Generic Webhook Trigger 在以前已經介紹過,能夠這麼傳參
triggers {
GenericTrigger(
genericVariables: [
[key: 'ref', value: '$. ref']
],
token: env.JOB_NAME ,
regexpFilterText: '$ref',
regexpFilterExpression: 'refs/heads/' + env.BRANCH_NAME,
)
}
複製代碼
env.BRANCH_NAME 爲當前 pipeline 的分支名
有些全局環境變量是多分支項目中特有的,能夠直接在pipeline中引用,如 ${env.BRANCH_NAME} 下面的就不翻譯了
For a multibranch project, this will be set to the name of the branch being built, for example in case you wish to deploy to production from master but not from feature branches; if corresponding to some kind of change request, the name is generally arbitrary (refer to CHANGE_ID and CHANGE_TARGET).
For a multibranch project corresponding to some kind of change request, this will be set to the change ID, such as a pull request number, if supported; else unset.
For a multibranch project corresponding to some kind of change request, this will be set to the change URL, if supported; else unset.
For a multibranch project corresponding to some kind of change request, this will be set to the title of the change, if supported; else unset.
For a multibranch project corresponding to some kind of change request, this will be set to the username of the author of the proposed change, if supported; else unset.
For a multibranch project corresponding to some kind of change request, this will be set to the human name of the author, if supported; else unset.
For a multibranch project corresponding to some kind of change request, this will be set to the email address of the author, if supported; else unset.
For a multibranch project corresponding to some kind of change request, this will be set to the target or base branch to which the change could be merged, if supported; else unset.
Multibranch Pipeline Events 的做用是什麼