Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 633
Saikat Avatar asked Feb 06 '17 07:02

Saikat


People also ask

How do I trigger Jenkins job in Git push?

Go to Manage Jenkins -> Global Tool Configuration -> Git Add git executable path to Global Tool Configuration. Let us start with creating a Freestyle Project : Step 1: Go to New Item -> create a freestyle project. Step 2: Go to Configure, add a project description, and Github project URL.

How do I trigger a build automatically in Jenkins?

Follow the steps as mentioned below to trigger a Jenkins job automatically based on GitHub's webhook configurations: Step 1: Go to the Configuration page of the respective job and under the build trigger section, check the "GitHub hook trigger for GITScm polling" checkbox and click on the Save button.


2 Answers

The easiest option by far (that I'm aware of) is to remotely tell the Jenkins Git plugin that there's a new commit for a defined repository. However, this will not trigger Jenkins to start a job immediately. What happens is that the Git plugin starts (re-)indexing the specific repository. If changes are detected the Jenkins job is then started.

From your repository (GitHub, GitLab, etc.) you should trigger the following URL:

http://my-jenkins-host/git/[email protected]:group/repository.git&delay=0sec

The value for url must match the SCM URL you configured in the Jenkins job (Git plugin)!

Gotcha: it may be that your Jenkins is not deployed under the root context (/) in which case the URL would be http://my-jenkins-host/context-path/git/...

like image 66
Marcel Stör Avatar answered Oct 10 '22 02:10

Marcel Stör


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:

  • https://jenkins.io/doc/book/pipeline/syntax/#triggers
like image 23
Markus Schulte Avatar answered Oct 10 '22 01:10

Markus Schulte