Answers: <p>Using a multi-branch pipeline you have several different ways of filtering branches, take for instance a regular expression for "foo". In order to use this job type, you must create a Jenkinsfile in your repo with build instructions using the scripted or declarative syntax. The term pull request is typically synonymous with services like Github and Bitbucket. </p>
  
  <p><a href="https://i.sstatic.net/TE8VV.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/TE8VV.png" alt="multibranch pipeline branch source"></a>  </p>
  
  <p>You can also filter within the pipeline itself using declarative syntax:</p>
  
  <pre><code>when {changeRequest branch: "foo.*", comparator: 'REGEXP' }
  </code></pre>
  
  <p>And also scripted syntax:</p>
  
  <pre><code>if (env.BRANCH_NAME ==~ /foo.*/){  
      ...
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-07-25T09:50:34.683Z
Answers: 2019-07-25T09:50:34.683Z
Answers: casey vega
Answers: <p>Once could use the <a href="https://jenkins.io/doc/book/pipeline/syntax/" rel="nofollow noreferrer"><code>when</code> in conjunction with the <code>branch</code> keyword if Jenkins declarative pipelines would be used</a>:</p>
  
  <blockquote>
  <pre><code>pipeline {
      agent any
      stages {
          stage('Example Build') {
              steps {
                  echo 'Hello World'
              }
          }
          stage('Example Deploy') {
              when {
                  branch 'production'
              }
              steps {
                  echo 'Deploying'
              }
          }
      }
  }
  </code></pre>
  </blockquote>
  
Answers: CC BY-SA 4.0
Answers: 2019-07-27T10:01:59.187Z
Answers: 2019-07-27T10:01:59.187Z
Answers: 030
Body: <p>I want pull requests to Git where the target is master and the source has a string "foo" to trigger a Jenkins build.</p>
  
  <p>If this string "foo" appears in the source branch's name when there is a pull request, I want the Jenkins build to start.</p>
  
  <p>The only way I can think of doing something this sophisticated is to put the logic into a Jenkins pipeline file.  Is there a plugin or supported way of making this work?  I am not using Github.</p>
  
  <p>Is there a way to filter any branch that does not have "foo" in it?  How do I trigger a build based on a pull request?  I tried doing this by itself (without the search for the pattern "foo"), and even that failed.  There were no errors, but I know Jenkins was authenticating to Git with the credentials I configured in Jenkins.  The Jenkins job would not run when the conditions I configured were met.</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-07-25T07:01:43.790Z
Comments: This depends a bit on which plugins you've got on the server - do you have access to this information?
Comments: Bruce Becker
Tags: <jenkins><git><automation>
Title: How do I configure Jenkins to trigger on a pull request based on the Git branch's name?
UserDisplayName: NewYorkSup
ThreadText: # How do I configure Jenkins to trigger on a pull request based on the Git branch's name?
  
  (Asked by: username_0 on 2019-07-25)
  
  I want pull requests to Git where the target is master and the source has a string "foo" to trigger a Jenkins build.
  
  If this string "foo" appears in the source branch's name when there is a pull request, I want the Jenkins build to start.
  
  The only way I can think of doing something this sophisticated is to put the logic into a Jenkins pipeline file. Is there a plugin or supported way of making this work? I am not using Github.
  
  Is there a way to filter any branch that does not have "foo" in it? How do I trigger a build based on a pull request? I tried doing this by itself (without the search for the pattern "foo"), and even that failed. There were no errors, but I know Jenkins was authenticating to Git with the credentials I configured in Jenkins. The Jenkins job would not run when the conditions I configured were met.
  
  **Tags:** jenkins, git, automation
  
  > **Comments:**
  > **username_1**: This depends a bit on which plugins you've got on the server - do you have access to this information?  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2019-07-25. Score: 2
  
  Using a multi-branch pipeline you have several different ways of filtering branches, take for instance a regular expression for "foo". In order to use this job type, you must create a Jenkinsfile in your repo with build instructions using the scripted or declarative syntax. The term pull request is typically synonymous with services like Github and Bitbucket.
  
  [![multibranch pipeline branch source](https://i.sstatic.net/TE8VV.png)](https://i.sstatic.net/TE8VV.png)
  
  You can also filter within the pipeline itself using declarative syntax:
  
  ```
  when {changeRequest branch: "foo.*", comparator: 'REGEXP' }
  
  ```
  
  And also scripted syntax:
  
  ```
  if (env.BRANCH_NAME ==~ /foo.*/){  
      ...
  }
  
  ```
  
  
  ---
  
  ### Answer by username_3 on 2019-07-27. Score: 1
  
  Once could use the [`when` in conjunction with the `branch` keyword if Jenkins declarative pipelines would be used](https://jenkins.io/doc/book/pipeline/syntax/):
  
  > ```
  > pipeline {
  >     agent any
  >     stages {
  >         stage('Example Build') {
  >             steps {
  >                 echo 'Hello World'
  >             }
  >         }
  >         stage('Example Deploy') {
  >             when {
  >                 branch 'production'
  >             }
  >             steps {
  >                 echo 'Deploying'
  >             }
  >         }
  >     }
  > }
  >
  > ```
  
  
  ---
  
  