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

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))
    – Rik
    Commented Feb 6, 2017 at 15:13
  • 1
    @Rik 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.
    – Saikat
    Commented Feb 7, 2017 at 4:20
  • 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 screenshot
    – Rik
    Commented Feb 7, 2017 at 8:49
  • Possible duplicate of How to remotely trigger Jenkins multibranch pipeline project build?
    – jayhendren
    Commented Jul 20, 2017 at 18:53
  • @Rik 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. Commented Jan 25, 2018 at 13:13

3 Answers 3

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:

4
  • lol In my experience, this is one out of some more possible problems/solutions when getting a Jenkins-job triggered by GitHub.
    – Markus
    Commented Oct 25, 2018 at 15:44
  • any chance you can take a look at my latest question?
    – 4c74356b41
    Commented Oct 25, 2018 at 15:53
  • 1
    I did have a look at stackoverflow.com/questions/52991939/…, but I don't have an idea.
    – Markus
    Commented Oct 26, 2018 at 9:59
  • 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.
    – Onur Demir
    Commented Jun 20, 2019 at 10:04
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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.