Swapnil Kotwal
Swapnil Kotwal

Reputation: 5730

Run Jenkins job only once on changes pushed on github

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?

Upvotes: 1

Views: 1674

Answers (2)

Paul Hicks
Paul Hicks

Reputation: 14009

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.

Upvotes: 1

luboskrnac
luboskrnac

Reputation: 24581

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.

Upvotes: 1

Related Questions