Webhooks allow you to connect Bitbucket and Jenkins, which can be helpful in many different scenarios, such as when you want to integrate your CI environment with your Repository. Each time you push code to BitBucket, it can trigger the build process in Jenkins. Unfortunately, the webhook setup process is tedious and tricky. Not anymore! With this simplified method for setting up Jenkins BitBucket Webhook Integration, you’ll be able to set it up much faster and without the confusing steps that come with other ways.
Install a Jenkins plugin for Bitbucket
Bitbucket’s plugin system is simple to install; click on Manage Jenkins from your Bitbucket project’s main page and scroll down to select Plugins. This will redirect you to a list of available plugins. Click on Available, and search for Bitbucket Deployments Plugin. Click Install without restart, as it doesn’t need access to your machine but needs to set itself up.
Configure your Jenkins job with bitbucket repository details
To configure your Jenkins job with bitbucket repository details, you must head over to your Jenkins instance. For further information on how to get your Jenkins instance, see the configuration instructions below. After logging in:
- Choose Configure System from the top menu.
- Click on the bitbucket-webhook subheading under the External References header.
- Configure your project with bitbucket repository details: If you want to push changes from a local branch, you need to create a new post-receive hook.
Verify your Jenkins configuration looks ok.
Check out our webhook documentation once you’ve verified that your Jenkins configuration looks ok. This will provide you with instructions on how to activate a Jenkins integration with Atlassian Bitbucket. You’ll want to create a repository in Bitbucket and add a Post-Receive URL in Jenkins if it isn’t already there. Navigate to your project on either website and click Manage Jenkins from within your account settings. Navigate to Manage Plugins from within your Jenkins configuration and enable Bitbucket Server Plugin and save changes. Once helped, navigate back to Repository> Post-Receive URLs.
Configure a webhook on Bitbucket
To configure a webhook on Bitbucket, go to your project settings and select Repository. Then click on the Webhooks tab and hit Add webhook. You’ll need to specify a URL (this can be any URL; if you’re using Jenkins, your instance is likely accessible via HTTP), an event name (say PushEvent) and select a Secret token. The last one is important because that will enable Bitbucket to validate incoming events originating from your source code repository host. Optionally, you can add a Content-Type header specifying application/x-www-form-urlencoded, which will trigger sending data in the POST body when Bitbucket receives an HTTP POST request.
Exercise your integration
Create a new branch named `integration` to exercise your integration in your Repository. Then run `git push integration enslaver` and wait for it to be pulled into your CI server. Wait until you see a message from your CI server in the command line saying that everything passed successfully. This is how you ensure that all of your code was appropriately deployed. You can also create a branch inside your integration branch if you have additional projects or subdomains to test in different scenarios.
Check the Bitbucket Hook Log
You can use the Bitbucket Hook Log to see if the hook has fired. If you see a webhook succeeded in the logs, it was a success! If there is no information regarding the theme, something has gone wrong, and you should check for errors in your account settings or with your code. Again, refer to Bitbucket’s documentation for more detailed troubleshooting steps if needed.
The configuration for a webhook is straightforward. First, you will need to add an Incoming WebHook to your repository page. You will find a link to it under Repository Settings and will be asked for an API key. It would be best to use the bitbucket-integration API key and not anything else; otherwise, it won’t work. After that, click on the Add Incoming WebHook button, and you should see something like the following: Now you are all set and ready to go! The next thing we have to do is build our build job in Jenkins. So let’s go ahead and create a new position in Jenkins Bitbucket integration. Go back to Jenkins dashboard and select the New Item option from the left-hand side menu.
In conclusion, we learned how to set up BitBucket Webhook Jenkins Integration using the Cloudbees plugin, making things much more manageable. This guide can also be used as part of the CI process.