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?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Allen T.
  • 785
  • 1
  • 6
  • 9
  • Did anyone have issues with the below solutions when running through a reverse proxy inside IIS? – John B Jun 25 '13 at 01:44

7 Answers7

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:PASSWORD@YOUR.JENKINS.SERVER: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.

robjwilkins
  • 5,462
  • 5
  • 43
  • 59
salcosand
  • 2,022
  • 4
  • 24
  • 27
  • 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 '15 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 '15 at 10:32
  • 5
    This answer is superior than the accepted answer and answers that recommend polling. – Kim Stacks Apr 20 '15 at 13:09
  • 1
    @KimStacks agreed, and I'm adding the often sited link to [Polling Must Die](http://kohsuke.org/2011/12/01/polling-must-die-triggering-jenkins-builds-from-a-git-hook/). – Damon Mar 15 '16 at 20:59
  • 1
    In my case, the option "Build when a change is pushed to BitBucket" is triggering the build when someone creates a new branch, which is not good... – Pedro Madrid May 02 '16 at 16:07
  • 1
    @PedroMadrid I'm facing exactly the same as well. The "Repository push" Webhook Trigger causes builds when *anything* is pushed to the repo, which includes Commits, Tags, New Branches. If anyone reading this knows how to, please tell me how to only trigger the webhook when only a commit is pushed to the repo. Thanks! – Daksh Jun 10 '16 at 09:25
  • @PedroMadrid you might define a branch at your jenkins project to ensure that a build will only be triggered when changes happened this one. You can use regex to define a set of branches to listen for. This should avoid building on creating a new branch. On the other hand, why is it bad to build your project due to creating a new branch? You might have modified something. – salcosand Jul 04 '16 at 20:25
  • After a more detailed observation, I realized that the build is triggered when I setup a new job or change the repository configuration. Not as bad as I thought initially. – Pedro Madrid Jul 05 '16 at 12:08
  • 1
    This is superior to the plan Token triggered solution as it respects the branch you've selected in the job whilst regular token webhooks always triggers for all branchs in all jobs that are configured. Now a single webhook for n jobs (each with different branch) – danius Dec 27 '16 at 17:23
  • @salcosand that worked for me , even without passing the jenkins authentication credentials – anudeep Nov 05 '17 at 12:23
  • 1
    This doesn't work for Pipeline Jobs, since I don't have "Source-Code-Management" tab there. And I need to get the git url somehow into the GitHubRepositoryName for the BitbucketPlugin to find it. Very tricky! – VolkerK Dec 01 '17 at 10:48
  • 2
    This saved me: Make sure to include the slash ('/') on the end of the URL or the hook won't work. – Shani Shalgi Mar 13 '19 at 13:11
  • WHen I do this, I get the following rejection between CLI and jenkins `Error 403 No valid crumb was included in the request` – Adam Hughes Apr 14 '21 at 17:22
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 * * * *
Joseph Sturtevant
  • 13,194
  • 12
  • 76
  • 90
  • This totally works for our situation, thanks! But... does anyone use the service? – Allen T. Jun 27 '12 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 08 '15 at 05:46
  • 11
    For new readers, you should look at [this answer instead](http://stackoverflow.com/a/28250478/734790) and consider hooking the push instead. It cuts down on chatter and processing. The Bitbucket Plugin mentioned in [that answer](http://stackoverflow.com/a/28250478/734790) seems to still be the preferred method when working with Bitbucket. – Damon Mar 15 '16 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.

Ian Hunter
  • 9,466
  • 12
  • 61
  • 77
  • 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 '13 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 '13 at 03: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 01 '13 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 '15 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/notifyCommit?url=git@bitbucket.org:your-username/your-repo.git in it. Be sure that polling is on and schedule at some value, or this won't work.

floatingLomas
  • 8,553
  • 2
  • 21
  • 27
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!

Community
  • 1
  • 1
Abhijeet Kamble
  • 3,131
  • 2
  • 30
  • 36
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

joeystdio
  • 1,167
  • 9
  • 7
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