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

Saikat
  • 14,222
  • 20
  • 104
  • 125
  • 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 Feb 06 '17 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 Feb 07 '17 at 04: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 Feb 07 '17 at 08:49
  • Thanks for your help @Rik, much appreciated! – Saikat Feb 07 '17 at 11:18
  • If I use the `Git` (not `Github`) plugin, it works [config](http://pasteboard.co/vujxO7nHa.png) and [branch-index](http://pasteboard.co/vui1xj62N.png) – Rik Feb 07 '17 at 14:07
  • Sorry no luck! I am also using Git plugin with almost same config not sure what have I done wrong :( – Saikat Feb 08 '17 at 05:45
  • Your jenkins instance is not behind a firewall by accident, which doesn't allow github to push to the url? – Rik Feb 08 '17 at 06:42
  • We are using enterprise versions of Jenkins and GitHub. – Saikat Feb 08 '17 at 18:00
  • I am not fafmiliar with those, but are they very different? – Rik Feb 08 '17 at 18:27
  • Maybe a little difference, paid features added :-) – Saikat Feb 10 '17 at 03:38
  • Possible duplicate of [How to remotely trigger Jenkins multibranch pipeline project build?](https://stackoverflow.com/questions/39490150/how-to-remotely-trigger-jenkins-multibranch-pipeline-project-build) – jayhendren Jul 20 '17 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. – Marcel Stör Jan 25 '18 at 13:13

3 Answers3

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:

Markus Schulte
  • 4,171
  • 3
  • 47
  • 58
  • lol In my experience, this is one out of some more possible problems/solutions when getting a Jenkins-job triggered by GitHub. – Markus Schulte Oct 25 '18 at 15:44
  • any chance you can take a look at my latest question? – 4c74356b41 Oct 25 '18 at 15:53
  • 1
    I did have a look at https://stackoverflow.com/questions/52991939/multibranch-pipeline-github-webhook, but I don't have an idea. – Markus Schulte Oct 26 '18 at 09: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 Jun 20 '19 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.

Saikat
  • 14,222
  • 20
  • 104
  • 125
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/notifyCommit?url=git@gitlab.example.com: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/...

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198