Bird
Raised Fist0
Jenkinsdevops~7 mins

Build triggers (poll SCM, webhook, timer) in Jenkins - Commands & Configuration

Choose your learning style10 modes available
Introduction
Build triggers in Jenkins automatically start jobs based on events like code changes, scheduled times, or external signals. This helps keep your software up-to-date without manual intervention.
When you want Jenkins to check your code repository regularly for changes and start a build if there are any.
When you want Jenkins to start a build immediately after someone pushes code to your repository using a webhook.
When you want Jenkins to run builds at specific times, like every night or every hour.
When you want to automate testing and deployment without manually clicking buttons.
When you want to save time by automating repetitive build tasks triggered by events.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  triggers {
    pollSCM('H/15 * * * *')
    // This triggers polling every 15 minutes
    // For webhook, Jenkins listens automatically if configured in repo
    cron('H 2 * * *')
    // This triggers build daily at 2 AM
  }
  stages {
    stage('Build') {
      steps {
        echo 'Building the project...'
      }
    }
  }
}

The triggers block defines when Jenkins should start the build automatically.

  • pollSCM('H/15 * * * *') tells Jenkins to check the source code repository every 15 minutes for changes.
  • cron('H 2 * * *') schedules a build every day at 2 AM.
  • Webhook triggers are configured outside Jenkins but Jenkins listens for them to start builds immediately when code is pushed.
Commands
Manually triggers the Jenkins pipeline named 'my-pipeline' to test if the job runs correctly before setting triggers.
Terminal
jenkins-cli build my-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building the project... [Pipeline] End of Pipeline Finished: SUCCESS
Retrieves the current configuration of the 'my-pipeline' job to verify trigger settings.
Terminal
jenkins-cli get-job my-pipeline
Expected OutputExpected
<?xml version="1.1" encoding="UTF-8"?> <flow-definition plugin="workflow-job@2.40"> <triggers> <hudson.triggers.SCMTrigger> <spec>H/15 * * * *</spec> </hudson.triggers.SCMTrigger> <hudson.triggers.TimerTrigger> <spec>H 2 * * *</spec> </hudson.triggers.TimerTrigger> </triggers> </flow-definition>
Simulates a webhook POST request to Jenkins to trigger the 'my-pipeline' job immediately when code is pushed.
Terminal
curl -X POST http://jenkins.example.com/job/my-pipeline/build?token=webhooktoken
Expected OutputExpected
No output (command runs silently)
→
-X POST - Specifies the HTTP POST method to trigger the build
Lists all Jenkins jobs to confirm 'my-pipeline' exists and is ready for triggers.
Terminal
jenkins-cli list-jobs
Expected OutputExpected
my-pipeline example-job build-test
Key Concept

If you remember nothing else from this pattern, remember: build triggers automate job starts based on code changes, scheduled times, or external signals to save manual effort.

Common Mistakes
Setting poll SCM trigger without proper repository access permissions
Jenkins cannot check the repository for changes, so builds never start automatically.
Ensure Jenkins has correct credentials and access rights to the source code repository before enabling poll SCM.
Not configuring the webhook URL correctly in the source code repository
Jenkins never receives the push notification, so builds are not triggered immediately.
Set the webhook URL in the repository to point exactly to Jenkins job webhook endpoint with correct token.
Using incorrect cron syntax for timer triggers
The build does not run at the expected time or never runs.
Use Jenkins cron syntax carefully, for example 'H 2 * * *' for daily at 2 AM, and test with Jenkins cron syntax validator.
Summary
Use poll SCM trigger to check for code changes regularly and start builds automatically.
Use webhook trigger to start builds immediately when code is pushed to the repository.
Use timer trigger to schedule builds at specific times like nightly or hourly.
Verify triggers by checking job configuration and manually triggering builds.
Configure repository and Jenkins permissions correctly for triggers to work.

Practice

(1/5)
1. Which Jenkins build trigger checks the source code repository regularly for changes before starting a build?
easy
A. poll SCM
B. webhook
C. timer (cron)
D. manual trigger

Solution

  1. Step 1: Understand the purpose of poll SCM

    poll SCM trigger makes Jenkins check the source code repository at set intervals for any changes.
  2. Step 2: Compare with other triggers

    Webhook triggers build instantly on code push, timer triggers build at scheduled times, manual trigger requires user action.
  3. Final Answer:

    poll SCM -> Option A
  4. Quick Check:

    poll SCM = checks repo regularly [OK]
