简体   繁体   English

Jenkins 通用 Webhook 触发器,用于特定于分支的

[英]Jenkins Generic Webhook Trigger, for branch specific

I am trying to generate a webhook from Bitbucket to trigger a build whenever a user pushes or merges code on a specific branch.我试图从 Bitbucket 生成一个 webhook 以在用户推送或合并特定分支上的代码时触发构建。 I am using the "Generic Webhook Trigger" plugin in Jenkins to do this.我正在 Jenkins 中使用“通用 Webhook 触发器”插件来执行此操作。

I have two branches in question:我有两个分支有问题:

  1. Dev_Branch Dev_Branch
  2. UAT_Branch UAT_Branch

I have two separate Jenkin jobs(pipeline), one for Dev and one for UAT .我有两个单独的 Jenkin 工作(管道),一个用于Dev ,一个用于UAT The jobs build and deploy the code found in their respective branches.作业构建和部署在其各自分支中找到的代码。

I have created a webhook on bitbucket and configured the URL to look like this我在 bitbucket 上创建了一个 webhook 并将 URL 配置为如下所示

 http://<jenkins_url>/generic-webhook-trigger/invoke?token=<token_name>

I also checked the boxes for Push and Merge .我还检查了PushMerge的框。

I created two webhooks one with token_name = "DevToken" and the other "UATToken" and configured both my Jenkins jobs to invoke their token_name.我创建了两个 webhook,一个是 token_name = "DevToken",另一个是 "UATToken",并配置了我的两个 Jenkins 作业来调用它们的 token_name。 However, what I found is that, whenever a push/merge is made to any branch in my repo (even branches outside of Dev_Branch and UAT_Branch), the webhook is triggered and both Jenkin Jobs are built and deployed.但是,我发现,每当对我的 repo 中的任何分支(甚至是 Dev_Branch 和 UAT_Branch 之外的分支)进行推送/合并时,都会触发 webhook 并构建和部署两个 Jenkin 作业。

Can some guide me on how to configure my Jenkin Jobs so that my Dev pipeline is only triggered by changes in Dev_Branch and my UAT pipeline is only triggered by changes in UAT_Branch?有人可以指导我如何配置我的 Jenkins 作业,以便我的开发管道仅由 Dev_Branch 中的更改触发,而我的 UAT 管道仅由 UAT_Branch 中的更改触发吗?

I have read some tutorials that suggest using the "Post content Parameters" that are pulled from the JSON when a webhook is triggered, but this only stores the values from the JSON into variables.我读过一些教程,建议使用在触发 webhook 时从 JSON 中提取的“发布内容参数”,但这仅将 JSON 中的值存储到变量中。 And it wasn't made clear on how to use these variables to configure whether or not the build should be carried out.并且没有明确说明如何使用这些变量来配置是否应该执行构建。

Many Thanks非常感谢

Since you already know of how to pull values from the JSON, say, you have created a parameter, HOOK_BRANCH_NAME , in Post content parameters .因为您已经知道如何从 JSON 中提取值,所以您已经在Post content parameters 中创建了一个参数HOOK_BRANCH_NAME To make each pipeline project only build on specific branch, the rest is to use the above parameter in Optional filter section and filter out the branch you want.为了让每个pipeline项目只建立在特定的分支上,剩下的就是在Optional filter部分使用上面的参数,过滤掉你想要的分支。 Eg, in the pipeline of Dev_Branch, you configure Optional filter as below,例如,在 Dev_Branch 的管道中,您配置Optional 过滤器如下,

Expression: ^.*\bDev_Branch\b.*$
Text: $HOOK_BRANCH_NAME

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM