Answers: <p>Install the git plugin, add credentials for the account you want it to use, then set the job to use SCM Polling. It will check on a preset interval for changes to that repo and trigger when changes are detected. If you need instant triggering upon push, you're looking for webhooks, which is significantly more difficult to set up. I find that polling every 2 minutes or so is good enough in most cases. Here's a decent example: <a href="https://www.baeldung.com/jenkins-job-schedule" rel="nofollow noreferrer">https://www.baeldung.com/jenkins-job-schedule</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2020-09-10T07:29:38.387Z
Answers: 2020-09-10T07:29:38.387Z
Answers: Benjamin Isaacson
Answers: <p>If you're using the declarative DSL, you can use <a href="https://www.jenkins.io/doc/book/pipeline/syntax/#when" rel="nofollow noreferrer"><code>when</code></a></p>
  <p>Suppose the branch you're interested in is <code>main</code>, have a stage like:</p>
  <pre><code>.
  .
  .
  stages {
    stage('Specific Branch') {
      when { branch 'main' }
      steps {
        // only executed when the main branch is changed
      }
    }
  }
  .
  .
  .
  </code></pre>
  <p>From the Jenkins docs:</p>
  <blockquote>
  <p>Built-in Conditions</p>
  </blockquote>
  <blockquote>
  <p>branch</p>
  </blockquote>
  <blockquote>
  <p>Execute the stage when the branch being built matches the branch pattern (ANT style path glob) given, for example: when { branch 'master' }. Note that this only works on a multibranch Pipeline.</p>
  </blockquote>
  <blockquote>
  <p>The optional parameter comparator may be added after an attribute to specify how any patterns are evaluated for a match: EQUALS for a simple string comparison, GLOB (the default) for an ANT style path glob (same as for example changeset), or REGEXP for regular expression matching. For example: when { branch pattern: &quot;release-\d+&quot;, comparator: &quot;REGEXP&quot;}</p>
  </blockquote>
  <p>While this will only execute changes to the <code>main</code> branch, you still need to configure the job to be executed when changes to that branch are made. This can be done with the <a href="https://github.com/jenkinsci/job-dsl-plugin" rel="nofollow noreferrer">job dsl</a>:</p>
  <p>This slightly modified example is taken from the DSL plugin repo:</p>
  <pre><code>pipelineJob('job-dsl-plugin') {
    definition {
      cpsScm {
        scm {
          git {
            remote {
              url('https://github.com/your/repo')
            }
            branch('*/main')
          }
        }
        lightweight()
      }
    }
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2020-11-05T00:59:46.030Z
Answers: as docs says this is applicable only on multi-branch pipeline, is there a way this can be achieved jenkins pipeline job.
Answers: cross_handle
Answers: CC BY-SA 4.0
Answers: 2022-11-21T03:27:46.080Z
Answers: See this: https://issues.jenkins.io/browse/JENKINS-43104
Answers: ATutorMe
Answers: CC BY-SA 4.0
Answers: 2020-09-09T07:04:16.730Z
Answers: 2022-11-21T05:34:47.300Z
Answers: 2022-11-21T05:34:47.300Z
Answers: Bruce Becker
Body: <p>I require a setup in jenkins, which enables me to trigger a jenkins job based on a push event to a specific branch in github. tried so many methods neither worked.</p>
  
Tags: <jenkins><github>
Title: trigger jenkins jobs only when a specific branch is updated
UserDisplayName: cross_handle
ThreadText: # trigger jenkins jobs only when a specific branch is updated
  
  (Asked by: username_0 on 2020-09-09)
  
  I require a setup in jenkins, which enables me to trigger a jenkins job based on a push event to a specific branch in github. tried so many methods neither worked.
  
  **Tags:** jenkins, github
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2020-09-10. Score: 2
  
  Install the git plugin, add credentials for the account you want it to use, then set the job to use SCM Polling. It will check on a preset interval for changes to that repo and trigger when changes are detected. If you need instant triggering upon push, you're looking for webhooks, which is significantly more difficult to set up. I find that polling every 2 minutes or so is good enough in most cases. Here's a decent example: <https://www.baeldung.com/jenkins-job-schedule>
  
  
  ---
  
  ### Answer by username_2 on 2020-09-09. Score: 1
  
  If you're using the declarative DSL, you can use [`when`](https://www.jenkins.io/doc/book/pipeline/syntax/#when)
  
  Suppose the branch you're interested in is `main`, have a stage like:
  
  ```
  .
  .
  .
  stages {
    stage('Specific Branch') {
      when { branch 'main' }
      steps {
        // only executed when the main branch is changed
      }
    }
  }
  .
  .
  .
  
  ```
  
  From the Jenkins docs:
  
  > Built-in Conditions
  
  > branch
  
  > Execute the stage when the branch being built matches the branch pattern (ANT style path glob) given, for example: when { branch 'master' }. Note that this only works on a multibranch Pipeline.
  
  > The optional parameter comparator may be added after an attribute to specify how any patterns are evaluated for a match: EQUALS for a simple string comparison, GLOB (the default) for an ANT style path glob (same as for example changeset), or REGEXP for regular expression matching. For example: when { branch pattern: "release-\d+", comparator: "REGEXP"}
  
  While this will only execute changes to the `main` branch, you still need to configure the job to be executed when changes to that branch are made. This can be done with the [job dsl](https://github.com/jenkinsci/job-dsl-plugin):
  
  This slightly modified example is taken from the DSL plugin repo:
  
  ```
  pipelineJob('job-dsl-plugin') {
    definition {
      cpsScm {
        scm {
          git {
            remote {
              url('https://github.com/your/repo')
            }
            branch('*/main')
          }
        }
        lightweight()
      }
    }
  }
  
  ```
  
  > **Comments:**
  > **username_0**: as docs says this is applicable only on multi-branch pipeline, is there a way this can be achieved jenkins pipeline job.  
  > **username_3**: See this: https://issues.jenkins.io/browse/JENKINS-43104  
  
  ---
  
  