Implement continuous integration with BitBucket and Jenkins utilizing the “Generic Webhook Trigger” plugin

You will need a Linux/Windows box with Jenkins installed on it, and a BitBucket account, you really do not need to host your own BitBucket server, this method is more aimed at utilizing the free BitBucket account that BitBucket provides with Webhooks.

Jenkins is a continuous integration interface, that runs as a service on a server for almost any programming language. With the use of git hooks running on a totally different version control repository hosting service, pull or any other type of request may trigger Jenkins to start the build process automatically with the branch of your choice.

Jenkins

First step is to install Jenkins. I will not go into details on the installation steps, since there are probably many tutorials out there on how to do this.

Once you have Jenkins installed, and logged in, look for the “New Item” menu item on the left hand side, it will be the first option.

Enter the item name, and make sure you select “Freestyle Project”, click OK.

Now you are probably in the configuration screen, go back to the main screen or the dashboard, and click on “Manage Jenkins -> Manage Plugins, click on the Available tab” again, we need to add the “Generic Webhook Trigger” plugin, so in the filter search for that then install it.

Back on the dashboard again, you will see your project name you just added, click on it and select “Configure”. Under source control management, we select “Git”, and then we type in the repository URL into the Repository URL field, click on “Add” next to credentials.

On the credentials screen, I just entered in our BitBucket credentials for our generic build user with only read permissions. OK out of this screen, and go back to the Configuration screen, and select a branch you want to build when triggered, here I just put origin/master.

Under the “Build Triggers” section select the “Generic Webhook Trigger”, we need this section so that we can tell which branch has been changed and then to make sure it is the master branch in our case, so builds do not get triggered if we merge or push any other branch unrelated, except for master. In order to do this, Jenkins needs to parse the body of the message sent from the BitBucket webhook api, currently the branch name is in the JSON body of the BitBucket payload sent to your Jenkins server.

Under post contents parameters, for variable name we put in: branch
For the expression we put in: $.push.changes[0].old.name
Select the JSONpath radio button.

This will parse the JSON message to that path and store the actual branch name into the branch variable.

(i) Under token, enter in a secure token, you will need this when configuring the webhook api call from BitBucket.

Under optional filter we type in a reg ex we want to use to match the branch name variable contents, in our case I just put in ^master$
since that is the branch I’m interested in. Under text type in $branch this is the variable contents being dereferenced from the capture expression above.

I will leave the shell scripting up to the reader, and not go into that.

Click save, and we are done with the Jenkins side of things.

BitBucket

Log into your BitBucket account, and go to your repository, “Settings” then “Webhooks”, create a new webhook.
Give it a reasonable title, for the URL you will need to enter this:

http://:@.com:/generic-webhook-trigger/invoke?token=

– Obviously this is your Jenkins username.
– You have to log back into Jenkins under your user profile and get it under Configuration, select Show API Token, and paste it in there, it will me like a md5 sum.
– The hostname of the Jenkins server – By default port is 8080
– This is the token I mentioned above check point (i) for reference.

Fill the fields and paste it into the URL section of the BitBucket Webhook section.

Check skip certificate validation and select any additional triggers you want. Click on save, and your done.

Summary

Whenever you trigger what you selected in the BitBucket Webhook section, BitBucket will send a request to your Jenkins server, and Jenkins will begin the build, the build consists of the build script section in Jenkins.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.