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

3

15

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

Lothair answered 6/2, 2017 at 7:34 Comment(5)
this is what should happen if you configure your mbp. Add your source (git) and fill in the repo endpoint. Make sure there is a Jenkinsfile at root level and set build configuration > mode to by Jenkinsfile. Then make sure that github trigegrs jenkins (via project > settings > services > Jenkins (github plugin))Bayadere
@Bayadere Unfortunately this does not seem to be working. I have covered all the points you mentioned. This worked when I added poll scm option but that's what I want to avoid. Just to add one more detail - I am using Jenkins Enterprise and we have a folder there to create jobs related to our team.Lothair
at my work we have this configured for gitlab, and that works. I assume github is the same. If I have Some time later today I will check if I can get it working for github and then post a screenshotBayadere
Possible duplicate of How to remotely trigger Jenkins multibranch pipeline project build?Coshow
@Bayadere my job configuration (Git, not GitHub) doesn't allow to define build triggers. There's no "Build Triggers" section, only "Scan Multibranch Pipeline Triggers". Are you sure you were testing with a pipeline project? We're on v2.100 from 2018-01-03.Pasol
A
7

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:

Azevedo answered 8/1, 2018 at 13:1 Comment(4)
lol In my experience, this is one out of some more possible problems/solutions when getting a Jenkins-job triggered by GitHub.Azevedo
any chance you can take a look at my latest question?Lynnette
I did have a look at #52992439, but I don't have an idea.Azevedo
In jenkins docs you sent: . For Pipelines which are integrated with a source such as GitHub or BitBucket, triggers may not be necessary as webhooks-based integration will likely already be present.Krispin
N
3

You can use this descriptive pipeline script to auto build your pipeline whenever new changes are pushed to github

pipeline{
   agent any     
     triggers {
        githubPush()
      }
   ...
 }

Also you should enable the github webhook with relavant secret tokens.

Norse answered 13/12, 2019 at 7:27 Comment(0)
S
2

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/...

Smutchy answered 25/1, 2018 at 13:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.