Mastering Jenkins Job Scheduling: A Dive into Dashboard, Declarative, and Scripted Methods

Hello everyone! 👋

In the fast-paced world of software development, automating your workflows is essential. Jenkins, an open-source automation server, empowers developers to streamline their processes through Jenkins Pipelines.

Through this blog, I aim to share insights into scheduling your Jenkins jobs at specific times ! 🙂
We’ll explore three methods for scheduling Jenkins jobs: through the Jenkins dashboard, using declarative syntax, and employing scripted syntax.

1. Scheduling Jobs via Jenkins Dashboard:

Jenkins provides an intuitive web-based dashboard that allows users to schedule jobs easily.
Follow these steps:

  • Login to Jenkins: Navigate to your Jenkins instance and log in.

  • Access Job Configuration: Locate the job you want to schedule and click on it.

  • Configure Build Triggers: Inside the job configuration, find the “Build Triggers” section. Check the box next to “Build periodically” to enable scheduling.

  • Cron Syntax: Use the Cron syntax to define the schedule. For example, to run a job every day at 7:20AM, use 20 7 * * *.

  • Save Changes: Save your changes, and the job will now run based on the specified schedule.

  • The cron syntax for timing in Jenkins follows the standard cron format with five fields, representing minutes, hours, days of the month, months, and days of the week. The syntax is as follows:

* * * * *
| | | | |
| | | | +----- Day of the week (0 - 6) (Sunday to Saturday)
| | | +------- Month (1 - 12)
| | +--------- Day of the month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)

For example, the cron expression 0 2 * * * means:

  • Minute: 0 (at the start of the hour)

  • Hour: 2 (2 AM)

  • Every day of the month

  • Every month

  • Every day of the week

This expression schedules a Jenkins job to run every day at 2 AM. You can customize the values in each field to define your desired schedule.
Always be mindful of the time zone settings in your Jenkins environment and adjust your cron expressions accordingly to achieve the desired scheduling behavior.

2. Declarative Syntax:

Declarative syntax in Jenkins allows for defining pipeline jobs using a structured, human-readable format. Here’s a simple example:

// groovyFile code

pipeline {
    agent any
    triggers {
        cron('0 2 * * *') // Schedule: Every day at 2 AM
    }
    stages {
        stage('Build') {
            steps {
                // Your build steps here
            }
        }
        // Additional stages as needed
    }
}

Explanation:

  • The triggers block uses the cron directive to set the schedule.

  • Adjust the cron expression to meet your desired schedule.

3. Scripted Syntax:

Scripted syntax allows for more flexibility and customisation in Jenkins pipelines. Here’s a scripted syntax example:

// groovyFile Code
node {
    // Define when the job should run
    def schedule = cron('0 2 * * *') // Every day at 2 AM
// Check if it's time to run
    if (currentBuild.getBuildVariables()['JENKINS_TIMER'] == null) {
        currentBuild.setBuildVariables([JENKINS_TIMER: schedule])
    }
def shouldRun = currentBuild.getBuildVariables()['JENKINS_TIMER'] == schedule
if (shouldRun) {
        // Your build steps here
    } else {
        echo "Job not scheduled to run at this time."
    }
}

Explanation:

  • This script checks the current time against the defined schedule before executing the build steps.

  • The JENKINS_TIMER variable helps track when the job is scheduled to run.

Conclusion:

Whether you prefer the simplicity of the Jenkins dashboard, the structured declarative syntax, or the flexibility of scripted syntax, Jenkins offers multiple ways to schedule jobs. Choose the method that best fits your project’s requirements and enjoy the benefits of automated and scheduled builds.

Additional Resources:

Feel free to replace the placeholder links with actual URLs relevant to your audience. Adapt the text to suit your writing style and ensure clarity for your readers.

Happy blogging! 📘🚀