Search code examples
githubjenkinsjenkins-pluginsjenkins-cli

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?


Solution

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