How to Trigger Non-Multibranch Jobs from BitBucket Server?

Issue

  • I want to configure Jenkins and BitBucket Server to automatically trigger non-multibranch jobs on changes

Environment

Resolution

The Git Plugin provides support in Jenkins to process webhooks received from Bitbucket Server via the add-on Webhook to Jenkins for Bitbucket. This add-on performs a request to the endpoint /git/notifyCommit that is the push notification feature of the Git Plugin.

Note: The Bitbucket Plugin provides support in Jenkins to process webhooks received from Bitbucket Server via the add-on Bitbucket Server Web Post Hooks Plugin. However the functionality is currently broken. This is tracked by JENKINS-28877.

Configuration in Jenkins

Freestyle Jobs

1) Configure the Git SCM:

2) Configure the project to “Build when a change is pushed to BitBucket”:

Pipeline Jobs

1) Configure the Pipeline script from SCM:

Or a Pipeline script:

node {
    checkout([$class: 'GitSCM',
        branches: [[name: '*/develop']],
        doGenerateSubmoduleConfigurations: false,
        extensions: [],
        submoduleCfg: [],
        userRemoteConfigs: [[
            credentialsId: 'support-team-bitbucket-up',
            url: 'https://bitbucket.beescloud.com/scm/ce/ce-424.git'
    ]]])
}

2) Configure the project to “Build when a change is pushed to BitBucket”:

IMPORTANT: Build the project once to retrieve the Git data.

Note: It is possible to checkout multiple repositories in Pipeline. Be aware that when the Git plugin receives a notification that a certain repository has changes, any Jenkins pipeline that checkouts this repository using the git or the checkout step may be triggered if this step does not have poll: false specified

Configuration in Bitbucket Server side

Go to the Settings of a repository. Then go to the section Hooks and click on Bitbucket Server Webhook to Jenkins:

Specify the Jenkins URL. Also ensure that the Repo Clone URL matches the Repository URL in your Jenkins job:

Click on Enable and ensure that the webhook and enabled:

From now on, Push events to the repository notifies the Bitbucket plugin and triggers a new build.

Consideration

1) You need to ensure that the Repo Clone URL configured is exactly the same as in the Jenkins job, if not the trigger will not work correctly.

2) You need to ensure that your workspace is not deleted after every build. The Git plugin needs the workspace in order to compare previous commits to check if there were changes in the repository or not.

Pull Requests

(We strongly encourage to use Pipeline Multibranch for such scenario)

There is currently no simple solution to trigger jobs on Pull Requests for BitBucket Cloud. There is some work in progress in the Bitbucket Plugin to make this possible. This is tracked by JENKINS-23685.

In the meantime, a workaround is to use polling solution like Stash Pullrequest Builder

Troubleshooting

Please have a look at Bitbucket Webhooks Troubleshooting to troubleshoot issues with these solutions.

References

Bitbucket Integration

Have more questions? Submit a request

3 Comments

  • 4
    Avatar
    Irmantas Marozas

    What exactly allows Bitbucket Webhook to trigger build without providing any authentication in the URL? Is there any authentication process happening behind the scenes?

  • 0
    Avatar
    Philip O'gorman

    When I hit the test button I get : Error: Connection reset,

    What is is the authentication set up needed?

  • 0
    Avatar
    Allan Burdajewicz

    This atlassian plugin send a request to the `/git/notifyCommit` endpoint. As explained in the documentation of the Git plugin, this endpoint does not require authentication.

    In order to troubleshoot this, you can add a Logger to the package `hudson.plugins.git.GitStatus` and check if the notification is received:

      Received notification for uri = https://my.bitbucket.examle.com/scm/myproject/myrepo.git ; sha1 = c2dca5be8beffb8843556e876a35e746b03348db ; branches = [master]

    If you encounter any issue, feel free to open a ticket to CloudBees Support.

Please sign in to leave a comment.