Monday 10 May 2021

How to schedule a job in Jenkins pipeline? How to run automation suite everyday with auto trigger scheduler?


1. What is Jenkins?

As defined in Jenkins docs, "Jenkins is a self-contained, open source automation server that can be used to automate all sorts of tasks related to building, testing, and delivering or deploying software."

We use it to create jobs or tasks which will help us to achieve CI and CD in our project.

The best thing :  it is "FREE"

What to Use Jenkins for and When to Use It

Use Jenkins to automate your development workflow so you can focus on the work that matters most. Jenkins is commonly used for:

  • Building projects
  • Run tests to detect bugs and other issues as soon as they are introduced
  • Static code analysis
  • Deployment

Execute repetitive tasks, save time, and optimize your development process with Jenkins.


Pre-requisite:


  • Why we need to schedule Jenkins job for automation test suite?

here are some reasons why we need Jenkins job scheduling for automation test suites:

  1. Automating repetitive tasks: Jenkins job scheduling helps automate the execution of repetitive test suites at regular intervals, ensuring that test cases are executed consistently and reducing the time and effort required to execute them manually.

  2. Early detection of issues: Scheduling tests to run at regular intervals or specific times can help detect issues or bugs early in the development process, enabling teams to address them before they become major problems.

  3. Efficient resource utilization: Jenkins allows for parallel test execution, enabling multiple tests to run simultaneously on different machines or environments, making efficient use of available resources and reducing overall test execution time.

  4. Integration with other tools: Jenkins can be integrated with other tools like JIRA, Git, and Selenium, allowing for seamless automation of test cases and providing real-time feedback to developers on the status of tests.

  5. Customization: Jenkins job scheduling provides flexibility in scheduling tests based on various factors such as environment, code changes, and other triggers, enabling teams to customize their test automation workflows based on their specific needs and requirements.

Overall, Jenkins job scheduling is an essential component of test automation that helps improve efficiency, reliability, and scalability of test automation workflows.


The steps for schedule jobs at Jenkins:


First, let’s look at the Jenkins job scheduling configuration. It looks a lot like Linux’s cron syntax. 

  1. Click on the "Configure" option for the job.
  2. scroll down to "Build Triggers" .
  3. Click on the "Build Periodically" CheckBox.
  4. Add scheduled time in the Schedule field:: This will take cron expression as below:



  1. MINUTES Minutes in one hour (0-59)
  2. HOURS Hours in one day (0-23)
  3. DAYMONTH Day in a month (1-31)
  4. MONTH Month in a year (1-12)
  5. DAYWEEK Day of the week (0-7) where 0 and 7 are sunday

 How to integrate Maven project with Jenkins?

Maven & Jenkins Integration

  • So we need to input value for 5 *, and each * represents one parameter in the sequence as mentioned above.
  • If I want to run my job daily at 11 pm then the expression will be:    0 23 * * *
IMAGE REFERENCE:




  • If we want to run the job 23:45 (11:45 PM) every Saturday: 45 23 * * 6


  • To schedule every hour, then you can put: 0 * * * *

Then your job will be executed every hour (07:00, 08:00, 09:00 and so on)

*********************************************

  • Each line of a crontab file represents a job, and looks like this:

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │                                   7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * <command to execute>


Non-standard predefined scheduling definitions

Some cron implementations[9] support the following non-standard macros:

EntryDescriptionEquivalent to
@yearly (or @annually)Run once a year at midnight of 1 January0 0 1 1 *
@monthlyRun once a month at midnight of the first day of the month0 0 1 * *
@weeklyRun once a week at midnight on Sunday morning0 0 * * 0
@daily (or @midnight)Run once a day at midnight0 0 * * *
@hourlyRun once an hour at the beginning of the hour0 * * * *
@rebootRun at startupN/A

How to create parameterized job in Jenkins? VERY IMP

Jenkins builds periodically - multiple schedules


You can apply even more than one schedule for a Jenkins job.


For example, you might have a regular schedule to build Jenkins periodically, let’s say we want to build periodically every work day’s like this 


30 8 * * 1-5

Let’s assume we need to build Jenkins periodically every week before the weekend starts. 


30 16 * * 5

Now we can easily combine these two Jenkins schedules


30 8 * * 1-5 
30 16 * * 5

Jenkins will build every work day at 8:30am and every week on Friday at 4:30pm


Jenkins Build Periodically - two Jenkins schedules


Once you are done with setting up nightly job, do trigger email notification for the build logs and execution report

TRIGGER EMAIL NOTIFICATION WITH JENKINS

Learn (API-Microservice)Testing+(CoreJava+UI)-SDET with Self Paced Videos and one LIVE Doubt Session

TRANING VIDEOS AVAILABLE with Live Doubt Session @4500/-(course-1 below,API TRaining Videos With ClassNotes and Coding Set) and 6500/- (API+UI, both course-1 & 2 below) Check Training Page for Course Content or reach out @whatsapp +91-9619094122

Entire course content can be found below:  COURSE CONTENT
Suggested Post:

ALL ABOUT AUTOMATION FRAMEWORK DESIGN

Java Interview question Set-1:

SET-01 1-10

Java Interview question Set-2:

SET-02 10-20

Java Interview question Set-3:

SET-03 21-30

*************************************************

SeleniumWebdriver Automation Testing Interview Questions:

API Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Rest-API

 
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes

 
Docker Interview Question Set
https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

 
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux

 
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign


Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java


GIT Interview Question Set:
https://automationreinvented.blogspot.com/search/label/GIT


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions


1 comment:

All Time Popular Posts

Most Featured Post

Scenario Based Frequently Asked Interview Q&A on TestNG

  🔺 LinkedIn: https://www.linkedin.com/in/sidharth-shukla-77b53145/ 🔺 Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl Welcome to our compr...