0
0
Jenkinsdevops~5 mins

Triggers directive in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want Jenkins to start a job automatically without clicking a button. The triggers directive helps Jenkins know when to start a job by itself based on events or schedules.
When you want a job to run every day at a specific time without manual start.
When you want Jenkins to start a build after a code change is pushed to a repository.
When you want to trigger a job after another job finishes successfully.
When you want to run a job periodically to check system health or run tests.
When you want to start a job based on external events like webhooks.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  triggers {
    cron('H 2 * * *')
    pollSCM('H/15 * * * *')
  }
  stages {
    stage('Example') {
      steps {
        echo 'Job triggered automatically'
      }
    }
  }
}

The triggers block inside the pipeline tells Jenkins when to start the job automatically.

cron('H 2 * * *') runs the job once every day at 2 AM.

pollSCM('H/15 * * * *') checks the source code repository every 15 minutes and triggers the job if there are changes.

Commands
This command updates the Jenkins job with the Jenkinsfile that includes the triggers directive so Jenkins knows when to start the job automatically.
Terminal
jenkins-jobs --conf jenkins.ini update example-job
Expected OutputExpected
Job updated successfully
This command manually triggers the job to test that the Jenkinsfile and triggers are working correctly.
Terminal
jenkins-cli build example-job
Expected OutputExpected
Started build #1
This command shows the job configuration including the triggers to verify they are set correctly.
Terminal
jenkins-cli get-job example-job
Expected OutputExpected
<triggers> <hudson.triggers.TimerTrigger> <spec>H 2 * * *</spec> </hudson.triggers.TimerTrigger> <hudson.triggers.SCMTrigger> <spec>H/15 * * * *</spec> </hudson.triggers.SCMTrigger> </triggers>
Key Concept

If you remember nothing else from this pattern, remember: the triggers directive tells Jenkins when to start a job automatically without manual intervention.

Common Mistakes
Writing the cron schedule incorrectly in the triggers block.
Jenkins will not trigger the job at the expected times or may fail to parse the schedule.
Use valid cron syntax and test schedules with online cron validators before adding them.
Forgetting to update the Jenkins job after changing the Jenkinsfile triggers.
Jenkins will continue using the old triggers and not the new ones.
Always run the update command or reload the job configuration after editing the Jenkinsfile.
Using both cron and pollSCM triggers without understanding their differences.
This can cause unexpected multiple job runs or missed triggers.
Choose the trigger that fits your use case or configure them carefully to avoid overlap.
Summary
The triggers directive in Jenkinsfile automates job start based on schedules or events.
Use cron to schedule jobs at specific times and pollSCM to trigger on source code changes.
Always update the Jenkins job configuration after changing triggers to apply them.