In continuous automation suite execution, it is important to run test cases every day and at certain time intervals to ensure that the product is working seamlessly in production.If any cases fail, the QA is alerted and the issue is fixed before a customer encounters the issue.
Cron Patterns helps to achieve automation by scheduling builds in Jenkins according to the requirement of the frequency with which the certain case should be triggered.
When creating a job in Jenkins,we can set the frequency according to the job that will be triggered.
Before we understand how this works, let’s first see what are the most common parameters used –
Minutes – Minutes in an hour (0-59)
Hours – Hours in a day (0-23)
DayMonth – Days in a month (1-31)
Month – Months in a year (1-12)
DayWeek – Day of week (0-7), 0 and 7 are Sunday
The most common Cron Patterns used for scheduling jobs are explained below –
1) * * * * *
This will run every minute, all the time: When we wish to run jobs every single minute, we use this pattern
2) 0 * * * *
This will run at minute zero, every hour (at hourly interval): The number of minutes can be changed accordingly by changing 0 to how many ever minutes you wish to schedule the build
3) 15 * * * *
This will also run hourly but run at minute 15 instead – it would run like this – (00:15, 01:15, 02:15):
4) 30 2 * * *
This will run once a day, at 2:30am: we can change the hours accordingly to change the timing at which the build will be triggered.
5) */10 * * * *
Division operator is also used when we want the build to be run every 10 minutes:
This is usually the most common scheduling pattern used as it tests the stability of the product by running the job very frequently and in a way mimics the customer usage of the product too.
6) 0 5-10 * * *
A range can be specified using the dash (-). This will be repeated every hour between 5:00 and 10:00 a.m.
7) 0 0 2 * *
Sometimes we have requirements where we want the job to be triggered only every month – this is where the above cron pattern is used – This will run once a month, on the second day of the month at midnight (January 2nd, 12:00 am, February 2nd, 12:00 am):
8) 0 * * * 1
When the job is required to be triggered only on certain days of the week, we use the above pattern – Which will run every hour on Mondays only.
We can replace this with other numbers between 0 and 7 to run on any day of the week accordingly.
9) 0,10,20 * * * *
We can use multiple numbers separated by commas for the minutes – This will run three times every hour, at minutes 0, 10 and 20:
This is used when we are trying to reproduce a customer issue where a customer is facing inconsistency in the issue,but the issue is reproducible one out of 3 times; to test this scenario we make use of this pattern.
Thus, this is how builds in Jenkins are scheduled to run accordingly with the use of simple logic of CRON expressions.