Create/Configure Jenkins Cron Job
What are Jenkins cron jobs? Why do you need them?
As the name suggests, "cron job" it allows you to set up a schedule to run pipelines automatically. You don't need to manually click the "Build" button every time. Set a time for this job, and it will run those pipelines at that specific time with the frequency you choose, whether daily, weekly, monthly, or even yearly. So basically, it automatically triggers the pipeline builds without any manual interaction.
When should you use a cron job?
Scheduled Task:
When you need to run the same script repeatedly at specific times or intervals, such as daily, weekly or monthly.
Monitoring:
When you are working on some monitoring tasks and need to generate reports that runs frequently, like monitor system resources, logs or network activity.
System Maintenance:
While performing routine system maintenance, like updating software or running security scans.
Backup & Recovery:
When you want to automate backups and recovery processes.
Creating a Jenkins Cron Job with an Example
Here, we will be creating a Jenkins cron job for a scheduled task that generates two types of report of “azure cost”:
Daily Report:
The daily report script(pipeline) should run every day at 2 AM IST. This pipeline runs and generates a daily azure cost report and then stores it on the azure container.
Monthly Report:
The monthly report script(pipeline) should run on every 6th date of the month at 2:30 AM IST. This pipeline runs and generates a monthly azure cost report and then stores it on the azure container.
So basically, one Jenkins job that has two cron setups.
Step 1: Create a Jenkins job
Click on the job you want to schedule (or create a new one)
Scroll down to the Build Triggers section.
Check the box next to Build periodically with parameters
Step 2: Configure Cron Schedule
In the Schedule field, enter your desired cron expression (e.g., * * * * * for every minute). To understand “cron expressions” in simpler way, you can use this link: Crontab.guru - The cron schedule expression generator
In our case, our Jenkins job is a parametrized job. The parameter “Report_Type” accepts two values “Daily” and “Monthly” for generating reports accordingly.
So now, in the Schedule field we write two cron expression as follows:
For Daily Report(runs pipeline daily at 2am):
0 2 * %Report_Type=Daily
For Monthly Report(runs pipeline on every 6th at 2:30am):
30 2 6 %Report_Type=Monthly
Step 3: Save Changes
Declarative Pipeline Configuration Example:
The above mentioned cron job can also be set up using a Jenkinsfile by adding these code in it:
Verify Cron Job
Go to the Jenkins dashboard.
Click on the job.
Click on Build History
Verify the scheduled build is triggered at the specified time.
Kudos!! By following the above mentioned steps, you should now have a Jenkins cron job set up and running according to your schedule.
Reference Docs:
Parameterized Scheduler | Jenkins plugin
Subscribe to my newsletter
Read articles from Yogendra Singh Rathore directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Yogendra Singh Rathore
Yogendra Singh Rathore
Hey tech buddies, Yogendra Here !! I love to learn and share stuff about DevOps. Feel free to connect and explore how my skills can align with your needs.