62

I have a git repository hosted on BitBucket, and have set up SSH authentication between the repository and my Jenkins server. I can build on Jenkins manually, but cannot get the Jenkins service on BitBucket to trigger builds.

Jenkins configuration:  
- Project Name: [my_jenkins_job]  
- Build Triggers:  
--Trigger Builds Remotely:  
---Token: [token]

BitBucket configuration:  
- Endpoint: http://[my_jenkins_address]/job/[my_jenkins_job]/build (I've also tried build?token=[token])  
- Project Name: [my_jenkins_job]  
- Module Name: [blank]  
- Token: [token]

Visiting http://{my_jenkins_address}/job/{my_jenkins_job}/build?token={token} kicks off a build properly.

Why doesn't pushing a change to BitBucket cause Jenkins to initiate a build?

1
  • Did anyone have issues with the below solutions when running through a reverse proxy inside IIS?
    – John B
    Jun 25, 2013 at 1:44

7 Answers 7

80

Due to the Jenkins Hook of Bitbucket is not working at all for me and I have different Jenkins projects for different branches I had come to this solution:

  • Install Bitbucket Plugin at your Jenkins
  • Add a normal Post as Hook to your Bitbucket repository (Settings -> Hooks) and use following url:

https://YOUR.JENKINS.SERVER:PORT/bitbucket-hook/

and if you have setup authentication on jenkins then URL must be like

