Atlassian supported Jenkins integration for Bitbucket Server

72 comments

Ashish Sharma June 25, 2020

@Imran Khan This is my usecase, please confirm if can be achieved:

  • BitBucket will notify jenkins when a new tag is created (matching specific pattern) along with a commit. Say tag name will be of pattern release-*
  • If tag pattern is matched on new commit, the job is triggered
  • Job can either be scheduled based on Git polling or based on git push events

Can this be achived using the plugin, will job be triggered and use the commit of newly created tag which matches the pattern?

Thanks,

Ashish

Imran Khan
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 25, 2020

@Ashish Sharma , Bitbucket notifies Jenkins on git push if the plugin has been configured correctly for that repository in Bitbucket. If tag pattern/branch (which was pushed) is matched as per the configuration inside Jenkins then, the job should run. 

Like Ashish Sharma likes this
rweickelt June 26, 2020

@Imran Khan

could you please explain:

  1. What information does the Bitbucket webhook installed by this plugin transport?
  2. How does the Jenkins plugin translate that information into a git checkout?

My observation is, that the installed webhook notifies Jenkins only about something was pushed to bitbucket, but does not contain the exact commit IDs. Thus, when Jenkins receives the trigger event it fetches the whole git repo and then it tries to find out which commits are new (not built yet). It uses the given pattern as a ref filter.

This is IMO similar behavior to what the stash notification plugin did and is a common source for misunderstandings and annoyance. E.g. when connecting an existing repo to a Jenkins job and setting * as branch pattern, Jenkins would build all branches and tags on the first trigger event.

Thanks

Martin Henschke
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 2, 2020

Hi @rweickelt, I'm a developer from Bitbucket Server working on the plugin. Thanks for your questions:

1. Your observation is correct- the webhook does not contain any commit or branch information at all- it's simply a notification for our plugin that there has been a remote change, and will fire each time a commit is pushed.

2. Once we receive a webhook, the plugin performs a poll on the Bitbucket Server, which effectively returns a list of all significant/builable changes in the repo that match the branch selector. A non-empty list indicates a build needs to be performed, and at that point the job is triggered.

Our SCM delegates all git operations to the Git SCM. A revision to build is chosen with a statement like this (with a wild branch selector):

git fetch --tags --force --progress http://your-bitbucket-instance.com/proj/repo/ +refs/heads/*:refs/remotes/webapp/*

The important thing to note here is that a webhook doesn't impart any additional information- it merely asks Jenkins if it wants to perform a job, which is decided based on that poll result, so a first build triggered by a webhook should be the same as one performed by triggering a build manually. I ran a quick test in a Freestyle job after pushing a commit to one branch in a project with multiple branches and commits, and the latest commit was the only one built.

I hope this answers your question. Please feel free to reach out if you have any other questions, and if you're seeing unintended behaviour from the plugin, you can always file a ticket at https://issues.jenkins-ci.org/ with the component atlassian-bitbucket-server-integration-plugin.

Thanks,

Martin

Like Imran Khan likes this
Gabriela Costa July 19, 2020

Hi :) If you're looking for more advanced features you should check out this app my team developed Webhook to Jenkins for Bitbucket.

Here is a list of features that our application offers that you can't do with simple Webhooks:

  • Support for project-level configuration.
  • Data center optimization
  • Support for adding multiple Jenkins servers.
  • Support for build parameters.
  • Easily test and debug your configuration on the hook settings screen.
  • Ability to retrigger builds manually from the pull request screen.
  • Highly configurable, with options to ignore certain committers, branches and more.
  • Support for Bitbucket Mirrors
Imran Khan
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 19, 2020

Hi @Gabriela Costa ,

Thanks for posting the details. 

I'm Product Manager of Bitbucket Server. Can you please expand on what you mean by Data Center optimization?  

To clarify, the Jenkins plugin described in this article works seamlessly with both - Bitbucket Server and Data Center. It includes support for smart mirroring. In fact its so simple that user just needs to select mirror from list of servers from a dropdown inside Jenkins job. Also, you can configure multiple Jenkins instances to use it with single Bitbucket Server/Data Center. 

We want users to be able to use and configure the build jobs easily and avoid lot of manual configurations so, we do the heavy lifting for them. With Bitbucket Server 7.4 we have added a new builds page -https://confluence.atlassian.com/bitbucketserver/bitbucket-server-7-4-release-notes-1013849643.html that will show rich build information from Bamboo and Jenkins inside Bitbucket. You will see more CI/CD features including enhancements to this integration in coming months. 

Cheers,

Imran

Mihai Moldovanu August 8, 2020

I just ran into the same issue as Nikolay

Tim Black August 28, 2020

I have followed the instructions for setting up our Bitbucket Server instance in Jenkins and the personal access token for an admin user, but when testing the connection I get "Connection failure, please try again", despite having followed the instructions at stated without any apparent problem.

For the "Instance URL" I put verbatim what appears as "base URL" in the Server settings of our On Prem Data Center Bitbucket instance: https://bitbucket.our.domain:8443. From the same Jenkins machine, I have no trouble accessing this bitbucket instance over ssh to clone repos.

We are now running Bitbucket Server 7.2.3 and Jenkins 2.222.3, with Bitbucket Server Integration plugin 2.0.1.

I do not see any relevant information in our /var/log/jenkins/jenkins.log after this failed connection test.

Please provide any suggestions you have for troubleshooting this.

Martin Henschke
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 1, 2020

Hi @Tim Black , thanks for your comment. I'm afraid it's difficult to diagnose the issue without more information. You mention being able to clone with SSH locally, which may be the issue- the plugin only supports cloning by SSH from version 2.1.0 and above.

