Below is the requirement needed to achieve using the Jenkins Pipeline and i am new bee into Jenkins Pipeline.

  • After completing development work and pushing his changes to Bitbucket the user creates a pull request.
  • In order to approve a pull request we require at least one successful Jenkins build. Thereby we would like to get only the build result of the code checked in for the pull request.
  • When a pull request is created/updated Jenkins shall be triggered automatically for real continuous integration.
  • The build result shall be reported back to Bitbucket.

Used Stash Pull Request Builder and stash Notifier for the above process which is working for Normal Freestyle Project.

We need to migrate the similar functionality using Jenkins pipeline, So have created the jenkins job as below.

enter image description here

The pipeline script to checks out the PR branch and trigger build is as below

node {
    stage('Checkout') {         
            $class: 'GitSCM',
            extensions: [               
                [$class: 'CleanCheckout'],              
            branches: [
                [name: '']
                credentialsId: 'id', 
                url: 'repourl.git'
                refspec: ('+refs/pull-requests/*/from:refs/remotes/origin/pr/*/from'), 
                branch: ('origin/pr/${pullRequestId}/from')

    stage('Build') {    
        sh 'make'
    stage('notify') {
    step([$class: 'StashNotifier'])
        try {
            // Do stuff
            currentBuild.result = 'SUCCESS'     
        } catch(err) {
            currentBuild.result = 'FAILED'
    step([$class: 'StashNotifier'])


Though i have done the above configuration when i create/update the PR, the build is not automatically triggered in jenkins. I guess the notification from stash to jenkins did not happen because we specify "origin/${pullRequestId}/from" in free style project. But i do not have that option to specify in pipeline job.

Tried few alternatives as below.

Instead of stash Pull Request Builder i tried with just "Poll SCM" project and specified cron job to trigger as "H/2 * * * *". Upon commits the job is triggered at jenkins. It means that for every commit the jenkins job gets triggered. But Jenkins should trigger the job when PR is created/updated.

I am missing something here certainly which could be basic and new to jenkins pipeline.

Any hint on achieving the desired behavior?

  • edited the question based on findings – user1876040 Jul 12 '17 at 11:51
  • Basic stuff first, Did you whitelist Bitbucket IPs? Also, can you check bitbucket webhooks and let us know if they are working as expected. – Munai Das Udasin Jul 12 '17 at 21:48
  • Hello, @Munai Das Udasin, Thanks for the response.Below are my observations, As we work in inhouse network, bitbucket ips is one of the whitelisted IP's. So thats not an issue. Also, our analysis on Webhooks has fewer drawbacks a) When PR is updated, then job do not gets triggered at jenkins b) Job gets triggered when PR is declined, as we do not want as per use case. – user1876040 Jul 17 '17 at 7:57

In groovy script, you need to reference environment variables in a different way than in bash.

So probably this line is causing trouble:

branch: ('origin/pr/${pullRequestId}/from')

Try using:

branch: ('origin/pr/' + env.pullRequestId + '/from')

To expand on the answer above - try using:

branch: "origin/pr/${pullRequestId}/from"

because in groovy, string interpolation is not done for simple strings - i.e. strings within single quotes. See http://groovy-lang.org/syntax.html#_string_interpolation for details on that.

  • FYI there's no answer above, StackExchange sites are not forums and the order of the answer is not always by time, when referencing another answer, link to it or it may be hard to know which answer you're referring to later. – Tensibai Dec 1 '17 at 19:25

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.