How to connect Bitbucket to Jenkins properly How to connect Bitbucket to Jenkins properly git git

How to connect Bitbucket to Jenkins properly


In order to build your repo after new commits, use Bitbucket Plugin.

There is just one thing to notice:When creating a POST Hook (notice that it is POST hook, not Jenkins hook), the URL works when it has a "/" in the end. Like:

URL: JENKINS_URL/bitbucket-hook/e.g. someAddress:8080/bitbucket-hook/

Do not forget to check "Build when a change is pushed to Bitbucket" in your job configuration.


I had a similar problems, till I got it working. Below is the full listing of the integration:

  1. Generate public/private keys pair: ssh-keygen -t rsa
  2. Copy the public key (~/.ssh/id_rsa.pub) and paste it in Bitbucket SSH keys, in user’s account management console:enter image description here

  3. Copy the private key (~/.ssh/id_rsa) to new user (or even existing one) with private key credentials, in this case, username will not make a difference, so username can be anything:enter image description here

  4. run this command to test if you can get access to Bitbucket account:ssh -T git@bitbucket.org

  5. OPTIONAL: Now, you can use your git to to copy repo to your desk without passwjord git clone git@bitbucket.org:username/repo_name.git
  6. Now you can enable Bitbucket hooks for Jenkins push notifications and automatic builds, you will do that in 2 steps:

    1. Add an authentication token inside the job/project you configure, it can be anything:enter image description here

    2. In Bitbucket hooks: choose jenkins hooks, and fill the fields as below:enter image description here

Where:

**End point**: username:usertoken@jenkins_domain_or_ip**Project name**: is the name of job you created on Jenkins**Token**: Is the authorization token you added in the above steps in your Jenkins' job/project Recommendation: I usually add the usertoken as the authorization Token (in both Jenkins Auth Token job configuration and Bitbucket hooks), making them one variable to ease things on myself.


I was just able to successfully trigger builds on commit using the Hooks option in Bitbucket to a Jenkins instance with the following steps (similar as link):

  1. Generate a custom UUID or string sequence, save for later
  2. Jenkins -> Configure Project -> Build Triggers -> "Trigger builds remotely (e.g., from scripts)"
  3. (Paste UUID/string Here) for "Authentication Token"
  4. Save
  5. Edit Bitbucket repository settings
  6. Hooks -> Edit:Endpoint: http://jenkins.something.co:9009/Module Name:Project Name: Project NameToken: (Paste UUID/string Here)

The endpoint did not require inserting the basic HTTP auth in the URL despite using authentication, I did not use the Module Name field and the Project Name was entered case sensitive including a space in my test case. The build did not always trigger immediately but relatively fast. One other thing you may consider is disabling the "Prevent Cross Site Request Forgery exploits" option in "Configure Global Security" for testing as I've experienced all sorts of API difficulties from existing integrations when this option was enabled.