1

I followed this answer to set up a Jenkins job, and it's working fine.

I have scheduled a Job on github master commit push as

Poll SCM : * * * * *

But, it continuously starts a build Job each minute.

How can I restrict this so that it runs only once per commit push?

Community
  • 1
  • 1
Swapnil Kotwal
  • 5,418
  • 6
  • 48
  • 92
  • You should consider using the solution in the [preferred answer](http://stackoverflow.com/a/9921495/3195526) to the same question. It's a better solution. – Paul Hicks Mar 08 '15 at 22:46

2 Answers2

1

This approach is actually polling. That means Jenkins is scanning every minute if there aren't any changes in GitHub repository.

If you want true Push from Github to your Jenkins You need to Integrate Github WebHooks with Jenkins. I wrote a blog post on this subject. Scroll to section 2: "Jenkins - Github Integration"

If you are just playing around or using it for your personal open source project, you may want to look into Jenkins alternatives like Drone.io or Codeship.io. These services are generally free for open source and can configure Github Webhooks in few clicks. But they are not suitable for complicated enterprise builds.

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • I have followed steps you have mentioned but I'm getting invalid host error. It may be due to Jenkins hosted on locally and I don't have Public url ? – Swapnil Kotwal Mar 09 '15 at 07:53
  • Yes. First statement in section 2 is: "Blocking requirement here is to have Jenkins server accessible from web. If you can’t for whatever reason, you must stick with polling the source control in Jenkins." – luboskrnac Mar 09 '15 at 07:58
  • I also referred http://blog.cloudbees.com/2012/01/better-integration-between-jenkins-and.html but no better luck? Is that "invalid host" is due to localhost ? – Swapnil Kotwal Mar 09 '15 at 08:01
1

There are several options. The two I've used with most success are:

  1. Use a git commit hook to call the Jenkins rest API to start the job. The simple approach is to call the job's build API call directly (something like http://jenkinsmachine:8080/job/your-jobs-name/build), but that hardcodes the job name and branch into the git hook script. A more flexible approach is to use the Git plugin's own rest mini-API, as described by Kohsuke in his blog.
  2. Use something like the Throttle Concurrent Builds plugin or a creative use of slaves nodes and executors to limit the number of cuncurrent builds of that job.

The first option is much preferred, but there are times when rest access to the jenkins machine from the git machine is not available, so the second option can be used in those circumstances.

Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
  • Hi @Paul :- I'm pretty much new with Jenkins. However Throttle Concurrent build solved my problem. Could you please point me to some guideline to "use git commit hook to call the Jenkins rest API to start the job" – Swapnil Kotwal Mar 09 '15 at 09:30
  • Updated answer with link to useful blog article. – Paul Hicks Mar 09 '15 at 21:03
  • Just noticed that lkmac linked to the same blog post :) Obviously a good article! – Paul Hicks Mar 10 '15 at 09:02
  • I'm confused with this blog, as it says to just execute the 'curl' command, How can I do this ? using another Job ? – Swapnil Kotwal Mar 10 '15 at 10:00
  • The call to curl is from the git machine. You put it in your hook script: usually the `post-receive` or `post-commit` hook, depending on which repo you want to use to inform Jenkins that a change has happened in git. This is the step that changes the process from being polling-based to push-based. – Paul Hicks Mar 10 '15 at 10:18
  • Added another link. Check out the link to the git plugin, in the amended answer. – Paul Hicks Mar 10 '15 at 10:34