Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

how to use the bitbucket payload in jenkins

Ori Wiesel
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 31, 2017

i'm trying to get the branch name that triggered the jenkins job via webhook, in my jenkins job.

but i can't figure how to use the bitbucket payload

2 answers

1 vote
Adrian K_ March 13, 2018

It is an old question but since there is no answer yet, I will suggest the way I'm using.

Bitbucket payload is in JSON format. I'm reading the payload using jq. Jq is similar to sed for json files.

The payload comes to jenkins as BITBUCKET_PAYLOAD. In order to consume the data in jenkins job, I create a variable payload and further I trim the payload to show only specific branch impacted, as follow:

payload=${BITBUCKET_PAYLOAD}
payloadbranch=$(echo $payload | jq -r .push.changes[].new.name)

the "payloadbranch" is the variable to be used as branch in git path.

Anand Patil August 18, 2018

I tried the follow in my jenkins pipeline script

sh'''
payload=${BITBUCKET_PAYLOAD}
echo "payload="+ payload
'''

but the payload is empty. Also tried other parameters but nothing seems to work. This branch name should be made explicitly available to Jenkins for the Webhook plugin

 

Thanks

Anand

Antonius Golly September 5, 2019

The variable BITBUCKET_PAYLOAD is only available in Freestyle jobs. One workaround would be to create such a Freestyle job and trigger with that job your pipeline job as described here: https://stackoverflow.com/questions/41639641/access-bitbucket-payload-data-in-jenkins-pipeline-job

 

Alternatively, and this is my preferred way, you can create a middleware that parses the Bitbucket payload and calls the jenkins hook for parametrized builds as described here: https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build

0 votes
Tomas Bjerre February 7, 2018

You can consume the payload with Generic Webhook Trigger Plugin.

Muddassir Farooq February 19, 2018

but what if one is using bitbucket_webhook?

Deleted user May 5, 2021

or using the Bitbucket Sources Plugin with a multi-branch build configuration?

Nishant Tyagi September 1, 2024

Did anyone fine solution for this basically my webhook does trigger the job but i am unable to capture the payload in json format my issue is i want to capture the pr id and source branch

Adrian K_ September 1, 2024

You need to explain a bit, what does it mean "unable to capture the payload in json format" ? Do you get the payload at jenkins end? What echo $BITBUCKET_PAYLOAD will return. It seems it works only in a freestyle project, I haven't tested in other projects tho.

Nishant Tyagi September 2, 2024

hi @Adrian K_  my issue is my webhook trigger jenkins job once my pr is created now in jenkins job i want the data from webhook payload like what is my pr id for that particular trigger and what is my source branch and i want to echo those thing in jenkins console output my limitation is i amusing very old version of jenkins and i cannot install generic webhook plugin when i add string parameter in jenkins with name payload it gives empty value do i need to add anything at webhook level as well

webhook url: https://username:apitoken@jenkinsurl/job/jobname/buildWithParameters?

 

echo $payload gives empty 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events