Schedule Jenkins job using Jenkins Rest API

只愿长相守 提交于 2021-02-18 12:06:04

问题


I have a Jenkins job which is scheduled for a specific time. I want to modify that timing programmatically.

I tried to modify the build by installing Schedule Build plugin and modify it using http://jenkins_url/job/jobname/build?delay=3344sec. But this will put the job in quiet period which holds the java thread. I'm looking to modify the Schedule entry without putting it to quiet period.


回答1:


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

  1. programmatically retrieve the job configuration in XML format, then
  2. modify the scheduling time in that configuration (see below)
  3. 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.



回答2:


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(); 
    }
}


来源:https://stackoverflow.com/questions/60186314/schedule-jenkins-job-using-jenkins-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!