How to trigger a Jenkins multibranch pipeline when code change is pushed to GitHub

Saikat picture Saikat · Feb 6, 2017 · Viewed 10.4k times · Source

I have a Jenkins job (multibranch pipeline) setup for a GitHub repo. I want to trigger that job automatically whenever there is a change pushed to that Git repo. How can I achieve this using a Jenkinsfile?

I want to avoid any config change in the Jenkins job such as Poll SCM etc. For multibranch pipeline there is no such option as Build whenever a change is pushed to GitHub

Answer

Markus Schulte picture Markus Schulte · Jan 8, 2018

For your job "reacting" to events from your GitHub, set the job build trigger to "Poll SCM", but do not specify a schedule, as described on this Stackoverflow answer.

For declarative Jenkinsfile, this means:

pipeline {
  triggers {
    pollSCM('') // Enabling being build on Push
  }
  ...
}

Further readings: