BitBucket WebHook Jenkins
Asked Answered
O

3

10

I'd like to configure bitbutcket to trigger a jenkins build.

I've spent some time researching this and all the answers are from a few years ago, and have not found any guides because things seem to have changed since.

What I'm trying to do: A bitbucket push to a particular branch triggers a build.

What I've got: Bitbucket web hooks which fires HTTP request to Jenkins on a push to any branch. I've also installed the Bitbucket plugin on Jenkins which adds a check box in the job config Build when a change is pushed to BitBucket. This checkbox doesnt seem to work (maybe I set it up wrong? minimal docs for this), despite me pushing to the configured branch in the SCM section.

Problem 1: Bitbucket does not fire a GET, but another request which causes a 403. I tested with postman, and it works with a GET, but not a POST.

Problem 2: This HTTP build request is fired on pushes to any branch. While the build is still restricted to a particular branch, it seems unnecessary to be rebuilding all the time.

How do i address these issues? Bitbucket does not seem to be very flexible in customizing this. The Jenkins plugin for bitbucket has a lot of 'bad' reviews. How are developers currently doing this?

Osyth answered 30/8, 2017 at 23:53 Comment(1)
August 2023 onwards solution: plugins.jenkins.io/generic-webhook-triggerDinosaurian
S
9

Is your Jenkins URL accessible from your bitbucket server? If yes that it should be fairly simple to do it. You add the webhook in your repository as http://<url-of-jenkins>/git/notifyCommit?url=<url-of-repository>. When jenkins receives this POST, it automatically triggers builds on those jobs that use this git repo with that URL you give in webhook.
But you also need to make sure your Build Schedule is set to empty for those jobs. otherwise it wont get triggered. You can specify a branch in webhook URL too
See the Push Notification from repository here
https://wiki.jenkins.io/display/JENKINS/Git+Plugin

Stubborn answered 31/8, 2017 at 3:44 Comment(4)
Excellent, thanks. I've set this up and it works, however it builds on pushes to any branch, is this expected?Osyth
it will build based on the branch specifier you set in jenkins job. if your job,you set the branch as develop then only changes from that branch will get this job triggered.Stubborn
For those in the same boat, i got this working by specifying the branch and omitting the ".git" from the bitbucket url webhook. (to specify the branch, see the link above)Osyth
This is not working for Pull Request Merged scenario , also I would like to know how would jenkins know about the authenticated user from bitbucket triggered the Build. What's the authentication and token information in this ? I configured this exactly and bitbucket always gives 200 but Jenkins Builds never get triggered.Aikido
D
30

SPECIFIC solution for Jenkins CI server--Webhook to Jenkins for Bitbucket plugin has been commercialized in latest version of Bit-Bucket and the current price is around $4800 which was earlier a free offering, because of this, guys who want to save their bucks, can go to the alternative solution by using webhooks feature of bit-bucket:-

Steps to create a webhook:-

BitBucket Side

1) Go to your bitbucket repo, click on Repository Setting, under WORKFLOW got for WEBHOOKS option and create a webhook.

a) creation of webhook:- URL https://JenkinsserverURL/git/notifyCommit?url=https://bitbucket.repository-link/repository.git

b) In the name tab, give any name of your choice

c) click on TEST CONNECTION before saving it. Make sure you get http status 200 d) View details your logs, check your request and response is correct.

Things to take care of from Jenkins Side:-

1) Make sure repository mentioned in bitbucket webhook is used in Jenkins job. 2) In SCM option, activate/select Poll SCM option, don't mention anything in the schedule, leave it blank. 3) configure rest job,

Whenever your git repo observes any change an automatic build will get triggered in Jenkins. By default push trigger is activated and if you want to activate other action, please select those events while creating webhook.

***to specify the branch in repository webhook:-

http://yourserver/git/notifyCommit?url=<URL of the Git repository>[&branches=branch1[,branch2]*][&sha1=<commit ID>]

Cheers,

