Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schedule Jenkins job using Jenkins Rest API

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.

like image 348
user2439278 Avatar asked Feb 12 '20 10:02

user2439278


People also ask

How do I access the REST API from a Jenkins project?

You can find the URL to call to interact with the Jenkins system with the REST API link in the bottom right hand corner of each screen: In this example, we want to trigger the build of a Jenkins project, so we open the project and find the REST API link points us to a URL like http://jenkinsserver/jenkins/job/Run%20a%20script/api/.

How do I create a schedule in Jenkins?

Jenkins schedule format Under Build Triggers - Build periodically - Schedule you can create a schedule (or multiple schedules) for Jenkins to build periodically or on a specific date/time. It might be tests that should be run periodically (every morning for example) or a DB clean up Jenkins job or any other Jenkins job.

How to set up a Jenkins server?

A Jenkins server – for this tutorial, we'll use the official Docker image to set up a Jenkins server Login in to your Jenkins application and click on Create a Job. The type of job doesn't matter. It can be Freestyle, Maven, etc. All that matters is how we configure the scheduler. For this article, we'll go with Pipeline.

How do I trigger a Jenkins job?

There are many ways to trigger a Jenkins job: manually (of course), using a Cron Expression, hooks, watching other projects, etc. Let's start with triggering a job via the Cron utility. We'll choose Build periodically and for the Schedule field use the following expression:


1 Answers

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.
like image 157
Alex O Avatar answered Oct 28 '22 21:10

Alex O