Jenkins 管道确定分支是否用于 Bitbucket 拉取请求 [英] Jenkins pipeline determine if a branch is for Bitbucket pull request

查看:42
本文介绍了Jenkins 管道确定分支是否用于 Bitbucket 拉取请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Jenkins 与 Bitbucket 分支源插件.

I'm using Jenkins together with the Bitbucket branch source plugin.

一切都很好,但我希望能够根据分支是否与拉取请求相关联来运行/排除管道中的某些阶段,例如:

Everything works great, but I want to be able to run/exclude certain stages in my pipeline depending on whether the branch is associated with a pull request or not, such as:

pipeline {
  stages {
    stage('build') {
      //compile
    }    
    stage('package') {
      when {
        environment name: 'IS_PULL_REQUEST', value: 'true'
      }      
      //create deployable package
    }
  }
}

Jenkins 知道分支何时用于 PR,因为它将源与目标合并,并且还在多分支管道页面的拉取请求文件夹中显示分支.

Jenkins knows when the branch is for a PR because it merges the source with the target and also displays the branch in the pull request folder on the multibranch pipeline page.

我可以在管道中使用环境变量来排除/包含阶段吗?

Is there an environment variable I can use within the pipeline to exclude/include stages?

推荐答案

您可以使用 BRANCH_NAMECHANGE_ID 环境变量来检测拉取请求.当您从分支运行多分支管道构建时(在创建拉取请求之前),会设置以下环境变量:

You can use BRANCH_NAME and CHANGE_ID environment variables to detect pull requests. When you run a multibranch pipeline build from a branch (before creating a pull request), the following environment variables are set:

  • env.BRANCH_NAME 设置为存储库分支名称(例如 develop),
  • env.CHANGE_BRANCHnull
  • env.CHANGE_IDnull.
  • env.BRANCH_NAME is set to the repository branch name (e.g. develop),
  • env.CHANGE_BRANCH is null,
  • env.CHANGE_ID is null.

但是一旦你创建了一个拉取请求,那么:

But once you create a pull request, then:

  • env.BRANCH_NAME 设置为 PR-d+ 名称(例如 PR-11),
  • env.CHANGE_BRANCH 设置为真正的分支名称(例如develop),
  • env.CHANGE_ID 设置为拉取请求 ID(例如 11).
  • env.BRANCH_NAME is set to the PR-d+ name (e.g. PR-11),
  • env.CHANGE_BRANCH is set to the real branch name (e.g. develop),
  • env.CHANGE_ID is set to the pull request ID (e.g. 11).

我在管道中使用以下 when 条件来检测拉取请求:

I use the following when condition in my pipelines to detect pull requests:

when {
    expression {
        // True for pull requests, false otherwise.
        env.CHANGE_ID && env.BRANCH_NAME.startsWith("PR-")
    }
}

这篇关于Jenkins 管道确定分支是否用于 Bitbucket 拉取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