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

2,939 阅读4分钟

Updated: 2019-08-15

如果希望通过Webhook触发multibranch pipeline项目需要安装 multibranch-scan-webhook-trigger-plugin 插件 安装完之后,配置界面多出一个Scan by webhook选项

image.png

实际中一个项目的代码仓库可能会有很多分支,比如develop,master等。Jenkins 支持创建多分支pipeline的任务。

创建多分支项目

新建 "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,找到后就立即执行。

根据发现的分支数量,比如这里3个就自动创建了3个pipeline项目,点进去后可以像pipeline任务一样进行详细配置。

image.png

使用 when 指令判断多分支

我们需要判断针对不同分支做不同事情,使用 if else 比较low,不够优雅

stage("deploy to test") {
  steps {
      script {
          if (env.GIT_NAME == 'testing') {
            echo 'deploy to test'
          }
     }
   }
}

可以使用when指令

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判断条件,还支持多种判断条件。

  • 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/