Jenkins2 学习系列10 -- 多分支pipeline构建

Updated: 2019-08-15git

若是但愿经过Webhook触发multibranch pipeline项目须要安装 multibranch-scan-webhook-trigger-plugin 插件 安装完以后,配置界面多出一个Scan by webhook选项github

image.png

实际中一个项目的代码仓库可能会有不少分支,好比develop,master等。Jenkins 支持建立多分支pipeline的任务。web

建立多分支项目

新建 "Item" 直接选择 "Multibranch Pipeline" 便可 Tab中有不少配置项,好比 General,Branch Sources,Build Configuration等正则表达式

  • Scan Multibranch Pipeline Triggers 触发 扫描分支频率,最低是1分钟

image.png

  • Orphaned Item 孤儿任务,所谓孤儿任务即代码仓库中该分支被删除,可是Jenkins分支中还保留着。

image.png

image.png

  • Health metric 健康指标 我也不清楚有什么用,望指教

配置完成后,Jenkins就会自动执行首次构建,首先扫描全部的分支,若是根据配置的路径去找Jenkinsfile,找到后就当即执行。express

根据发现的分支数量,好比这里3个就自动建立了3个pipeline项目,点进去后能够像pipeline任务同样进行详细配置。bash

image.png

使用 when 指令判断多分支

咱们须要判断针对不一样分支作不一样事情,使用 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指令的用法

when指令容许pipeline根据给定的条件,决定是否执行阶段内的步骤。when指令必须至少包含一个条件。when指令除了支持branch判断条件,还支持多种判断条件。spa

  • changelog:若是版本控制库的changelog符合正则表达式,则执行
  • changeset:若是版本控制库的变动集合中包含一个或多个文件符合给定的Ant风格路径表达式,则执行
when {
  changeset "**/*.js"
}
复制代码
  • environment:若是环境变量的值与给定的值相同,则执行
when {
  environment name: 'DEPLOY_TO', value: 'production'
}
复制代码
  • equals:若是指望值与给定的值相同,则执行
when {
  equals expected: 2, actual: currentBuild.number
}
复制代码
  • expression:若是Groovy表达式返回的是true,则执行 当表达式返回的是字符串时,它必须转换成布尔类型或null;不然,全部的字符串都被看成true处理。
when {
  expression {
    return env.BRANCH_NAME != 'master'
  }
}
复制代码
  • building Tag:若是pipeline所执行的代码被打 了tag,则执行
  • tag:若是pipeline所执行的代码被打了tag,且tag名称符合规则,则执行 若是tag的参数为空,即tag (),则表示不论tag名称是什么都执行,与buildingTag的效果相同。
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:全部条件都必须符合。下例表示当分支为master且环境变量DEPLOY TO的值为production时,才符合条件。
allOf {
  branch "master";
  environment name: 'DEPLOY_TO', value: 'production'
}
复制代码

注意,多条件之间使用分号分隔。

  • anyOf:其中一个条件为true, 就符合。下例表示master分支或staging分支都符合条件。
anyOf {
  branch "master";
  branch "staging";
}
复制代码

Generic Webhook Trigger 插件在多分支pipeline场景下的应用

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} 下面的就不翻译了

  • 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).

  • CHANGE_ID

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.

  • CHANGE_URL

For a multibranch project corresponding to some kind of change request, this will be set to the change URL, if supported; else unset.

  • CHANGE_TITLE

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.

  • CHANGE_AUTHOR

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.

  • CHANGE_AUTHOR_DISPLAY_NAME

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.

  • CHANGE_AUTHOR_EMAIL

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.

  • CHANGE_TARGET

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

image.png

参考

converting-conditional-to-pipeline/

相关文章
相关标签/搜索