Hint: poll SCM means Jenkins checks repo regularly [OK]
Common Mistakes:
  • Confusing webhook with poll SCM
  • Thinking timer triggers check repo changes
  • Assuming manual trigger is automatic
2. Which of the following is the correct syntax to schedule a Jenkins build every day at midnight using cron?
easy
A. * * * * 0
B. 0 0 * * *
C. 0 12 * * *
D. 0 0 0 * *

Solution

  1. Step 1: Understand cron syntax

    Cron format is 'minute hour day month weekday'. To run at midnight daily, minute=0 and hour=0.
  2. Step 2: Match the correct expression

    0 0 * * * "0 0 * * *" means every day at 00:00 hours (midnight).
  3. Final Answer:

    0 0 * * * -> Option B
  4. Quick Check:

    Midnight daily cron = 0 0 * * * [OK]
Hint: Midnight daily cron is always '0 0 * * *' [OK]
Common Mistakes:
  • Mixing hour and minute fields
  • Using wrong day or weekday values
  • Confusing 12 PM with midnight
3. Given this Jenkins pipeline snippet, what will trigger the build?
triggers {
  pollSCM('H/15 * * * *')
}
medium
A. Build runs only if SCM changes detected every 15 minutes
B. Build runs every 15 minutes regardless of changes
C. Build runs once daily at 15 minutes past midnight
D. Build runs immediately after a webhook event

Solution

  1. Step 1: Understand pollSCM with 'H/15 * * * *'

    This means Jenkins checks the SCM every 15 minutes for changes.
  2. Step 2: Determine when build triggers

    Build triggers only if changes are detected during these checks, not every 15 minutes automatically.
  3. Final Answer:

    Build runs only if SCM changes detected every 15 minutes -> Option A
  4. Quick Check:

    pollSCM triggers on changes every 15 mins [OK]
Hint: pollSCM triggers only on changes, not just time [OK]
Common Mistakes:
  • Assuming build runs every 15 minutes regardless
  • Confusing pollSCM with timer trigger
  • Thinking webhook triggers are involved here
4. A Jenkins job with this trigger configuration does not start builds as expected:
triggers {
  pollSCM('H/5 * * * *')
}
What is the most likely cause?
medium
A. Timer trigger conflicts with pollSCM
B. The cron syntax 'H/5 * * * *' is invalid
C. Webhook is required for pollSCM to work
D. SCM polling is disabled in Jenkins global settings

Solution

  1. Step 1: Check pollSCM prerequisites

    pollSCM requires SCM polling enabled globally in Jenkins settings.
  2. Step 2: Validate other options

    Syntax 'H/5 * * * *' is valid; webhook is separate trigger; no conflict with timer here.
  3. Final Answer:

    SCM polling is disabled in Jenkins global settings -> Option D
  4. Quick Check:

    pollSCM needs global SCM polling enabled [OK]
Hint: pollSCM needs global SCM polling enabled [OK]
Common Mistakes:
  • Assuming cron syntax is wrong
  • Thinking webhook is mandatory for pollSCM
  • Believing timer conflicts cause failure
5. You want Jenkins to build your project automatically every weekday at 6 AM and also immediately when code is pushed to GitHub. Which trigger setup is best?
hard
A. Use only pollSCM with cron '0 6 * * 1-5'
B. Use webhook only without timer
C. Use timer trigger '0 6 * * 1-5' and configure GitHub webhook
D. Use pollSCM every minute

Solution

  1. Step 1: Understand requirements

    Build at 6 AM weekdays and immediately on GitHub push means two triggers needed: timer and webhook.
  2. Step 2: Evaluate options

    Use timer trigger '0 6 * * 1-5' and configure GitHub webhook combines timer for scheduled build and webhook for instant build on push, fulfilling both needs.
  3. Final Answer:

    Use timer trigger '0 6 * * 1-5' and configure GitHub webhook -> Option C
  4. Quick Check:

    Timer + webhook = scheduled + instant builds [OK]
Hint: Combine timer and webhook for schedule plus instant build [OK]
Common Mistakes:
  • Using only pollSCM misses instant build
  • Using webhook alone misses scheduled build
  • Polling every minute is inefficient
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard