41

Jenkins is running on localhost. I have my repository in GitHub. I have the option to 'Build when a change is pushed to GitHub' checked.

When I click 'Build Now', build is done successfully, no issues there. But when am committing code to my repository, auto build is not happening. I can access GitHub from my system as the repository is public and I believe even Jenkins should be able to detect it. I know there is a polling option but I want Jenkins to build when change is detected in repository(as this is what we have been trying to achieve).

Configuration:

Jenkins 1.615

Git Plugin 2.3.5

Git Client Plugin 1.17.1

————————————————————————————————————————————

EDIT: "Build when a change is pushed to GitHub" option has been renamed to "GitHub hook trigger for GITScm polling" in most recent version of GitHub plugin. (thanks to @smrubin's feedback.)

mainframer
  • 20,411
  • 12
  • 49
  • 68
Anuj Balan
  • 7,629
  • 23
  • 58
  • 92

4 Answers4

77

I suspect you missed the webhook url.

Besides checking the Build when a change is pushed to GitHub option, you should also add the webhook url into your Github repository to get the Auto trigger mechanism to work and here is how:

Go to your Github repository:

Settings--> Webhooks&Services-->Service--> Add Services--> Choose "Jenkins (GitHub plugin)"

Then fill in the Jenkins hook url with your jenkins url like this: http://your_jenkins_url/github-webhook/ enter image description here


And, VERY IMPORTANT, since you are installing your jenkins server in your localhost, please be aware that you shouldn't fill in above Jenkins hook url like http://localhost:8080/github-webhook/ because Github is not able to recognize localhost or 127.0.0.1 or 192.168.*.*.

Either you should use an externally accessible DNS name or an IP address, which can be recognized by Github.

mainframer
  • 20,411
  • 12
  • 49
  • 68
  • This answer has come as a ray of hope :) I understand that I need to go to GitHub and add service to by providing the Jenkins hook URL. I am confused what this ? How do I get that / configure the same? – Anuj Balan Jun 01 '15 at 16:43
  • Under GitHub WebHook(Jenkins Global Config), I found this 'http://localhost:8080/github-webhook/' and I believe thats what you were referring to. I need to figure out how to remove this localhost – Anuj Balan Jun 01 '15 at 17:23
  • @AnujBalan: were you able to figure out to remove the localhost? – inquisitive Sep 03 '15 at 04:58
  • Saved my time :) Great – Kapil Yadav Jul 04 '16 at 12:27
  • how did u fix this? Where did u get the public ip from jenkins? – Perazim Aug 26 '16 at 09:01
  • In my case it started working after installation of ParameterizeRemoteBuild plugin (Trigger Build remotely option) in build section. – Lovey Nov 09 '16 at 18:24
  • 2
    @Perazim Use ngrok. [Setting up Jenkins on Ubuntu with Vagrant and accessing it on Internet with ngrok](http://www.inanzzz.com/index.php/post/eh2c/setting-up-jenkins-on-ubuntu-with-vagrant-and-accessing-it-on-internet-with-ngrok) – BentCoder Jan 17 '17 at 12:09
  • 3
    FYI, in most recent version of the GitHub plugin, this option has been renamed to "GitHub hook trigger for GITScm polling" https://wiki.jenkins-ci.org/display/JENKINS/Github+Plugin – smrubin Jan 22 '17 at 06:24
  • Can I do this by adding Jenkins machine's ssh key in github? – Harsha Biyani Aug 09 '17 at 09:33
45

I had the same problem and solved it. The problem was that the URL had to end with a /. Meaning:

http://<jenkinsurl>:8080/github-webhook/

http://<jenkinsurl>:8080/github-webhook

(Notice the missing / at the end.)

AskYous
  • 4,332
  • 9
  • 46
  • 82
3

Actually if you do the webhook settings from Jenkins -> Github plugin configuration (mentioned above), you will still see webhooks get created in github. So, above two approaches basically doing the same thing.

I personally like it to create webhook from Github, because in this way you don't have to share or store github user info in jenkins.

Steps :

  1. Login into Github (with Admin)

  2. Go to the repository you want to hook with jenkins

  3. Click on settings tab -> webhooks & services
  4. Click on Add Webhook.
  5. Enter payload url : like : http://:8080/github-webhook/
  6. Select content type as json.
  7. you are done.

Now you do the changes and commit , you will see jenkins build get trigger automatically. Don't forget to do the settings in jenkins jobs to start the build when push code in github.

Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
suprakash
  • 31
  • 2
1

The GitHub plugin (https://wiki.jenkins-ci.org/display/JENKINS/GitHub+Plugin) mentioned two ways: manual mode and automatic mode. mainframer gives the answer to the manual mode. Today I found something new. mainframer's add to the service doesn't allow me to add multiple Jenkins's instance, so I add the others add the Webhook as shown below. It also works fine.

I wanted to include a picture here but my id doesn't have the right to do so.

Jirong Hu
  • 2,315
  • 8
  • 40
  • 63