How to Trigger Non-Multibranch Jobs from Bitbucket Cloud?

Issue

  • I want to configure Jenkins and Bitbucket Cloud to automatically trigger non-multibranch jobs on changes

Environment

Resolution

The Bitbucket Plugin provides support in Jenkins to process webhooks received from Bitbucket Cloud.

Configuration in Jenkins

Freestyle Jobs

Configure the Git SCM:

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: 'dohbedoh-bitbucket-up',
	        url: 'https://bitbucket.org/jenkinssupport/simple-project.git'
    ]]])
}

Configure the project to Build when a change is pushed to Bitbucket:

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

Configuration in Bitbucket Server side

Go to the Settings of a repository. Then go to the section Webhooks and click on Add Webhook:

Give it the name you want and specify the Bitbucket Branch Source endpoint $JENKINS_URL/bitbucket-hook/:

Ensure that the webhook is created and enabled:

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

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.

Moreover as there is no refspec for repository pull requests in Bitbucket Cloud, it makes it difficult to leverage for example the push notification endpoint of the Git Plugin.

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

Troubleshooting

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

References

Bitbucket Integration

Have more questions?

4 Comments

  • 0
    Avatar
    Amedee Van

    Is this documentation also valid for Bitbucket Server, despite the title?

  • 0
    Avatar
    Amedee Van

    In https://support.cloudbees.com/hc/en-us/articles/115000053051-How-to-Trigger-Multibranch-Jobs-from-Bitbucket-Server- the endpoint is

    /bitbucket-scmsource-hook/notify

    Here on this page it is

    /bitbucket-hook/

    Are both endpoints valid?

  • 0
    Avatar
    Amedee Van

    To answer my own question, I tested the connections.

    $JENKINS_URL/

    Status code 200

    $JENKINS_URL/bitbucket-hook/

    Status code 500

    Details:

    <!DOCTYPE html><html class=""><head resURL="/static/d2b967e9" data-rooturl="" data-resurl="/static/d2b967e9">
        
        
    
        <title>Jenkins [Jenkins]</title><link rel="stylesheet" href="/static/d2b967e9/jsbundles/base-styles-v2.css" type="text/css"><link rel="stylesheet" href="/static/d2b967e9/css/color.css" type="text/css"><link rel="stylesheet" href="/static/d2b967e9/css/responsive-grid.css" type="text/css"><link rel="shortcut icon" href="/static/d2b967e9/favicon.ico" type="image/vnd.microsoft.icon"><link color="black" rel="mask-icon" href="/images/mask-icon.svg"><script>var isRunAsTest=false; var rootURL=""; var resURL="/static/d2b967e9";</script><script src="/static/d2b967e9/scripts/prototype.js" type="text/javascript"></script><script src="/static/d2b967e9/scripts/behavior.js" type="text/javascript"></script><script src='/adjuncts/d2b967e9/org/kohsuke/stapler/bind.js' type='text/javascript'></script><script src="/static/d2b967e9/scripts/yui/yahoo/yahoo-min.js"></script><script src="/static/d2b967e9/scripts/yui/dom/dom-min.js"></script><script src="/static/d2b967e9/scripts/yui/event/event-min.js"></script><script src="/static/d2b967e9/scripts/yui/animation/animation-min.js"></script><script src="/static/d2b967e9/scripts/yui/dragdrop/dragdrop-min.js"></script><script src="/static/d2b967e9/scripts/yui/container/container-min.js"></script><script src="/static/d2b967e9/scripts/yui/connection/connection-min.js"></script><script src="/static/d2b967e9/scripts/yui/datasource/datasource-min.js"></script><script src="/static/d2b967e9/scripts/yui/autocomplete/autocomplete-min.js"></script><script src="/static/d2b967e9/scripts/yui/menu/menu-min.js"></script><script src="/static/d2b967e9/scripts/yui/element/element-min.js"></script><script src="/static/d2b967e9/scripts/yui/button/button-min.js"></script><script src="/static/d2b967e9/scripts/yui/storage/storage-min.js"></script><script src="/static/d2b967e9/scripts/hudson-behavior.js" type="text/javascript"></script><script src="/static/d2b967e9/scripts/sortable.js" type="text/javascript"></script><script>crumb.init("Jenkins-Crumb", "656dac061b79bb19fa5b99b9ad227444fe3b9a72a2941f2ea89e4cf130d482af");</script><link rel="stylesheet" href="/static/d2b967e9/scripts/yui/container/assets/container.css" type="text/css"><link rel="stylesheet" href="/static/d2b967e9/scripts/yui/assets/skins/sam/skin.css" type="text/css"><link rel="stylesheet" href="/static/d2b967e9/scripts/yui/container/assets/skins/sam/container.css" type="text/css"><link rel="stylesheet" href="/static/d2b967e9/scripts/yui/button/assets/skins/sam/button.css" type="text/css"><link rel="stylesheet" href="/static/d2b967e9/scripts/yui/menu/assets/skins/sam/menu.css" type="text/css"><link rel="stylesheet" href="/static/d2b967e9/css/google-fonts/roboto/css/roboto.css" type="text/css"><link rel="stylesheet" href="/static/d2b967e9/jsbundles/ui-refresh-overrides.css" type="text/css"><meta name="ROBOTS" content="INDEX,NOFOLLOW"><meta name="viewport" content="width=device-width, initial-scale=1"><script src="/adjuncts/d2b967e9/org/kohsuke/stapler/jquery/jquery.full.js" type="text/javascript"></script><script>var Q=jQuery.noConflict()</script><script>
          if(window.Prototype && JSON) {
            var _json_stringify = JSON.stringify;
            JSON.stringify = function(value) {
                var _array_tojson = Array.prototype.toJSON;
                delete Array.prototype.toJSON;
                var r=_json_stringify(value);
                Array.prototype.toJSON = _array_tojson;
                return r;
            };
         }  
       </script><script src="/plugin/extended-choice-parameter/js/selectize.min.js" type="text/javascript"></script><script src="/plugin/extended-choice-parameter/js/jsoneditor.min.js" type="text/javascript"></script><script src="/plugin/extended-choice-parameter/js/jquery.jsonview.min.js" type="text/javascript"></script><link rel="stylesheet" href="/plugin/extended-choice-parameter/css/jquery.jsonview.css"><link rel="stylesheet" id="icon_stylesheet" href="/plugin/extended-choice-parameter/css/selectize.css"><link rel="stylesheet" id="icon_stylesheet" href="/plugin/extended-choice-parameter/css/selectize.bootstrap2.css"><link rel="stylesheet" id="theme_stylesheet"><link rel="stylesheet" id="icon_stylesheet"><style type="text/css">
        #lenient-shutdown-msg {
            font-weight: bold;
            font-size: larger;
            color: white;
            background-color: #ef2929;
            text-align: center;
            padding: 0.5em;
        }
    </style><style type="text/css">
    .pipeline-annotated {
        display: none;
    }
    
    .pipeline-new-node {
        display: none;
    }
    </style><script src="/static/d2b967e9/jsbundles/vendors.js" type="text/javascript"></script><script src="/static/d2b967e9/jsbundles/page-init.js" type="text/javascript"></script></head><body data-model-type="hudson.model.Hudson" id="jenkins" class="yui-skin-sam two-column jenkins-2.222.1" data-version="2.222.1"><a href="#skip2content" class="skiplink">Skip to content</a><div id="page-head"><header id="header" class="page-header"><div class="page-header__brand"><div class="logo"><a id="jenkins-home-link" href="/"><img src="/static/d2b967e9/images/jenkins-header-logo-v2.svg" alt="[Jenkins]" id="jenkins-head-icon"><img src="/static/d2b967e9/images/title.png" alt="Jenkins" width="139" id="jenkins-name-icon" height="34"></a></div><a href="/" class="page-header__brand-link"><img src="/static/d2b967e9/images/jenkins-header-logo-v2.svg" alt="[Jenkins]" class="page-header__brand-image"><span class="page-header__brand-name">Jenkins</span></a></div><div class="searchbox hidden-xs"><form role="search" method="get" name="search" action="/search/" style="position:relative;" class="no-json"><div id="search-box-minWidth"></div><div id="search-box-sizer"></div><div id="searchform"><input role="searchbox" name="q" placeholder="search" id="search-box" class="main-search__input has-default-text"><span class="main-search__icon-leading"><svg viewBox="0 0 24 24" aria-hidden="" focusable="false" class="svg-icon "><use href="/static/d2b967e9/images/material-icons/svg-sprite-action-symbol.svg#ic_search_24px"></use></svg></span><a href="https://jenkins.io/redirect/search-box" class="main-search__icon-trailing"><svg viewBox="0 0 24 24" aria-hidden="" focusable="false" class="svg-icon "><use href="/static/d2b967e9/images/material-icons/svg-sprite-action-symbol.svg#ic_help_outline_24px"></use></svg></a><div id="search-box-completion"></div><script>createSearchBox("/search/");</script></div></form></div><div class="login page-header__hyperlinks"><div id="visible-am-insertion" class="page-header__am-wrapper"></div><a href="/login?from=%2Fbitbucket-hook%2F"><b>log in</b></a></div></header><div id="breadcrumbBar"><tr id="top-nav"><td id="left-top-nav" colspan="2"><link rel='stylesheet' href='/adjuncts/d2b967e9/lib/layout/breadcrumbs.css' type='text/css' /><script src='/adjuncts/d2b967e9/lib/layout/breadcrumbs.js' type='text/javascript'></script><div class="top-sticker noedge"><div class="top-sticker-inner"><div class="breadcrumbs__wrapper"><ul id="breadcrumbs"><li class="item"><a href="/" class="model-link inside">Jenkins</a></li><li href="/" class="children"></li></ul><div id="breadcrumb-menu-target"></div></div></div></div></td></tr></div></div><div id="page-body" class="clear"><div id="side-panel"><div class="task"><a href="https://jenkins.io/" class="task-icon-link"><img src="/static/d2b967e9/images/24x24/next.png" style="width: 24px; height: 24px; width: 24px; height: 24px; margin: 2px;" class="icon-next icon-md"></a> <a href="https://jenkins.io/" title="Jenkins project" class="task-link">Jenkins project</a></div><div class="task"><a href="https://jenkins.io/redirect/report-an-issue" class="task-icon-link"><img src="/static/d2b967e9/images/24x24/gear2.png" style="width: 24px; height: 24px; width: 24px; height: 24px; margin: 2px;" class="icon-gear2 icon-md"></a> <a href="https://jenkins.io/redirect/report-an-issue" title="Bug tracker" class="task-link">Bug tracker</a></div><div class="task"><a href="https://jenkins.io/redirect/mailing-lists" class="task-icon-link"><img src="/static/d2b967e9/images/24x24/search.png" style="width: 24px; height: 24px; width: 24px; height: 24px; margin: 2px;" class="icon-search icon-md"></a> <a href="https://jenkins.io/redirect/mailing-lists" title="Mailing Lists" class="task-link">Mailing Lists</a></div><div class="task"><a href="https://twitter.com/jenkinsci" class="task-icon-link"><img src="/static/d2b967e9/images/24x24/user.png" style="width: 24px; height: 24px; width: 24px; height: 24px; margin: 2px;" class="icon-user icon-md"></a> <a href="https://twitter.com/jenkinsci" title="Twitter: @jenkinsci" class="task-link">Twitter: @jenkinsci</a></div></div><div id="main-panel"><a name="skip2content"></a><h1 style="text-align: center"><img src="/static/d2b967e9/images/rage.png" width="154" height="179"><span style="font-size:50px"> Oops!</span></h1><div id="error-description"><p>A problem occurred while processing the request.
            Please check <a href="https://jenkins.io/redirect/issue-tracker">our bug tracker</a> to see if a similar problem has already been reported.
            If it is already reported, please vote and put a comment on it to let us gauge the impact of the problem.
            If you think this is a new issue, please file a new issue.
            When you file an issue, make sure to add the entire stack trace, along with the version of Jenkins and relevant plugins.
            <a href="https://jenkins.io/redirect/users-mailing-list">The users list</a> might be also useful in understanding what has happened.<h2>Stack trace</h2><pre style="margin:2em; clear:both">net.sf.json.JSONException: JSONObject["user"] not found.
    	at net.sf.json.JSONObject.getString(JSONObject.java:2040)
    	at com.cloudbees.jenkins.plugins.BitbucketPayloadProcessor.processPostServicePayload(BitbucketPayloadProcessor.java:178)
    	at com.cloudbees.jenkins.plugins.BitbucketPayloadProcessor.processPayload(BitbucketPayloadProcessor.java:42)
    	at com.cloudbees.jenkins.plugins.BitbucketHookReceiver.doIndex(BitbucketHookReceiver.java:54)
    	at java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:627)
    	at org.kohsuke.stapler.Function$MethodFunction.invoke(Function.java:396)
    	at org.kohsuke.stapler.Function$InstanceFunction.invoke(Function.java:408)
    	at org.kohsuke.stapler.Function.bindAndInvoke(Function.java:212)
    	at org.kohsuke.stapler.Function.bindAndInvokeAndServeResponse(Function.java:145)
    	at org.kohsuke.stapler.IndexDispatcher.dispatch(IndexDispatcher.java:27)
    	at org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:747)
    	at org.kohsuke.stapler.Stapler.invoke(Stapler.java:878)
    	at org.kohsuke.stapler.MetaClass$9.dispatch(MetaClass.java:456)
    	at org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:747)
    	at org.kohsuke.stapler.Stapler.invoke(Stapler.java:878)
    	at org.kohsuke.stapler.Stapler.invoke(Stapler.java:676)
    	at org.kohsuke.stapler.Stapler.service(Stapler.java:238)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:755)
    	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1617)
    	at hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:154)
    	at hudson.plugins.audit_trail.AuditTrailFilter.doFilter(AuditTrailFilter.java:113)
    	at hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:151)
    	at jenkins.security.ResourceDomainFilter.doFilter(ResourceDomainFilter.java:76)
    	at hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:151)
    	at com.cloudbees.jenkins.support.slowrequest.SlowRequestFilter.doFilter(SlowRequestFilter.java:37)
    	at hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:151)
    	at org.jenkinsci.plugins.ssegateway.Endpoint$SSEListenChannelFilter.doFilter(Endpoint.java:248)
    	at hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:151)
    	at org.jenkinsci.plugins.modernstatus.ModernStatusFilter.doFilter(ModernStatusFilter.java:52)
    	at hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:151)
    	at hudson.plugins.greenballs.GreenBallFilter.doFilter(GreenBallFilter.java:59)
    	at hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:151)
    	at jenkins.metrics.impl.MetricsFilter.doFilter(MetricsFilter.java:125)
    	at hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:151)
    	at jenkins.telemetry.impl.UserLanguages$AcceptLanguageFilter.doFilter(UserLanguages.java:128)
    	at hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:151)
    	at hudson.util.PluginServletFilter.doFilter(PluginServletFilter.java:157)
    	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604)
    	at com.cloudbees.jenkins.plugins.BitbucketCrumbExclusion.process(BitbucketCrumbExclusion.java:21)
    	at hudson.security.csrf.CrumbFilter.doFilter(CrumbFilter.java:127)
    	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604)
    	at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:84)
    	at hudson.security.UnwrapSecurityExceptionFilter.doFilter(UnwrapSecurityExceptionFilter.java:51)
    	at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    	at jenkins.security.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:118)
    	at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    	at org.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:125)
    	at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    	at org.acegisecurity.ui.rememberme.RememberMeProcessingFilter.doFilter(RememberMeProcessingFilter.java:135)
    	at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    	at org.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:271)
    	at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    	at jenkins.security.BasicHeaderProcessor.doFilter(BasicHeaderProcessor.java:93)
    	at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    	at org.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:249)
    	at hudson.security.HttpSessionContextIntegrationFilter2.doFilter(HttpSessionContextIntegrationFilter2.java:67)
    	at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
    	at hudson.security.ChainedServletFilter.doFilter(ChainedServletFilter.java:90)
    	at hudson.security.HudsonFilter.doFilter(HudsonFilter.java:171)
    	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604)
    	at org.kohsuke.stapler.compression.CompressionFilter.doFilter(CompressionFilter.java:49)
    	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604)
    	at hudson.util.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:82)
    	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604)
    	at org.kohsuke.stapler.DiagnosticThreadNameFilter.doFilter(DiagnosticThreadNameFilter.java:30)
    	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604)
    	at jenkins.security.SuspiciousRequestFilter.doFilter(SuspiciousRequestFilter.java:36)
    	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604)
    	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:545)
    	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
    	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:566)
    	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
    	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235)
    	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1610)
    	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233)
    	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1300)
    	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188)
    	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:485)
    	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHa
  • 0
    Avatar
    Allan Burdajewicz

    Hello Amedee,

    This documentation is for:

    • Non Multibranch job
    • Bitbucket Cloud

    Have a look at Bitbucket Integration to find the link to your scenario.

Please sign in to leave a comment.