How to schedule a build in Jenkins?
Asked Answered
A

10

80

How do I schedule a Jenkins build such that it would be able to build only at specific hours every day?

For example to start at 4 PM

0 16 1-7 * *

I understand that as, "at 0 minutes, at 4 o'clock PM, from Monday to Sunday, every month", however it builds every minute :(

I would be grateful for any advice. Thanks!

Atomize answered 9/8, 2011 at 17:26 Comment(2)
Where does one actually enter this to schedule a job?Fecula
In config of your project section "Build triggers" choose "build periodically"Rufusrug
M
109

Update: please read the other answers and comments as they contain more info (e.g., hash functions) that I did not know when I first answered this question.

According to Jenkins' own help (the "?" button) for the schedule task, 5 fields are specified:

This field follows the syntax of cron (with minor differences). Specifically, each line consists of 5 fields separated by TAB or whitespace: MINUTE HOUR DOM MONTH DOW

I just tried to get a job to launch at 4:42PM (my approximate local time) and it worked with the following, though it took about 30 extra seconds:

42 16 * * *

If you want multiple times, I think the following should work:

0 16,18,20,22 * * *

for 4, 6, 8, and 10 o'clock PM every day.

Maldonado answered 9/8, 2011 at 23:50 Comment(2)
Right, too much stars. My bad in question. I will fix my topic.Atomize
Hi Michal, Are still having trouble with the scheduling? You accepted my answer, but your comment only concerned fixing the question, which I see you've done. Thanks.Maldonado
H
86

In the job configuration one can define various build triggers. With periodically build you can schedule the build by defining the date or day of the week and the time to execute the build.

The format is as follows:

MINUTE (0-59), HOUR (0-23), DAY (1-31), MONTH (1-12), DAY OF THE WEEK (0-6)

The letter H, representing the word Hash can be inserted instead of any of the values, it will calculate the parameter based on the hash code of your project name, this is so that if you are building several projects on your build machine at the same time, lets say midnight each day, they do not all start there build execution at the same time, each project starts its execution at a different minute depending on its hash code. You can also specify the value to be between numbers, i.e. H(0,30) will return the hash code of the project where the possible hashes are 0-30

Examples:

start build daily at 08:30 in the morning, Monday - Friday:

  • 30 08 * * 1-5

weekday daily build twice a day, at lunchtime 12:00 and midnight 00:00, Sunday to Thursday:

  • 00 0,12 * * 0-4

start build daily in the late afternoon between 4:00 p.m. - 4:59 p.m. or 16:00 -16:59 depending on the projects hash:

  • H 16 * * 1-5

start build at midnight:

  • @midnight

or start build at midnight, every Saturday:

  • 59 23 * * 6

every first of every month between 2:00 a.m. - 02:30 a.m. :

  • H(0-30) 02 01 * *

more on CRON expressions

Henbit answered 10/6, 2015 at 9:43 Comment(4)
I think start build daily in the late afternoon between 4:00 p.m. - 4:59 p.m. or 16:00 -16:59 depending on the projects hash: should be H 16 * * * (Not H 16 * * 1-5)Kaczmarek
Thanks but are you sure 1-6 is monday to friday? Isn't it 1-5 ? etc.Salaried
If 1-6 is Mon - Sat, then 0-5 should be Sun - Fri. +1 for so many useful examples.Povertystricken
Correction:H(0,30) 02 01 * * should be H(0-30) 02 01 * *Walcott
K
12

This example is everyday, once around 9am and once around 5pm. (edited per comments).

H 9,17 * * * 
Kremlin answered 8/4, 2014 at 19:35 Comment(3)
Not exactly. That will run once between 9:00am and 9:59am, plus once between 5:00pm and 5:59pm.Isleana
To be more precise: That job will start once between 9:00am and 9:59am, and start once between 5:00pm and 5:59pm. The job will not be terminated if it takes longer.Antalya
Note that "will run once between 0900 and 0959": the H stands for "hash" - it's a hash based on the project, so that this project will ALWAYS run on the same minute in the 9th hour and the 17th hour. If the hash algorithm generates the number "16" for your project, then your project job will always start at 0916 and 1716.Tiannatiara
S
10

The steps for schedule jobs in Jenkins:

  1. click on "Configure" of the job requirement
  2. scroll down to "Build Triggers" - subtitle
  3. Click on the checkBox of Build periodically
  4. Add time schedule in the Schedule field, for example, @midnight

enter image description here

Note: under the schedule field, can see the last and the next date-time run.

Jenkins also supports predefined aliases to schedule build:

@hourly, @daily, @weekly, @monthly, @midnight

@hourly --> Build every hour at the beginning of the hour --> 0 * * * *

@daily, @midnight --> Build every day at midnight --> 0 0 * * *

@weekly --> Build every week at midnight on Sunday morning --> 0 0 * * 0

@monthly --> Build every month at midnight of the first day of the month --> 0 0 1 * *

Stipendiary answered 15/1, 2020 at 14:47 Comment(0)
E
4

To build once a day between say 4PM to 6PM you can use

H H(15-17) * * *

Emporium answered 20/8, 2014 at 9:41 Comment(0)
M
3
H H(5-21)/2 * * 1-5

MON-FRI build every 2 hour between

Mucky answered 2/12, 2016 at 17:0 Comment(0)
L
2

That appears to be a cron expression. Note that your example builds only on the first to seventh of every month, at 16:00. You likely have some sort of other error, or Jenkins uses non-standard CRON expressions.

Lull answered 9/8, 2011 at 17:33 Comment(0)
T
1

Jenkins uses Cron Expressions.

You can simply schedule hourly builds by just typing@hourly.

Tonina answered 12/2, 2016 at 16:25 Comment(0)
N
1

In Jenkins , we have the format is as:

Minute(0-59) Hour(0-23) Day(1-7) Month(1-12) Day of the Week

Natividad answered 9/6, 2016 at 9:41 Comment(0)
N
0

Try this for 4 PM from Monday to Sunday

0 16 * * *

You can check the description messgage displayed while you configuring in "Build periodically' under Jenkins. (Refer the screenshot given below)

"Would last have run at Sunday, November 17, 2019 4:00:05 PM IST; would next run at Monday, November 18, 2019 4:00:05 PM IST."

Screenshot

enter image description here

The seconds in the time " Monday, November 18, 2019 4:00:05 PM IST" refers to our current system seconds.

Nucleus answered 18/11, 2019 at 5:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.