How to connect Bitbucket to Jenkins properly

112,346

Solution 1

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.

Solution 2

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 [email protected]

  5. OPTIONAL: Now, you can use your git to to copy repo to your desk without passwjord git clone [email protected]: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.

Solution 3

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 Name Token: (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.

Solution 4

By iterating I learned that the Token field and the token in an endpoint can be the same. So I set them to be the same as the user token and it works! Also check that the user has privileges to make a job.

Anyway, you can check access.log and see if Bitbucket makes a try or not.

jenkins

P.S. Also a link to Bitbucket Documentation. May some day it will become more useful.

Solution 5

I am not familiar with this plugin, but we quite successfully use Bitbucket and Jenkins together, however we poll for changes instead of having them pushed from BitBucket (due to the fact our build server is hidden behind a company firewall). This approach may work for you if you are still having problems with the current approach.

This document on Setting up SSH for Git & Mercurial on Linux covers the details of what you need to do to be able to communicate between your build server and Bitbucket over SSH. Once this is done, with the Git Plugin installed, go to your build configuration and select 'Git' under Source Code Management, and enter the ssh URL of your repository as the repository URL. Finally, in the Build Triggers section, select Poll SCM and set the poll frequency to whatever you require.

Share:
112,346

Related videos on Youtube

Highmastdon
Author by

Highmastdon

C#.Net, Java, PHP, Nodejs, PostgreSQL, Oracle, SQL Server, MongoDB, Hadoop, HBase, Javascript, Typescript, git, bash, Fishshell, Jenkins, Rest, GraphQL, Web components, LitElement, React, Angular, Vue, ... Try new tech, keep the best of each

Updated on July 09, 2022

Comments

  • Highmastdon
    Highmastdon almost 2 years

    Since about 1 week now, Bitbucket doesn't (?) send a request to my Jenkins server.

    I've set it all up like this:

    Endpoint http://username:apitoken@jenkinshost/
    username = username in Jenkins
    apitoken = apitoken connected to the username in Jenkins
    jenkinshost = my host where I run Jenkins

    Project name is a project
    Token: The token I can setup in the per-project configuration.

    I've done this according to this website: http://felixleong.com/blog/2012/02/hooking-bitbucket-up-with-jenkins.
    It did work, but it doesn't anymore. Did Bitbucket change something? How can I fix this?

    enter image description here

    • Highmastdon
      Highmastdon almost 11 years
      @andrewdotnich About everything that is possible. At the end Ive done it like robjohncox says (below), with checking for differences. It works, but I don't like the polling part. I just want instant rebuild when I commit...
    • andrewdotnich
      andrewdotnich almost 11 years
      Can you add more detail about how it doesn't work? What error messages are you getting (if any?) Is there anything in the Jenkins logs?
    • Highmastdon
      Highmastdon almost 11 years
      It worked, and from a certain time it din't work anymore. I DONT get error messages as this request should happen from the Bitbucket host, to my server. [sarcasm] Unfortunately I've no root access to the Bitbucket application [/sarcasm]. I might misunderstand your question, if so, please enlighten me :) Also: when I try to do the request myself, by pointing a web browser to my endpoint, it does work. Which means in my understanding, Bitbucket is doing something wrong, or doing nothing at all.
    • Manoj
      Manoj almost 11 years
      You may try out the Bitbucket OAuth Plugin to connect Bitbucket and Jenkins. You can find steps here
    • Highmastdon
      Highmastdon almost 11 years
      Does this have anything to do with pulling from the remote repository (Bitbucket)?
    • DeejUK
      DeejUK over 10 years
      I can trigger a build by hitting a URL in Jenkins, but BitBucket doesn't seem to be sending the right request.
    • Highmastdon
      Highmastdon over 10 years
      @Deejay same thing what I saw.
    • securecurve
      securecurve over 9 years
      @Deejay, how you will trigger a build by hitting a URL in Jnekins?
    • securecurve
      securecurve over 9 years
      @Highmastdon, did you manage to make it working?
    • Highmastdon
      Highmastdon over 9 years
      @securecurve haven't had time or need to fix it. However, I will look into it very soon because the server where it runs, will be reinstalled. I'll report back my findings.
    • securecurve
      securecurve over 9 years
      @Highmastdon, Thanks for your response .. I got it working. Check my answer below
  • Highmastdon
    Highmastdon almost 11 years
    Yes I know this solution, but I'm explicitly looking for setting up this plugin.
  • Highmastdon
    Highmastdon over 10 years
    Great! I've seen a change in how BitBucket calls their 'services'. A rename into hooks indeed. So maybe they've been fixing things regarding this. I'll have a look and if it's working I'll accept your answer :)
  • codematix
    codematix almost 9 years
    The trailing "/" is what got it working for me. PHEW! Thanks!
  • mark
    mark over 8 years
    For me this does not work - I get a 403 error back here. :/
  • mohi
    mohi over 8 years
    @mark maybe this link can help? stackoverflow.com/questions/7427557/…
  • Roman Blachman
    Roman Blachman over 8 years
    Yes, it seems that the Webhooks feature in Bitbucket works perfectly with the Jenkins plugin, just need to add the trailing slash in the end. Tested when using Jenkins with Google based authentication (OAUTH). Thanks!
  • Talha Awan
    Talha Awan over 7 years
    missing "/" was the exact issue. Thanks.
  • Sinaesthetic
    Sinaesthetic over 6 years
    For some reason, jenkins can't connect to the repo (permissions denied) but if I try to ssh to bitbuck from the jenkins box, bitbucket takes the key just fine :(
  • marios
    marios almost 6 years
    how can you get the exact URL to give to bitbucket?