Schedule Jenkins job using Jenkins Rest API
You can use the Build Triggers -> Build periodically
job configuration option. Use that to specify the exact time for starting a new build.
If you need to change that time, use the Jenkins REST API to...
- programmatically retrieve the job configuration in XML format, then
- modify the scheduling time in that configuration (see below)
- re-post the new job configuration
In bash, this can be done with a one-liner (using curl
and sed
) to modify the XML section below (the example schedules a run for noon, Feb 29):
[...]<triggers><hudson.triggers.TimerTrigger><spec>00 12 29 02 * </spec></hudson.triggers.TimerTrigger></triggers>[...]
Note:
- as a plus you wouldn't depend on any supplementary plugins
- caveat: you cannot specify a year in the schedule -- so if you need to schedule builds more than one year in advance then you need some magic on top.
I can't get it to work, but the source code for the plugin references a "schedule" url action and a "date" param.
I tried something like:
http://localhost:8080/job/jobname/job/develop/schedule?date=2020-02-20
Which it didn't reject but I can't see a build.
below is the source code of the action performed when the button is pressed to schedule:
var newRequest = function() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else { return new ActiveXObject("Microsoft.XMLHTTP"); }}var sumbitScheduleRequest = function(absoluteUrl, quietPeriodInSeconds, isJobParameterized){ if(isJobParameterized){ // if job has parameters, redirect to build page, so user can set parameters window.location = absoluteUrl + "build?delay=" + quietPeriodInSeconds + "sec"; }else{ // if job has NO parameters, submit build directly var csrfCrumb; var csrfRequest = newRequest(); csrfRequest.onreadystatechange = function() { if (csrfRequest.readyState === 4) { if (csrfRequest.status === 200 || csrfRequest.status === 201) { csrfCrumb = JSON.parse(csrfRequest.responseText); } else { // csrf might be deactivated } // do the actual submit var xmlhttp = newRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState === 4) { if (xmlhttp.status === 200 || xmlhttp.status === 201) { window.location = absoluteUrl; return false; } else { window.location = absoluteUrl; return false; } } }; xmlhttp.open("POST", absoluteUrl + "build?delay=" + quietPeriodInSeconds + "sec", true); if (csrfCrumb) { xmlhttp.setRequestHeader(csrfCrumb.crumbRequestField, csrfCrumb.crumb) } xmlhttp.send(); } }; csrfRequest.open('GET', rootURL + '/crumbIssuer/api/json', false); csrfRequest.send(); }}