title: Jenkins: PR pipeline triggered by a commit to target branch
body: <p>I have the following <code>Jenkinsfile</code>:</p>
  <pre><code>pipeline {
      environment {
          STAGING_BRANCH = 'project'
      }
      agent any
      stages {
          stage('Staging Environment') {
              steps {
                  sh &quot;sh /bin/create-staging-env&quot;
                  }
              }
              when {
                  expression { env.CHANGE_TARGET == env.STAGING_BRANCH &amp;&amp; env.CHANGE_ID }
              }
          }
      }
  }
  </code></pre>
  <p>The condition <code>expression { env.CHANGE_TARGET == env.STAGING_BRANCH &amp;&amp; env.CHANGE_ID }</code> is meant to only execute the stage when it is a pull request and the target branch is <code>project</code>.</p>
  <p>I have this pull request which only has 1 commit:</p>
  <p><a href="https://i.stack.imgur.com/wfUql.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wfUql.png" alt="enter image description here" /></a></p>
  <p>But jenkins ran this pipeline multiple (7) times:</p>
  <p><a href="https://i.stack.imgur.com/FYKTl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FYKTl.png" alt="enter image description here" /></a></p>
  <p><strike>My guess for the trigger of the additional builds is that when a commit is pushed to another branch, and that branch has a pull request.</strike></p>
  <p><strong>Edit</strong>
  Now I understand why the builds were created. They were caused by commits to the target branch <code>project</code>, since the target branch changed by a commit, Jenkins will execute the pipeline for that PR again.</p>
  <p>Then my question changes to, How to get which branch the commit that triggered this pipeline is pushed into. For example if a commit is pushed to the <code>project</code> branch I would like to ignore the stage for PRs. I want to do something like <code>expression { ... &amp;&amp; env.COMMIT_BRANCH != 'project'}</code></p>