Version 4.2 of Webhook to Jenkins for Bitbucket and onwards support sending custom URL parameters to Jenkins during the notification/trigger process. But this will not work unless Jenkins has been configured to allow url parameters.

Setup

Configuring Jenkins

Git Plugin for Jenkins version 2.5.0 and onwards will by default reject all url parameters. You must configure Jenkins to allow URL parameters by doing ONE of the following:

  1. Setting the JVM switches -Dhudson.model.ParametersAction.keepUndefinedParameters=true and -Dhudson.plugins.git.GitStatus.allowNotifyCommitParameters=trueThis will allow arbitrary notify commit parameters (reference). Only enable this if you trust all unauthenticated users to not pass harmful arguments to your jobs.

  2. Setting the JVM switch -Dhudson.plugins.git.GitStatus.safeParameters=PARM1,PARM1. If this property is set to a comma separated list of parameters, then those parameters will be allowed for any job. Only set this value for parameters you trust in all the jobs in your system.

For detailed instructions on adding Java arguments to Jenkins, see CloudBees guide here

Usage

Bitbucket Server

  1. Go to your hook settings, then go to Webhook to Bitbucket for Jenkins → Advanced Configuration → URL Parameters → Parameters.

  2. Enter the URL parameter into the parameter box. The rules for the parameters are as follows:

    1. The formatting for the url parameter is DESIRED_VARIABLE_NAME={PARAMETER_NAME}. 

    2. Every parameter must start on a new line

    3. Name your parameter whatever you'd like, this is how you're going to reference the parameter in Jenkins. In our example we're going to name the parameter e.g. TARGET_BRANCH.

    4. To pull information from Bitbucket to use as a parameter (like the branch name), look at the table below and put it on the right side of the equal sign and surround it with curly braces, e.g. {BRANCH}

    5. If you want to put your own custom value, just eliminate the curly braces and put the value at the right side of the equal sign.

Jenkins

To be able to access the parameter in your Jenkins job do the following:


  1. Go to your project configuration, then go to the General tab. Select This project is parameterized.

  2. Put the name of the parameter that you chose above (URL Parameter settings in the webhook) in the name box.

You should now have your parameters passed to Jenkins.

Available Parameters

Here's a list of available parameters:

Parameter

Description

PR_SOURCE 

The branch name of the source branch (Only available in pull requests).

PR_DESTINATION 

The branch name of the destination branch (Only available in pull requests).

PR_SOURCE_COMMIT

The most recent commit id/reference for the source branch (Only available in pull requests).

PR_DESTINATION_COMMIT

The most recent commit id/reference for the destination branch (Only available in pull requests).

PR_ID

The pull request ID number (Only available in pull requests).

PR_URL

The URL of the pull request (Only available in pull requests).

PR_TITLE

The title of the pull request (Only available in pull requests).

PR_ACTION

The action that triggered build for the pull request (Only available in pull requests).

PR_VERSION

The version of the pull request (Only available in pull requests).

PR_REVIEWERS

The pull request reviewer display names (Only available in pull requests).

PR_REVIEWERS_SLUG

The pull request reviewer slugs (Only available in pull requests).

REPOSITORY

The repository being affected by the event.

PROJECT

The project the repository being affected by the event is located in.

BRANCH

The branch associated with the event.

COMMIT

The commit id/reference associated with the event.

USER

The username of the user who initiated the event.

USER_EMAIL

The email of the user who initiated the event.

BB_URL

The base URL of the Bitbucket instance hosting the affected repository.

BB_PORT

The network port of the Bitbucket instance hosting the affected repository.