If you are still having issues, please file a ticket at https://issues.jenkins-ci.org/ with the component atlassian-bitbucket-server-integration-plugin.

Thanks,

Martin

Matt Kucia September 8, 2020

That Jenkins plugin is not really an option right now as there are other plugins which support PRs unlike this one.

bart.scully September 8, 2020

It would be really nice if this official plugin supported all of the available webhooks that bitbucket is capable of sending. The lack of ability to build on PR creation is really annoying.

Tim Black September 14, 2020

My problem turned out to be caused by the fact that our bitbucket server was using a self-signed certificate. Once we got an external verified, trusted cert, the connection worked fine.

Like Matt Kucia likes this
Tim Black September 15, 2020

1. It appears that this plugin supports notifying Bitbucket of jenkins build status. Does this plugin make the Bitbucket Server Notifier plugin obsolete?

2. The only Bitbucket-related pipeline steps I'm seeing in the pipeline-syntax page on my jenkins instance is `bbs_checkout`. Is this the only one? Is there some other reference somewhere of what pipeline features this plugin supports? 

2.5. Does this plugin support any pipeline steps (or could you provide me with groovy code) to help me retrieve the available references in a given repo hosted on Bitbucket Server?

I'd like to add job parameters allowing the user to override the submodule references contained in the gitlinks in the primary super-repo. Unless there's any special parameter types provided by this plugin, I would use extendedChoice parameter and run git at the command line to populate a drop-down control. Seems like a very basic feature your plugin could provide as a new Jenkins Pipeline Parameter Type.

EDIT: since writing this I have found the git parameter plugin, which essentially does what I'm asking if the Bitbucket Server plugin could do. https://plugins.jenkins.io/git-parameter/

3. Can I post questions to issues.jenkins-ci.org, or is this the best place to ask questions? 

Like Martin Henschke likes this
sriprasanna.mg December 22, 2020

Hi @Imran Khan 

Thanks a lot for actively working on the improvement of this plugin.

I have an usecase:

Jenkins should start a build anytime a new tag is created in bitbucket. Build is triggered and executing on push but when I create a new tag the build is triggered but nothing else happens.

 

On push:

Snag_580e761.png

On Tag creation:Snag_580f53c.png

 On Tag creation the Trigger is received but build doesnt start hence nothing to return to bitbucket.

Searching online - the recommendation is to 

Set refspec to: +refs/tags/*:refs/remotes/origin/tags/*

branch specifier: **

But there I see no possibility to set the refspec in your plugin 
https://stackoverflow.com/questions/29742847/jenkins-trigger-build-if-new-tag-is-released

Martin Henschke
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 20, 2021

Hi @sriprasanna.mg, thanks for your comment.

As of right now we do not support triggering builds on tag creation. If you'd like us to look at this feature in the future, please create a new ticket at https://issues.jenkins.io/ with the component atlassian-bitbucket-server-integration-plugin.

Thanks,

Martin

can ökçelik January 27, 2021

Hi guys,

Is there any method to access eventKey value comes from json payload? An environment variable etc for that?

I couldnt see on documentation nor global environment variables page if it exists.

If not, simply it should :)

In my case, I try to run Sonarqube analysis only when pr:opened state. So there should be a method to determine which event triggered the current build.

Many thanks

Deleted user March 25, 2021

Getting a 403 on webhook side. Gives a missing crumb error. Not sure how to fix it. Any help?

abbadabbadu March 30, 2021

@Tim Blacksorry to gravedig, we happen to have the same type of setup (bitbucket + self-signed certificate), and are trying to integrate with jenkins via the bitbucket server plugin.
We are experiencing exactly the same type of problems.

Would you mind sharing (if there is more) details of what led to the conclusion about the self-signed certificate?

Thanks!

abbadabbadu March 31, 2021

For the reference, after writing the above comment, I found the issue.

The OkHttp does absolutely the right thing, and validates the host name not on the CN (depreacted by RFC) but on the subjectAltName. Here is an example how to make a CSR :
https://blog.arkey.fr/2017/10/19/self-signed-certificates-in-java.en/

Tim Black April 1, 2021

@abbadabbadu it sounds like you solved your issue, but it's not clear what you did to fix it. We are no longer using self-signed certificate so my experience maybe isn't relevant anymore. 

I'm mostly interested in figuring out what the actual roadmap is for this plugin, specifically when/if it is ever going to support PR-event-triggered webhooks. I see in the release notes that there is general support for webhooks in plugin releases since 1.0-alpha3 in Sept 2019, and that some related bugs have been fixed since, but it's not clear what sort of webhook this is - the release notes just say:

"Automatic webhook creation in a Bitbucket Server repo when a Jenkins job is saved."

@Imran Khan , @Martin Henschke anyone, where can I find information about the functional operation of this webhook? Is this only for triggering the job on pushes to the repo? Where is the webhook payload defined? 

In issues JENKINS-62943 and JENKINS-60342 many users have asked for "support for Pull Requests". There I had proposed that the plugin developers simply provide a generalized webhook functionality that ensures a complete payload is sent to jenkins in the job trigger so that it's pipeline logic can decide and implement behavior. @Martin Henschke can you give an update on the status of this new "In Progress" feature, and what you're actually implementing? Thanks for your time.

pawan.mishra August 12, 2021

Hi @Imran Khan  Does it support "Bitbucket Cloud" as well ?

Kristy
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 5, 2021

Hi @pawan.mishra, this is only for Bitbucket Server / Data Center.

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events