需求
Jenkins多分支流水線(Multibranch Pipeline )項目類型能夠在同一個項目的不同分支上實現不同的Jenkinsfile。在多分支流水線項目中, Jenkins 自動的發現、管理和執行在源代碼控制中包含Jenkinsfile
的分支的流水線,這消除了手動創建和管理流水線的需要。
在實際應用中,我們經常需要Git+Jenkins實現代碼提交觸發Job的自動構建。對於普通Job,webhook自動觸發即可;但是對於多分支流水線,每次webhook觸發的可能是master、develop或更多分支的構建,這顯然是不符合要求的,將會產生很多垃圾的構建歷史,給後期查看構建歷史排錯帶來極大的不便。
因此,我們需要的是webhook按分支觸發,例如我們的應用場景:
- Git版本庫有master/develop兩個分支,分別對應生產和開發環境;
- develop分支代碼提交觸發自動構建,master分支手動構建;
自動構建只針對develop分支,這樣按分支構建,將會避免master或其他分支產生垃圾構建。
注意:本文只介紹通過generic webhook trigger觸發部分,至於git版本庫端的webhook請根據實際情況自行配置。
自動構建流程
1.generic webhook trigger 插件實現Jenkins 觸發器;
2.通過插件的正則匹配,基於refs/heads/develop、refs/heads/master進行判斷;
(1)若develop分支提交代碼,則變量ref=refs/heads/develop,匹配refs/heads/develop,觸發構建;
(2)若master分支提交代碼,則變量ref=refs/heads/master,不匹配refs/heads/develop,不觸發構建;
3.在develop分支中根據Jenkinsfile進入不同的stage構建;
由於觸發的是develop分支,則when條件匹配develop分支,develop分支構建;而master分支構建跳過。
關於正則匹配及多分支流水線,請參考以下鏈接:
- https://www.jenkins.io/zh/doc/book/pipeline/multibranch/
- https://plugins.jenkins.io/generic-webhook-trigger/
- https://github.com/jenkinsci/generic-webhook-trigger-plugin/tree/master/src/test/resources/org/jenkinsci/plugins/gwt/bdd
多分支流水線創建
1.點擊Jenkins主頁上的 New Item
2.以test命名job
3.配置job
4.Jenkinsfile
git每個分支必須有相同Jenkinsfile文件,否則“Discovery Branches”檢測不到分支。
pipeline {
agent any
options {
ansiColor('xterm')
timestamps()
}
triggers {
GenericTrigger (
causeString: 'Triggered by develop',
genericVariables: [[key: 'ref', value: '$.ref']],
printContributedVariables: true,
printPostContent: true,
regexpFilterExpression: 'refs/heads/' + BRANCH_NAME,
regexpFilterText: 'refs/heads/develop',
token: 'VXnNT5X/GH8Rs'
)
}
stages {
stage("測試部署") {
when {
branch 'develop'
}
steps {
echo 'develop branch'
}
}
stage("生產部署") {
when {
branch 'master'
}
steps {
echo 'master branch'
}
}
}
post {
unstable {
emailext (
body: """項目名稱:${JOB_NAME}\n構建編號:${BUILD_NUMBER}\n構建日誌:${BUILD_URL}console""",
subject: '【Jenkins構建通知】:$JOB_NAME - Build # $BUILD_NUMBER - Unstable!',
to: '[email protected]',
from: '[email protected]'
)
}
success {
emailext (
body: """項目名稱:${JOB_NAME}\n構建編號:${BUILD_NUMBER}\n構建日誌:${BUILD_URL}console""",
subject: '【Jenkins構建通知】:$JOB_NAME - Build # $BUILD_NUMBER - Successful!',
to: '[email protected]',
from: '[email protected]'
)
}
failure {
emailext (
body: """項目名稱:${JOB_NAME}\n構建編號:${BUILD_NUMBER}\n構建日誌:${BUILD_URL}console""",
subject: '【Jenkins構建通知】:$JOB_NAME - Build # $BUILD_NUMBER - Failure!',
to: '[email protected]',
from: '[email protected]'
)
}
}
}
其中:triggers部分就是本次多分支流水線的關鍵所在,爲便於解釋,我們在下面詳細介紹
Generic Webhook Trigger 插件
Jenkinsfile中的triggers部分,是Generic Webhook Trigger 插件的具體配置,可以通過Declarative Directive Generator
(即聲明式指令生成器)圖形化生成上面的代碼,如下:
1.token配置
token:通過http://JENKINS_URL/generic-webhook-trigger/invoke?token=VXnNT5X/GH8Rs
可以觸發job。
2.過濾配置
其中:
(1)Expression:通過正則表達式匹配不同分支,此項目有兩個分支,即:
refs/heads/master
refs/heads/develop
而’refs/heads’ + BRANCH_NAME 中的BRANCH_NAME是通過環境變量獲取構建過程中的當前分支。
(2)Text:匹配的結果,即如果通過正則匹配的結果爲refs/heads/develop,則觸發構建;否則不會觸發構建
3.webhook觸發構建
(1)瀏覽器中通過webhook手動觸發構建:
http://x.x.x.x/generic-webhook-trigger/invoke?token=VXnNT5X/GH8Rs
通過上圖可看出develop分支觸發了自動構建,而master分支則沒有觸發。
(2)BlueOcean查看
總結
Webhook按分支觸發自動構建使多分支流水線更加靈活化,在便於運維集中管理每個項目的分支同時,有效避免了多分支同時構建產生過多的垃圾構建。
PS:如果你對博文感興趣,請關注我的公衆號“木訥大叔愛運維”,與你分享運維路上的點滴。