Answers: <p>To expand on <a href="https://devops.stackexchange.com/a/2249">the answer above</a> - try using:</p>
  
  <pre><code>branch: "origin/pr/${pullRequestId}/from"
  </code></pre>
  
  <p>Because in groovy, string interpolation is not done for simple strings - i.e. strings within single quotes. See <a href="http://groovy-lang.org/syntax.html#_string_interpolation" rel="nofollow noreferrer">http://groovy-lang.org/syntax.html#_string_interpolation</a> for details on that.</p>
  
Answers: CC BY-SA 3.0
Answers: 2017-12-01T19:25:20.863Z
Answers: 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.
Answers: Tensibai
Answers: CC BY-SA 4.0
Answers: 2017-12-01T12:33:35.090Z
Answers: 2019-05-22T20:25:14.047Z
Answers: 2019-05-22T20:25:14.047Z
Answers: Lars Immisch
Answers: <p>In groovy script, you need to reference environment variables in a different way than in bash.</p>
  
  <p>So probably this line is causing trouble: </p>
  
  <pre><code>branch: ('origin/pr/${pullRequestId}/from')
  </code></pre>
  
  <p>Try using:</p>
  
  <pre><code>branch: ('origin/pr/' + env.pullRequestId + '/from')
  </code></pre>
  
Answers: CC BY-SA 3.0
Answers: 2017-10-05T16:30:44.673Z
Answers: 2017-10-05T18:24:35.110Z
Answers: 2017-10-05T18:24:35.110Z
Answers: jgebal
Answers: <p>Was the same problem, to achieve the desired result i created two jobs: PR Trigger (Freestyle Project) and PR Builder (Pipeline).</p>
  
  <p>To PR Trigger i added Git plugin, Stash Pull Request Builder Plugin (same configuration didn't work for pipeline) and Parameterized Trigger Plugin (to pass pull request id to PR Builder pipeline; Project to Build - PR Builder).</p>
  
  <p><a href="https://i.sstatic.net/a1Gf0.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/a1Gf0.jpg" alt="enter image description here"></a></p>
  
  <p>To PR Builder i added Parameterized Builds Plugin.</p>
  
  <p><a href="https://i.sstatic.net/y2BwE.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/y2BwE.png" alt="enter image description here"></a></p>
  
Answers: CC BY-SA 4.0
Answers: 2019-02-11T11:27:06.973Z
Answers: 2019-02-11T12:17:42.957Z
Answers: 2019-02-11T12:17:42.957Z
Answers: MariaMironets
Body: <p>Below is the requirement needed to achieve using the Jenkins Pipeline and i am new bee into Jenkins Pipeline.</p>
  <ul>
  <li>After completing development work and pushing his changes to
  Bitbucket the user creates a pull request.</li>
  <li>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.</li>
  <li>When a pull request is created/updated Jenkins shall be triggered
  automatically for real continuous integration.</li>
  <li>The build result shall be reported back to Bitbucket.</li>
  </ul>
  <p><strong>Used Stash Pull Request Builder and stash Notifier for the above process which is working for Normal Freestyle Project.</strong></p>
  <p>We need to migrate the similar functionality using Jenkins pipeline, So have created the jenkins job as below.</p>
  <p><a href="https://i.sstatic.net/bLLH0.jpg" rel="noreferrer"><img src="https://i.sstatic.net/bLLH0.jpg" alt="enter image description here" /></a></p>
  <p>The pipeline script to checks out the PR branch and trigger build is as below</p>
  <pre><code>node {
      stage('Checkout') {         
          checkout(
          [
              $class: 'GitSCM',
              extensions: [               
                  [$class: 'CleanCheckout'],              
              ],
              branches: [
                  [name: '']
              ], 
              userRemoteConfigs: 
              [[
                  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'])
     }
  
  }
  </code></pre>
  <p>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 <code>&quot;origin/${pullRequestId}/from&quot;</code> in free style project. But i do not have that option to specify in pipeline job.</p>
  <p>Tried few alternatives as below.</p>
  <p>Instead of stash Pull Request Builder i tried with just &quot;Poll SCM&quot; project and specified cron job to trigger as <code>&quot;H/2 * * * *&quot;</code>. Upon commits the job is triggered at jenkins. It means that for every commit the jenkins job gets triggered. <strong>But Jenkins should trigger the job when PR is created/updated.</strong></p>
  <p>I am missing something here certainly which could be basic and new to jenkins pipeline.</p>
  <p>Any hint on achieving the desired behavior?</p>
  
Comments: CC BY-SA 3.0
Comments: 2017-07-12T11:51:32.683Z
Comments: edited the question based on findings
Comments: user1876040
Comments: CC BY-SA 3.0
Comments: 2017-07-12T21:48:30.803Z
Comments: Basic stuff first, Did you whitelist Bitbucket IPs?
  Also, can you check bitbucket webhooks and let us know if they are working as expected.
Comments: Munai Das Udasin
Comments: CC BY-SA 3.0
Comments: 2017-07-17T07:57:26.420Z
Comments: 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.
Comments: user1876040
Tags: <continuous-integration><jenkins-pipeline><jenkins-plugins><bitbucket><bcbsn>
Title: Jenkins Pipeline and stash Pull Request Builder not working on PR create/update
UserDisplayName: user1876040
ThreadText: # Jenkins Pipeline and stash Pull Request Builder not working on PR create/update
  
  (Asked by: username_0 on 2017-07-12)
  
  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](https://i.sstatic.net/bLLH0.jpg)](https://i.sstatic.net/bLLH0.jpg)
  
  The pipeline script to checks out the PR branch and trigger build is as below
  
  ```
  node {
      stage('Checkout') {         
          checkout(
          [
              $class: 'GitSCM',
              extensions: [               
                  [$class: 'CleanCheckout'],              
              ],
              branches: [
                  [name: '']
              ], 
              userRemoteConfigs: 
              [[
                  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?
  
  **Tags:** continuous-integration, jenkins-pipeline, jenkins-plugins, bitbucket, bcbsn
  
  > **Comments:**
  > **username_0**: edited the question based on findings  
  > **username_1**: Basic stuff first, Did you whitelist Bitbucket IPs? Also, can you check bitbucket webhooks and let us know if they are working as expected.  
  > **username_0**: Hello, @username_1, 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.  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_2 on 2017-12-01. Score: 3
  
  To expand on [the answer above](https://devops.stackexchange.com/a/2249) - 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.
  
  > **Comments:**
  > **username_3**: 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.  
  
  ---
  
  ### Answer by username_4 on 2017-10-05. Score: 3
  
  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')
  
  ```
  
  
  ---
  
  ### Answer by username_5 on 2019-02-11. Score: 0
  
  Was the same problem, to achieve the desired result i created two jobs: PR Trigger (Freestyle Project) and PR Builder (Pipeline).
  
  To PR Trigger i added Git plugin, Stash Pull Request Builder Plugin (same configuration didn't work for pipeline) and Parameterized Trigger Plugin (to pass pull request id to PR Builder pipeline; Project to Build - PR Builder).
  
  [![enter image description here](https://i.sstatic.net/a1Gf0.jpg)](https://i.sstatic.net/a1Gf0.jpg)
  
  To PR Builder i added Parameterized Builds Plugin.
  
  [![enter image description here](https://i.sstatic.net/y2BwE.png)](https://i.sstatic.net/y2BwE.png)
  
  
  ---
  
  