Access BitBucket payload data in Jenkins pipeline job Access BitBucket payload data in Jenkins pipeline job jenkins jenkins

Access BitBucket payload data in Jenkins pipeline job


Ultimately I had to settle for a workaround.

My final setup is a Pipeline job my-build and a Freestyle job my-build-trigger.

my-build-trigger is straightforward. It has a git repo set up in the SCM section and Build when a change is pushed to BitBucket checked in the build triggers section.

In post-build actions I selected Trigger parameterized build on other projects.

The above is important. Build other projects will not trigger a Pipeline job. It will simply say my-build is not buildable.

Pass your payload parameter onto your downstream (Pipeline) job under predefined parameters, entering as payload=${BITBUCKET_PAYLOAD}.

BITBUCKET_PAYLOAD is an environment variable populated by the BitBucket plugin from the BitBucket payload object. You can see payload documentation from BitBucket here

Your post-build will look like this:Jenkins post-build calling Pipeline with parameters

On your pipeline job, you should then create a corresponding parameter called payload (String type).

In your Jenkinsfile you can use this payload like so:

node {    stage 'Echo stuff pointlessly'    def payload = new groovy.json.JsonSlurper().parseText("${params.payload}")    // Echoes the new commit hash    echo payload.push.changes[0].new.target.hash}

Be warned, you may get a serialization error in your job (java.io.NotSerializableException: groovy.json.internal.LazyMap). That is explained in this question. In short, you need to nullify the payload and other JSONObject vars after use.

Any comments or improvements are always appreciated.


Here is how we solved it:

The latter allows you to receive data in the application/json format, which is what BitBucket sends.

In your job settings, make sure to enable the following options:

generic-webhook-plugin settings

Also, make sure to check both:

  • Print post content
  • Print contributed variables

This will show in your jenkins console all the variables it receive, also decomposing the json and converting it to plain variables. After you see the variables names that you want, make sure to "register" them as normal variables for your project.

I know you asked about using Jenkins Pipeline and this explanation is using the WebUI. The documentation of generic-webhook-trigger covers code snippets for exactly your case. I decided to add WebUI explanation so you can understand the gist of it.