Answer
Jenkins Build Triggers
Build triggers determine when a Jenkins job runs automatically.
1. SCM Polling (Poll Source Control Management)
Checks Git for changes on a schedule. If new commits found → triggers build.
H/5 * * * * → check every 5 minutes
H * * * * → check every hour
Pros: Simple, no webhook setup needed Cons: Delay (up to 5 min), unnecessary polling
// Jenkinsfile
pipeline {
triggers {
pollSCM('H/5 * * * *') // poll every 5 minutes
}
...
}
2. GitHub/GitLab Webhook (Recommended — Instant)
GitHub calls Jenkins the moment code is pushed.
Setup:
- ✓Jenkins: Install "GitHub plugin", generate token
- ✓GitHub: Settings → Webhooks → Add webhook →
http://jenkins.url/github-webhook/ - ✓Jenkins job: Check "GitHub hook trigger for GITScm polling"
// Jenkinsfile
pipeline {
triggers {
githubPush() // triggers on any push to the branch
}
...
}
Pros: Instant build on every push, no polling overhead Cons: Jenkins must be publicly accessible (or use ngrok for local dev)
3. Scheduled Build (Cron Syntax)
Run builds at specific times — nightly regression, weekly reports.
// Cron syntax: minute hour day-of-month month day-of-week
0 2 * * * → every day at 2:00 AM
0 22 * * 1-5 → weekdays at 10 PM
H 0 * * 7 → every Sunday at midnight (H = hash to spread load)
pipeline {
triggers {
cron('0 2 * * *') // nightly at 2 AM
}
...
}
4. Upstream Job Trigger
Trigger this job when another job completes.
Job: Build → triggers → Job: Test → triggers → Job: Deploy
pipeline {
triggers {
upstream(upstreamProjects: 'my-build-job', threshold: hudson.model.Result.SUCCESS)
}
...
}
5. Build with Parameters (Manual + API)
# Trigger via Jenkins API
curl -X POST "http://jenkins/job/my-job/build" \
--user "admin:token" \
--data-urlencode json='{"parameter":[{"name":"ENVIRONMENT","value":"staging"}]}'
6. Pull Request Trigger (Jenkins + GitHub PR Builder)
Trigger builds on PR creation/update — run tests before merge.
pipeline {
triggers {
pullRequestTrigger(
events: [openPullRequest(), updatePullRequest()]
)
}
}
Summary Table
| Trigger | Speed | Use Case |
|---|---|---|
| Webhook | Instant | Every push, PR builds |
| SCM Poll | ~5 min delay | When webhook isn't possible |
| Cron | Scheduled | Nightly regression tests |
| Upstream | Post-build | Deployment chains |
| Manual | Immediate | On-demand builds |
