How to Trigger Jenkins Build on Git Push Automatically
To trigger a Jenkins build on a
git push, configure your Jenkins job with the Git plugin and enable Build when a change is pushed to GitHub or use a webhook from your Git server to notify Jenkins. This setup makes Jenkins start a build automatically whenever new code is pushed.Syntax
To trigger a Jenkins build on git push, you typically use the following setup:
- Git Plugin: Connect Jenkins to your Git repository URL.
- Build Triggers: Enable
GitHub hook trigger for GITScm pollingorPoll SCM. - Webhook: Configure your Git server (GitHub, GitLab, Bitbucket) to send a webhook to Jenkins on push events.
This combination allows Jenkins to listen for push events and start builds automatically.
groovy
pipeline {
agent any
triggers {
pollSCM('H/5 * * * *')
}
stages {
stage('Build') {
steps {
echo 'Building on git push'
}
}
}
}Example
This example shows how to set up a Jenkins Pipeline job that triggers a build when a git push happens using a webhook and the GitHub hook trigger for GITScm polling option.
Steps:
- Create a Pipeline job in Jenkins.
- In the job configuration, under Build Triggers, check
GitHub hook trigger for GITScm polling. - Set your Git repository URL in the Pipeline script or SCM section.
- Configure your GitHub repository webhook to point to
http://your-jenkins-url/github-webhook/.
groovy
pipeline {
agent any
triggers {
githubPush()
}
stages {
stage('Build') {
steps {
echo 'Triggered by git push via webhook'
}
}
}
}Output
[Pipeline] Start of Pipeline
[Pipeline] echo
Triggered by git push via webhook
[Pipeline] End of Pipeline
Common Pitfalls
Common mistakes when triggering Jenkins builds on git push include:
- Not configuring the webhook URL correctly in the Git repository.
- Forgetting to enable the
GitHub hook trigger for GITScm pollingin Jenkins job triggers. - Using
Poll SCMwithout a proper schedule, causing delayed or no builds. - Firewall or network issues blocking webhook delivery.
- Incorrect repository URL or credentials in Jenkins SCM settings.
Always test webhook delivery from your Git server and check Jenkins logs for errors.
groovy
Wrong trigger setup example:
pipeline {
agent any
triggers {
// Missing githubPush() trigger
}
stages {
stage('Build') {
steps {
echo 'This will NOT trigger on git push'
}
}
}
}
Correct trigger setup example:
pipeline {
agent any
triggers {
githubPush()
}
stages {
stage('Build') {
steps {
echo 'This triggers on git push'
}
}
}
}Quick Reference
- Enable webhook on Git server: Point to
http://your-jenkins-url/github-webhook/. - Jenkins job trigger: Check
GitHub hook trigger for GITScm polling. - SCM configuration: Use correct Git repo URL and credentials.
- Test webhook: Use GitHub webhook test feature to confirm delivery.
- Firewall: Ensure Jenkins is reachable from Git server.
Key Takeaways
Configure Jenkins job with Git plugin and enable GitHub hook trigger for automatic builds.
Set up a webhook in your Git repository to notify Jenkins on every git push.
Verify webhook delivery and Jenkins trigger settings to avoid build failures.
Use Pipeline syntax with the githubPush() trigger for modern Jenkins jobs.
Check network and credentials if builds do not start after git push.