Jenkins builds kicked off by Bitbucket

Jozef Vandenmooter March 12, 2019

Has anyone been able to use a Webhook to kick off Jenkins builds in the latest version of BB Server (6.1.1) WITHOUT installing any plugins? It appears all required functionality is now built in (using a Webhook).

 

 

3 answers

1 accepted

1 vote
Answer accepted
kylekaja123 March 14, 2019

Assuming you mean without installing any Bitbucket Server plugins, I have found a decent way to trigger Jenkins builds using the standard Bitbucket Server webhooks.

I use the Jenkins plugin Generic Webhook Trigger.

The Jenkins plugin exposes a URL endpoint that you can configure in the Bitbucket webhook settings. E.g. http://my-jenkins-server:8080/generic-webhook-trigger/invoke?token=<unique-token>

Bitbucket Server sends a webhook payload as JSON. Use https://confluence.atlassian.com/bitbucketserver/event-payload-938025882.html to determine the payload data you want to extract. In the Jenkins configuration section for the Generic Webhook Trigger plugin, add post content parameters to extract data from the payload using expressions. For example, $.actor.displayName will get the display name of the person that caused the webhook to trigger (such as the person who pushed a commit or opened a pull request).

Here are some example expressions for common variables during a Push event:

repo_slug = $.repository.slug

project_name = $.repository.project.name

event_key (repo:refs_changed) = $.eventKey

date = $.date

commit_id = $.changes[0].toHash

branch_name = $.changes[0].ref.displayId

actor_email = $.actor.emailAddress

actor_display_name = $.actor.displayName

Otherwise, multiple webhooks can be configured in Bitbucket Server to be able to trigger certain jobs for certain events. For example, you may want to have a webhook that triggers a master Jenkins job when a commit is pushed to your master branch or a pull request job that triggers whenever someone opens a new pull request in your repository.In the same section is an item called a "token." This token allows the URL endpoint to only trigger jobs that contain that same token. For example, http://jenkins:8080/generic-webhook-trigger/invoke?token=my-unique-job-token will only trigger jobs that use a token of "my-unique-job-token. These tokens can be used to trigger a single job or groups of jobs through a single webhook. The token is optional, so you can use the URL as http://jenkins:8080/generic-webhook-trigger/invoke to trigger all jobs that do not have a token set.

There are many other features of the Generic Webhook Trigger plugin that is useful for Bitbucket Server webhooks, but I will not go into detail here.

The importance of this plugin is that these extracted parameters can be used elsewhere in the Jenkins configuration, most notably the Git plugin. Use the parameter values by using $parameter_name in places where you want the build to change depending on the webhook payload. For example, if I set up a parameter BB_SERVER_BRANCH = $.changes[0].ref.displayId, I could set the Git plugin "Branches to build" to $BB_SERVER_BRANCH, causing my Jenkins job to choose to build the branch that caused the webhook event.

0 votes
Rob Bradshaw January 18, 2021

Fantastic information. I used this to get branch builds and feedback to BB setup for a pipeline. PR change pushed >  triggers pipeline > fail/success status sent back to BB granting permission to merge changes.

0 votes
emards March 17, 2020

Hello

Very nice answer.

So much details.

I have been looking some of the information for some time.

I will see if it works in my jenkins pipeline.

It gives me lots to work with.

I would have been even happier if there was a documentation link to "common variables during a Push event"

 

Thanks

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events