Setting Up and Testing a Minimal GitHub-to-Jenkins Webhook Integration with a Secondary Endpoint

Introduction

GitHub webhooks provide a dynamic way to trigger external actions based on repository events. One common application is the initiation of builds in Continuous Integration tools such as Jenkins. In this guide, we’ll outline the process to set up a basic webhook that connects GitHub to Jenkins, and then extend our testing by introducing a secondary endpoint using a Python-based server.

Prerequisites

  • A running Jenkins server.
  • A GitHub repository.
  • Python environment.

Configuring Jenkins for Webhook Integration

  1. Install Necessary Plugins: Navigate to Jenkins’ Plugin Manager and ensure the GitHub plugin is installed.
  2. Set Up a Jenkins Job:
    • Go to the Jenkins dashboard.
    • Click “New Item” to create a freestyle project.
    • In project configuration, under Source Code Management, select Git and input the repository URL.
    • In Build Triggers, check GitHub hook trigger for GITScm polling.

Setting Up GitHub Webhooks

  1. Navigate to Repository Settings: In your GitHub repository, go to Settings -> Webhooks.
  2. Add the Primary Webhook for Jenkins:

    • Click Add webhook.
    • For Payload URL, use http://[jenkins-server]/github-webhook/.
    • For Content Type, choose application/json.
    • For events, select Just the push event.
    • Ensure the Active checkbox is checked.
  3. Add the Secondary Webhook for webhook_server.py:
    • Click Add webhook again.
    • For Payload URL, use http://[server-ip]:5000/ where [server-ip] is where webhook_server.py is running.
    • For Content Type, choose application/json.
    • For events, again select Just the push event.
    • Ensure the Active checkbox is checked.

webhook_server.py: The Secondary Endpoint

The secondary endpoint will provide direct feedback on webhook activity. Here’s how to set it up:

from http.server import BaseHTTPRequestHandler, HTTPServer

class WebhookHandler(BaseHTTPRequestHandler):
    def do_POST(self):
        content_length = int(self.headers['Content-Length'])
        post_data = self.rfile.read(content_length)
        print(post_data.decode('utf-8'))

        self.send_response(200)
        self.end_headers()

if __name__ == "__main__":
    server_address = ('', 8001)  # Listen on all interfaces, port 8001
    httpd = HTTPServer(server_address, WebhookHandler)
    print('Webhook server started on port 8001...')
    httpd.serve_forever()

To execute the script:

  1. Run the server: python webhook_server.py.

Testing the Integration

  1. Trigger a Push Event: Commit and push a change to the GitHub repository.
  2. Observe Responses: Jenkins should initiate its associated job. Meanwhile, the webhook_server.py terminal will display a message confirming the receipt of the webhook call.

Conclusion

Setting up a basic webhook for GitHub-to-Jenkins integration is a straightforward process that becomes even more transparent when extended with a secondary listener like webhook_server.py. This combined approach not only automates build triggers based on repository alterations but also offers a direct method to monitor and understand webhook activities.