3

I would like to schedule a Jenkins job but only when new commits has been made to SCM. Or preferably even more flexible case: how to run a scheduled job but only if another job has been (successfully) run.

One typical use case: deploy the application in night time, but only when changes has been made.

digitalfootmark
  • 577
  • 1
  • 5
  • 19
  • Have also a look at: http://stackoverflow.com/questions/10014252/jenkins-ci-how-to-trigger-builds-on-svn-commit and http://www.andyfrench.info/2015/03/automatically-triggering-jenkins-build.html – code4fun Nov 03 '15 at 14:15
  • Thanks for the links but the point was NOT to run the job immediately after the commit but sometime later, e.g. once per day (or night) – digitalfootmark Nov 03 '15 at 14:40

1 Answers1

6

What you describe is somewhat the default behaviour. And there's two ways to achieve it. In the job config you have the setting 'build trigger'.

  • Choose the option "query source code management". you then give the timing in the same manner as you configure a cron job. E.g. for building daily at 02:00 am: 0 2 * * * No build will be triggered when there hasn't been any new commit since the last build. Jenkins remembers it. This approach is good for very long-running builds and tests.

  • Install a build hook in your SCM and use it to trigger a build when a new commit is being made. Then choose in the 'build trigger' option the "start build externally (by script)". You have to use a build token in that case so that the script can authenticate against Jenkins API. This approach is good if you want to give relatively quick feedback to the committer about the build results (e.g. any errors or build failures which crept in).

planetmaker
  • 5,884
  • 3
  • 28
  • 37
  • 1
    Silly me.. I didn't check that the "Poll SCM" option has also a scheduled time for polling(!). – digitalfootmark Nov 03 '15 at 14:50
  • Later versions of Jenkins also have a “do not trigger build on commit notification” option that is hidden in the SCM configuration which you can use to have the commit notifications of your repository, well, not trigger the build. :) – Bombe Nov 25 '16 at 22:33