How to Use GitHub Webhook with Jenkins for Automated Builds
To use a
GitHub webhook with Jenkins, configure a webhook URL in your GitHub repository pointing to your Jenkins server's /github-webhook/ endpoint. Then, set up a Jenkins job with the GitHub hook trigger for GITScm polling enabled to automatically start builds when code changes are pushed.Syntax
The GitHub webhook URL format for Jenkins is:
http://your-jenkins-domain/github-webhook/
In Jenkins, enable the trigger GitHub hook trigger for GITScm polling in your job configuration.
Each part explained:
- GitHub webhook URL: The URL GitHub calls on events like push.
- Jenkins job trigger: Listens for webhook calls to start builds.
text
http://your-jenkins-domain/github-webhook/Example
This example shows how to set up a Jenkins freestyle project to build automatically when you push code to GitHub.
- Create a Jenkins freestyle project.
- Under Source Code Management, select Git and enter your GitHub repo URL.
- Under Build Triggers, check
GitHub hook trigger for GITScm polling. - In your GitHub repo, go to Settings > Webhooks and add a webhook with the URL
http://your-jenkins-domain/github-webhook/. - Set content type to
application/jsonand select Just the push event. - Save the webhook.
Now, when you push code, GitHub calls Jenkins and triggers the build.
groovy
pipeline {
agent any
triggers {
githubPush()
}
stages {
stage('Build') {
steps {
echo 'Building project triggered by GitHub webhook'
}
}
}
}Output
[Pipeline] Start of Pipeline
[Pipeline] echo
Building project triggered by GitHub webhook
[Pipeline] End of Pipeline
Common Pitfalls
- Incorrect webhook URL: Make sure the URL ends with
/github-webhook/and is accessible from GitHub. - Firewall or network issues: Jenkins must be reachable by GitHub; open necessary ports.
- Missing trigger in Jenkins job: The job must have
GitHub hook trigger for GITScm pollingenabled. - Wrong content type: GitHub webhook content type should be
application/json. - Not using GitHub plugin: Jenkins needs the GitHub plugin installed for webhook support.
text
Wrong webhook URL example: http://your-jenkins-domain/ Right webhook URL example: http://your-jenkins-domain/github-webhook/
Quick Reference
| Step | Action | Details |
|---|---|---|
| 1 | Create Jenkins job | Freestyle or Pipeline with Git repo configured |
| 2 | Enable trigger | Check 'GitHub hook trigger for GITScm polling' in job settings |
| 3 | Add GitHub webhook | URL: http://your-jenkins-domain/github-webhook/, Content type: application/json |
| 4 | Test webhook | Push code to GitHub and verify Jenkins build starts |
| 5 | Troubleshoot | Check Jenkins logs and GitHub webhook delivery status |
Key Takeaways
Set GitHub webhook URL to http://your-jenkins-domain/github-webhook/ to notify Jenkins on code changes.
Enable 'GitHub hook trigger for GITScm polling' in Jenkins job to start builds automatically.
Ensure Jenkins is reachable from GitHub and the GitHub plugin is installed.
Use application/json as webhook content type for proper communication.
Verify webhook delivery and Jenkins logs if builds do not trigger as expected.