{jenkins-plugin-info:bitbucket-push-and-pull-request}

1) This plugin requires a Jenkins v2.138.2 or later and supports

  • Bitbucket Cloud rest api v2.x+ and later
  • Bitbucket Server 5.14+ and later

2) This plugin will not work if the previous Bitbucket plugin  (https://plugins.jenkins.io/bitbucket) is installed.

3) warning: After updating the plugin from a version prior to the 2.x.x, the jobs with a pull request need to be reconfigured, reselecting once again, from the plugin conf. pane, the pull request event, that will trigger the build.


Plugin for Jenkins v2.138.2 or later, that triggers builds on Bitbucket's push and pull requests. It's based on the Sazo's fork (https://github.com/sazo/bitbucket-plugin) of the official Bitbucket Plugin (https://plugins.jenkins.io/bitbucket).

The new features introduced by Bitbucket Push and Pull Request are:


This plugin supports:

Configuration

Before you start...

Bitbucket Push And Pull Request Plugin will not work if the Bitbucket plugin  (https://plugins.jenkins.io/bitbucket) is still installed. So, please de-install the previous Bitbucket plugin if you want to use this new one.

Configure the webhook

In case you are using Bitbucket Cloud, configure your Bitbucket repository adding a webhook in the settings page. In the URL field (see image, point A) add your JENKINS_URL followed by "/bitbucket-hook/" (for example "https://my-jenkins.on-my-planet-far-away.com/bitbucket-hook/") Credentials for the webhook endpoint are not required, the trailing slash is mandatory. For more infos please consult the resource https://confluence.atlassian.com/bitbucket/manage-webhooks-735643732.html.

If you are using Bitbucket Server, follow these instructions: https://confluence.atlassian.com/bitbucketserver/managing-webhooks-in-bitbucket-server-938025878.html

Configure your Jenkins job

1.a. Configure the Bitbucket Repository under the Source Code Management with your credentials. For git:


1.b. In case you are using Mercurial instead of git, configure it as follows:


Please note: the branch, related to the events which trigger the builds, must be specified in the field Revision.


2. Now activate the plugin in your job selecting the "Build with Bitbucket Push and Pull Request Plugin" option in the Build Triggers pane.


Environment variables

Environment variables for Bitbucket Cloud and Server pull requests

NAMEVALUE
BITBUCKET_SOURCE_BRANCHsource branch
BITBUCKET_TARGET_BRANCHtarget branch
BITBUCKET_PULL_REQUEST_LINKlink
BITBUCKET_PULL_REQUEST_IDid
BITBUCKET_PAYLOADpayload as json string

Environment variables for Bitbucket Cloud pushs

NAMEVALUE
REPOSITORY_LINK branch (Deprecated. It will be removed)
BITBUCKET_SOURCE_BRANCHbranch
BITBUCKET_REPOSITORY_URLrepository url
BITBUCKET_PAYLOADpayload as json string

Code snippets


job('example-pull-request-created') {
  	triggers{
  		bitbucketPullRequestCreatedAction()
  	}
  	scm {
		git {
		    remote {
		        url("https://git.company.domain/scm/~username/telegram.git")
		    }
		}
	}
    steps {
        shell('echo START pull request created')
    }
}

job('example-pull-request-updated') {
  	triggers{
  		bitbucketPullRequestUpdatedAction()
  	}
  	scm {
		git {
		    remote {
		        url("https://git.company.domain/scm/~username/telegram.git")
		    }
		}
	}
    steps {
        shell('echo START pull request updated')
    }
}

// bitbucketPullRequestApprovedAction(boolean onlyIfReviewersApproved)
job('example-pull-request-approved') {
  	triggers{
  		bitbucketPullRequestApprovedAction(false)
  	}
  	scm {
		git {
		    remote {
		        url("https://git.company.domain/scm/~username/telegram.git")
		    }
		}
	}
    steps {
        shell('echo START pull request approved')
    }
}

// bitbucketRepositoryPushAction(boolean triggerAlsoIfTagPush, String allowedBranches)
job('example-push') {
  	triggers{
  		bitbucketRepositoryPushAction(false, "")
  	}
  	scm {
		git {
		    remote {
		        url("https://git.company.domain/scm/~username/telegram.git")
		    }
		}
	}
    steps {
        shell('echo START push')
    }
}
properties([
    pipelineTriggers([
        [
            $class: 'BitBucketPPRTrigger',
            triggers : [
                [
                    $class: 'BitBucketPPRPullRequestTriggerFilter',
                    actionFilter: [
                        $class: 'BitBucketPPRPullRequestCreatedActionFilter'
                    ]
                ]
            ]
        ]
    ])
])
node {
	def sourceBranch = ""
        def targetBranch = ""
        try{
            sourceBranch = "${BITBUCKET_SOURCE_BRANCH}";
            targetBranch = "${BITBUCKET_TARGET_BRANCH}";
        }catch(e){}

        if(sourceBranch == ""){
            sourceBranch = 'development'
        }

        if(targetBranch == ""){
            targetBranch = 'master'
        }

        checkout changelog: true, poll: true, scm: [
            $class: 'GitSCM',
            branches: [
                [name: '*/'+sourceBranch]
            ],
            doGenerateSubmoduleConfigurations: false,
            extensions: [
                 [
                    $class: 'PreBuildMerge',
                    options: [
                        fastForwardMode: 'FF',
                        mergeRemote: 'origin',
                        mergeStrategy: 'recursive',
                        mergeTarget: ''+targetBranch
                    ]
                ]
            ],
            submoduleCfg: [],
            userRemoteConfigs: [
                [
                    url: 'https://[user]@bitbucket.org/[org]/[repo].git']
                ]
            ]


        echo 'Some build steps'

}


Change Log

2.2.0 (2019-10-14)

Full Changelog

Implemented enhancements:

Merged pull requests:

2.0.0 (2019-06-25)

Full Changelog

Implemented enhancements:

Closed issues:

Merged pull requests: 

1.6.4 (2019-06-19)

Full Changelog

Closed issues:

Merged pull requests:

1.6.3 (2019-06-14)

Full Changelog

Closed issues:

Merged pull requests:

1.6.2 (2019-05-10)

Full Changelog

Closed issues:

Merged pull requests:

1.6.1 (2019-05-08)

Full Changelog

Implemented enhancements:

Closed issues:

Merged pull requests:

1.6.0 (2019-04-29)

Full Changelog

Implemented enhancements:

Closed issues:

Merged pull requests:

1.5.0 (2019-04-11)

Full Changelog

Implemented enhancements:

Closed issues:

Merged pull requests:


Issues