Durango answered 19/12, 2017 at 15:6 Comment(3)
great explanation, the URL formation is something important which I could not find anywhereWhipperin
Great solution. Thank's for thatKilmarx
Your example allows to hard-code branch / SHA1 into the URL, but there's no use for that. If the Git Plugin supports branches (and sha1) parameters, how can you make it dynamic, so that branch in question with the SHA1 of the commit would be inserted?Boardwalk
S
9

Is your Jenkins URL accessible from your bitbucket server? If yes that it should be fairly simple to do it. You add the webhook in your repository as http://<url-of-jenkins>/git/notifyCommit?url=<url-of-repository>. When jenkins receives this POST, it automatically triggers builds on those jobs that use this git repo with that URL you give in webhook.
But you also need to make sure your Build Schedule is set to empty for those jobs. otherwise it wont get triggered. You can specify a branch in webhook URL too
See the Push Notification from repository here
https://wiki.jenkins.io/display/JENKINS/Git+Plugin

Stubborn answered 31/8, 2017 at 3:44 Comment(4)
Excellent, thanks. I've set this up and it works, however it builds on pushes to any branch, is this expected?Osyth
it will build based on the branch specifier you set in jenkins job. if your job,you set the branch as develop then only changes from that branch will get this job triggered.Stubborn
For those in the same boat, i got this working by specifying the branch and omitting the ".git" from the bitbucket url webhook. (to specify the branch, see the link above)Osyth
This is not working for Pull Request Merged scenario , also I would like to know how would jenkins know about the authenticated user from bitbucket triggered the Build. What's the authentication and token information in this ? I configured this exactly and bitbucket always gives 200 but Jenkins Builds never get triggered.Aikido
C
0

For anyone here after July 2022, here are the simple steps I followed to make it work.

Create a live Jenkins URL

First, create a tunnel from a live URL to your local Jenkins URL using ngrok because using locahost:8080 directly as your webhook URL on bitbucket will simply not work as bitbucket does not recognize your local computer.

ps: ngrok claims to be the fastest way to put anything on the internet and I agree,
you can use it beyond Jenkins once you know the trick, 
such as quickly handling out your localhost react app for testing by your friends 
out of your local network

To do this is simple. For Linux:

  1. Install ngrok snap install ngrok
  2. Add authtoken ngrok config add-authtoken <token>

Don't have an auth token, sign up

  1. Start a tunnel on your Jenkins port eg ngrok http 8080

To know more and for other OS, check ngrok download page

You will then get a response like

ngrok                                                                                                                                                                                              (Ctrl+C to quit)
                                                                                                                                                                                                                   
Hello World! https://ngrok.com/next-generation                                                                                                                                                                     
                                                                                                                                                                                                                   
Session Status                online                                                                                                                                                                               
Account                       <your email>@<domain>.com (Plan: <plan type>)                                                                                                                                              
Version                       3.0.6                                                                                                                                                                                
Region                        Europe (eu)                                                                                                                                                                          
Latency                       162ms                                                                                                                                                                                
Web Interface                 <web interface url>
Forwarding                    https://<your-assigned-host>.ngrok.io -> http://localhost:8080                                       

Basically, the web interface URL on click gives you a web interface to inspect all the requests being tunnelled from your ngrok live URL to your local host.

Forwarding URL is basically a proxy to your localhost, so when you want to configure webhook, instead of using locahost:8080, you replace it with ngrok URL eg https://syue-162-34-12-01.eu.ngrok.io and all requests get tunnelled to localhost:8080

Hook up the URL on bitbucket cloud

Secondly, configure your Bitbucket repository with a Webhook, using URL JENKINS_URL/bitbucket-hook/ (no need for credentials but do remember the trailing slash) eg https://syue-162-34-12-01.eu.ngrok.io/bitbucket-hook/

If you are using bitbucket server and not cloud or you want to know more, the bitbucket plugin documentation for Jenkins is pretty straightforward and easily understandable, see bitbucket plugin

then you can inspect all your webhook requests on the web interface URL or via your terminal as well as check your build logs on Jenkins via your localhost port or ngrok live url.

Disclaimer: I have not figured out how to enable build only when a specific branch change but you can configure jenkins to only build a specific branch or any branch created as your need may demand, check Source Code Management and Build Triggers

Charcot answered 26/7, 2022 at 2:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.