Search code examples
githubjenkinscontinuous-integrationjenkins-pipelinemultibranch-pipeline

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


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


Solution

  • 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: