How to use Bitbucket Server 5.4 webhooks with Jenkins crumbs

Vladimir Krumov October 10, 2017

With version 5.4 of the Bitbucket server, Atlasian (re-)introduced the webhooks. I tried to configure a webhook to notify our Jenkins 2.83 instance but the request was refused with an "Invalid crumb" message.

Please advise how to connect the new Bitbucket server webhooks to a Jenkins 2.x instance.

Please note that the invoked Jenkins job is of type Pipeline, which executes a parameterized script from an SCM.

Here are the details of the HTTP request and response:

Request details

Event type:

Test connection event

URL endpoint:

https://jenkinshost/view/all/job/playground/job/test_pipeline/buildWithParameters

Headers

X-Request-Id: b346ae6c-f455-43f0-92c4-d5e74f922bfc
X-Event-Key: diagnostics:ping

Body

No headers

-------------------------------------------------------------------------

Response details

HTTP status:

403

Headers

Cache-Control: must-revalidate,no-cache,no-store
Server: Jetty(9.4.z-SNAPSHOT)
X-Content-Type-Options: nosniff
Content-Length: 460
Date: Wed, 11 Oct 2017 05:27:33 GMT
Via: 1.1 localhost (Apache-HttpClient/4.4.1 (cache))
Content-Type: text/html; charset=ISO-8859-1

Body

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 403 No valid crumb was included in the request</title>
</head>
<body><h2>HTTP ERROR 403</h2>
<p>Problem accessing /view/all/job/playground/job/test_pipeline/buildWithParameters. Reason:
<pre>    No valid crumb was included in the request</pre></p><hr><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.4.z-SNAPSHOT</a><hr/>

</body>
</html>

 

3 answers

1 vote
Tomas Bjerre February 7, 2018

You can use Generic Webhook Trigger Plugin. It does not require the crumb.

joseluisq September 10, 2019

I did work perfect for me. Thanks!

0 votes
Vladimir Krumov March 22, 2018

Our solution was to install "Pull request notifications" Bitbucket plugin:

It provides a good (for us) set of filters whether to trigger Jenkins for a build.

There is (at least) one main difference with the proposal of @Tomas Bjerre:  "Pull request notifications" Bitbucket plugin filters (sends or not) the requests at the Bitbucket side, while the "Generic Webhook Trigger" Jenkins plugin decides only on Jenkins side if to fulfill a received build request.

The approach to select depends only on your needs and possibilities :-)

michele hallak March 22, 2018

This one allows you to insert the crumble and the documentation is a bit more accurate... But I am looking for a simple solution for:

1. Bitbucket: On pull request, calls Jenkins job with parameters and the two URLs: repository and forked repository,

2. Jenkins job merge and build and report result to the pull request on Bitbucket

3. Integrator can sip coffee instead of working hard... (:+)

But somehow, everything works apart of the "merge" between the two repository....

I am thirsty for advices

Thanks

Michele

Michael Puglisi August 7, 2018

stage('Checkout'){
// get the short commit hash to be used to tag our final image with it env.COMMIT_ID = env.PULL_REQUEST_FROM_HASH.substring(0,8)
echo 'WORKSPACE:' + env.WORKSPACE
echo
'COMMIT_ID:' + COMMIT_ID
echo
'PULL_REQUEST_ID:' + env.PULL_REQUEST_ID
echo
'PULL_REQUEST_VERSION:' + env.PULL_REQUEST_VERSION
echo
'PULL_REQUEST_AUTHOR_EMAIL:' + env.PULL_REQUEST_AUTHOR_EMAIL
echo
'PULL_REQUEST_USER_NAME:' + env.PULL_REQUEST_USER_NAME
echo
'PULL_REQUEST_AUTHOR_NAME:' + env.PULL_REQUEST_AUTHOR_NAME
echo
'PULL_REQUEST_TO_BRANCH:' + env.PULL_REQUEST_TO_BRANCH
echo
'PULL_REQUEST_FROM_HASH:' + env.PULL_REQUEST_FROM_HASH
echo
'PULL_REQUEST_FROM_BRANCH:' + env.PULL_REQUEST_FROM_BRANCH
echo
'PULL_REQUEST_FROM_ID:' + env.PULL_REQUEST_FROM_ID
echo
'PULL_REQUEST_AUTHOR_DISPLAY_NAME:' + env.PULL_REQUEST_AUTHOR_DISPLAY_NAME
echo
'PULL_REQUEST_TITLE:' + env.PULL_REQUEST_TITLE
// checkout from bitbucket
env
.COMMIT_HASH = env.PULL_REQUEST_FROM_HASH
echo
'(Jenkinsfile) checking out from: ' + env.PULL_REQUEST_FROM_ID
checkout([$class:
'GitSCM', branches: [[name: "${env.PULL_REQUEST_FROM_ID}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'e4054b90-d76b-4ae2-bc10-REDACTED', url: 'ssh://git@REDACTED.git']]])
sh "git config --global user.email '${env.PULL_REQUEST_AUTHOR_EMAIL}'"
sh
"git config --global user.name '${env.PULL_REQUEST_USER_NAME}'"
sh
"git merge origin/${env.PULL_REQUEST_TO_BRANCH}"
// notify bitbucket that the job is in progress notifyBitbucket('INPROGRESS') }


 

We use the pull request notifier plugin to do what you are describing. Here is an example.  Note how we merge in the PULL_REQUEST_TO_BRANCH

 

sh "git merge origin/${env.PULL_REQUEST_TO_BRANCH}"

 

0 votes
michele hallak March 22, 2018

I have the same problem....

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events