0
0
JenkinsHow-ToBeginner · 4 min read

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.

  1. Create a Jenkins freestyle project.
  2. Under Source Code Management, select Git and enter your GitHub repo URL.
  3. Under Build Triggers, check GitHub hook trigger for GITScm polling.
  4. In your GitHub repo, go to Settings > Webhooks and add a webhook with the URL http://your-jenkins-domain/github-webhook/.
  5. Set content type to application/json and select Just the push event.
  6. 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 polling enabled.
  • 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

StepActionDetails
1Create Jenkins jobFreestyle or Pipeline with Git repo configured
2Enable triggerCheck 'GitHub hook trigger for GITScm polling' in job settings
3Add GitHub webhookURL: http://your-jenkins-domain/github-webhook/, Content type: application/json
4Test webhookPush code to GitHub and verify Jenkins build starts
5TroubleshootCheck 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.