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 的做用是什么