https://USERNAME:[email protected]:PORT/bitbucket-hook/

  • Configure your Jenkins project as follows:
  • under build trigger enable Build when a change is pushed to BitBucket
  • under Source Code Management select GIT; enter your credentials and define Branches to build (like **feature/*)

By this way I have three build projects, one for all features, one for develop and one for release branch. Make sure to include the slash ('/') on the end of the URL or the hook won't work.

And best of it, you don't have to ad new hooks for new Jenkins projects.

13
  • 2
    does this also work if i have a local jenkins server on my machine at home? or do I need a public jenkins server reachable from outside?
    – Felix
    Mar 10, 2015 at 10:56
  • 5
    When you are able to address your local machine form outside it should work. Otherwise your jenkis address is unknown to bitbucket.
    – salcosand
    Mar 11, 2015 at 10:32
  • 5
    This answer is superior than the accepted answer and answers that recommend polling.
    – Kim Stacks
    Apr 20, 2015 at 13:09
  • 1
    @KimStacks agreed, and I'm adding the often sited link to Polling Must Die.
    – Damon
    Mar 15, 2016 at 20:59
  • 2
    This saved me: Make sure to include the slash ('/') on the end of the URL or the hook won't work. Mar 13, 2019 at 13:11
40

I have our Jenkins instance set up to poll the repository every minute. Is that not frequent enough for your needs?

Our Jenkins configuration:

Build Triggers > Poll SCM = Checked

Build Triggers > Poll SCM > Schedule =

# every 1 minute
*/1 * * * *
3
  • This totally works for our situation, thanks! But... does anyone use the service?
    – Allen T.
    Jun 27, 2012 at 17:39
  • Works everywhere I know... for whatever reason does NOT work on the troubled box... jobs succeed just fine by running manual builds :(
    – Cmag
    Jan 8, 2015 at 5:46
  • 11
    For new readers, you should look at this answer instead and consider hooking the push instead. It cuts down on chatter and processing. The Bitbucket Plugin mentioned in that answer seems to still be the preferred method when working with Bitbucket.
    – Damon
    Mar 15, 2016 at 21:03
12

You can actually get this to work in Jenkins with the Bitbucket service, but it took some playing around to get it working.

If you use authentication, you can grab an API token from one of the users that can create builds. I created a separate account just for Bitbucket. You'll need at least Jenkins 1.426 to use the API token. You can then use HTTP authentication with the API token as your password for the Bitbucket service.

This is how mine is set up:

Endpoint: http://[bitbucket]:[APITOKEN]@[www.example.com/jenkins/]

Project Name: [NameOfMyJenkinsProject]

Module Name: [empty]

Token: [Token found in Jenkins project settings]

After I set it up like this, I was able to use the token as well as authentication to enable push-triggered builds from Bitbucket.

4
  • Do you need any specific plugins to get this to work? I wasn't able to get this to work, although someone else I know does this.
    – John B
    Jun 20, 2013 at 18:52
  • @JohnBubriski, I have the Git plugin setup, but I don't think that's relevant to receiving notifications like this. Were you able to figure it out?
    – Ian Hunter
    Jun 30, 2013 at 3:55
  • No I wasn't, but I'm 99% sure it has something to do with the fact that I'm running Jenkins behind a reverse proxy in IIS for port 80... I set it up with the direct port and it worked fine. Any experience with that?
    – John B
    Jul 1, 2013 at 13:27
  • @JohnBubriski I had the same issue (Bitbucket hook didn't trigger the Jenkins build and I had a reverse proxy setup). When I uncheck csrf enabled flag in the Bitbucket hook, it started to work.
    – azizunsal
    Feb 16, 2015 at 23:09
11

The token stuff is useless if you use authentication in Jenkins.

use the Git plugin, and use a POST hook with http[s]://your.site.com[/jenkins]/git/[email protected]:your-username/your-repo.git in it. Be sure that polling is on and schedule at some value, or this won't work.

6

Using Poll SCM is good, however it has few disadvantages also, it will fetch metadata from you GIT Repository, which anyway is using some percentage of bandwidth from your bitbucket server and Jenkins server.

It's better that Bitbucket knows when to trigger Jenkins if any new update arrives.If you are using bitbucket latest versions, there are a plugin name "Stash webhooks for Jenkins" which is now compatible with Bitbucket.

Within this plugin, you just have to specify the Jenkins URL and the JOB name, this also provides an extra layer of security between Jenkins and Bitbucket. enter image description here

This is the plugin (Stash webhooks for Jenkins which is not called Bitbucket webhooks for Jenkins) which can be downloaded from Atlassian Marketplace.

enter image description here Above is the Add-on settings where you have to specify Jenkins URL and Repo Clone URL. With the trigger, you can also check the connection etc.

Link for the plugin : Bitbucket webhooks for Jenkins

How this Plugin works:

  1. Install the Git Plugin in Jenkins. Configure your project to use Git for your Source Code Management. You will need to keep the Repository URL you use for configuration in Stash. Enable the Poll SCM option in the Build Triggers. This is required to remotely trigger a build. Since we don’t really need to poll, you can set the poll frequency to poll very infrequently. Save your project configuration.
  2. Enter the URL for your Jenkins instance and the Repository URL that you configured Jenkins to use. You can use the dropdown to get the clone URL for each supported the protocol. Afterwards, feel free to change it to match your Jenkins instance.
  3. That’s it!

With both Jenkins and Stash now configured, if you commit code, a build trigger will automatically occur. What happens is the post-receive hook fires a GET request to Jenkins, which then tells it to poll the repository (why you need to have polling turned on). The poll checks to see if there are actually any changes. Since there are, it triggers the build!

4

Just install the bitbucket plugin and follow the instructions as can be found on the plugins page:

Configure your BitBucket repository with a POST hook, using URL JENKINS_URL/bitbucket-hook (no need for credentials). On each commit, all jobs with "Build when a change is pushed to BitBucket" option enabled will be triggered.

See here: https://wiki.jenkins-ci.org/display/JENKINS/BitBucket+Plugin

1
  • 1
    Neither me. Do we have to make some change in Jenkinfile?
    – Lokesh
    Aug 28, 2019 at 6:49
1

I was struggling with this issue for long, and couldn't find a solution in Jenkins documentation, I found here a solution in Cloudbees support site.

You just need to add these tow webhooks to your bitbucket repo and it works very fine :

https://JENKIN_URL/bitbucket-scmsource-hook/notify
https://JENKIN_URL/bitbucket-hook

And i work for all pipelines even MultiBranch ones.

Hope this helps someone ;)

ADA

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.