Reputation: 179
I am trying to generate a webhook from Bitbucket to trigger a build whenever a user pushes or merges code on a specific branch. I am using the "Generic Webhook Trigger" plugin in Jenkins to do this.
I have two branches in question:
I have two separate Jenkin jobs(pipeline), one for Dev and one for 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
http://<jenkins_url>/generic-webhook-trigger/invoke?token=<token_name>
I also checked the boxes for Push and Merge.
I created two webhooks one with token_name = "DevToken" and the other "UATToken" and configured both my Jenkins jobs to invoke their 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.
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?
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. 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
Upvotes: 2
Views: 4719
Reputation: 59
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. 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. E.g., in the pipeline of Dev_Branch, you configure Optional filter as below,
Expression: ^.*\bDev_Branch\b.*$
Text: $HOOK_BRANCH_NAME
Upvotes: 1