How to schedule a job in Jenkins

Jenkins is an open-source automation server that allows developers and DevOps teams to automate the building, testing, and deployment of software projects. It provides a web-based interface to create and manage jobs, which are individual tasks or processes that can be scheduled and run automatically.

Job scheduling in Jenkins allows us to define when and how often a particular job should run. This is useful for automating repetitive tasks, such as running tests, generating reports, or deploying applications at specific intervals or events. By scheduling jobs in Jenkins, we can save time and effort by eliminating the need for manual execution.

Scheduling a job

  1. Install Jenkins: First, you need to install Jenkins on your server or local machine. You can follow this Answer for installation.

  2. Configure Jenkins: To configure Jenkins, you can follow this Answer.

  3. Access Jenkins Web Interface: Open a web browser and navigate to the Jenkins web interface. By default, Jenkins runs on http://localhost:8080. Enter this address in your browser's address bar, and you should see the Jenkins dashboard.

widget
  1. Create a new job: On the Jenkins dashboard, click on the "New Item" link to create a new job. Give your job a descriptive name that reflects its purpose. Select the appropriate job type based on your project requirements. For example, you can choose a "Freestyle project" for simple build and test tasks or a "Pipeline" for more complex workflows.

widget
  1. Configure job Settings: In the job configuration page, you'll find various settings to customize your job. Start by providing a meaningful description for the job to make it easier for others to understand its purpose. Next, configure the source code management options, such as specifying the repository URL and credentials if applicable.

widget
  1. Set build triggers: The next step is to define the triggers that will initiate the job execution. In the job configuration page, locate the "Build Triggers" section and select the appropriate trigger option based on your scheduling requirements.

  • Build periodically: To schedule the job at specific intervals, select the "Build periodically" option. In the "Schedule" field, you can define the schedule using the following cron syntax.

MINUTE HOUR DAY MONTH DAY_OF_WEEK
  • Minute: Values range from 0 to 59, representing the minute of the hour.

  • Hour: Values range from 0 to 23, representing the hour of the day.

  • Day of Month: Values range from 1 to 31, representing the day of the month.

  • Month: Values range from 1 to 12, representing the month of the year.

  • Day of the Week: Values range from 0 to 6, where 0 and 7 represent Sunday, and 1 to 6 represent Monday to Saturday.

For example, * * * * * runs the job every minute, H H * * * runs the job every hour, or you can use a custom schedule like 15 9 * * 1-5 to run the job at 9:15 AM from Monday to Friday.

Here are the special expressions you can use in the scheduling fields:

In addition to the custom expressions, Jenkins provides several predefined values that can be used as shortcuts:

  • @yearly or @annually - Runs the job once a year.

  • @monthly - Runs the job once a month.

  • @weekly - Runs the job once a week.

  • @daily - Runs the job once a day.

  • @hourly - Runs the job once an hour.

  • Poll SCM: If you want Jenkins to periodically check for changes in the source code repository and trigger the job accordingly, select the "Poll SCM" option. Specify the polling interval, which determines how often Jenkins checks for changes.

  • Other triggers: Jenkins offers various other triggers, such as triggering the job when a specific event occurs (e.g., code commit, new artifact release) or when another job completes. Choose the appropriate trigger based on your requirements.

widget
  1. Configure build steps: After setting up the triggers, define the build steps or actions that should be performed when the job runs. This could include compiling code, running tests, deploying artifacts, or executing custom scripts. Jenkins provides a wide range of plugins and integrations to support different build and deployment processes.

widget
  1. Save the job configuration: Once you have configured the job settings and scheduling, click on the "Save" or "Apply" button to save the job configuration.

  2. View and manage jobs: After saving the job configuration, you'll be redirected to the Jenkins dashboard. You can now view the list of jobs and their status. The scheduled job will run based on the specified schedule, and the job status and logs will be available on the job page.

widget

Copyright ©2024 Educative, Inc. All rights reserved