github-webhook-localhost-jenkins

GitHub Webhook Integration with Localhost Jenkins



So you want to integrate your GitHub project repository with a localhost Jenkins build server so that automated builds could be triggered upon code commit. That's a cool way to get your workflow automated. At SocketXP, we understand the challenges involved in setting up a CI/CD pipeline, and that's why we have written this article exclusively for you.

This article will demonstrate how to integrate your GitHub project with your localhost Jenkins server, so that your team could enjoy the benefits of continuous integration and continuous development.

1. Install the GitHub plugin in Jenkins

To receive and handle webhook notifications from GitHub, you need to install the GitHub plugin to your localhost Jenkins server. Go to "Manage Jenkins" -> "Manage Plugins" as shown below.

Note: Recent versions of Jenkins come with the GitHub plugin pre-installed. So you may skip this step, if you have already installed the recommend plugins set during Jenkins installation.


github-webhook-localhost-jenkins

Search the "GitHub" plugin in the available list of plugins and install the plugin, as shown below.

github-webhook-localhost-jenkins

2. Jenkins GitHub Webhook URL

Jenkins GitHub webhook URL looks like this:

http://[YOUR-JENKINS-URL]/github-webhook/

Let's say you access your Jenkins via the following Jenkins URL:

http://10.166.0.2:8080

Add "/github-webhook/" to the Jenkins URL to create the Jenkins GitHub webhook, as shown below.

http://10.166.0.2:8080/github-webhook/

Note:

Make sure that the "/" is present at the end of the URL.

3. Add the Jenkins webhook to GitHub Project

Follow the instructions below to add the Jenkins webhook to your GitHub project.

  • Login to your GitHub account
  • Go to your GitHub Project repository
  • Click the "Settings" tab on the top
  • Choose "Webhooks" tab in the lefthand side navigation bar.
  • Add the Jenkins webhook in the "Payload URL" text box, as shown below.
github-webhook-localhost-jenkins

Problem:

Typically, Jenkins build servers are run in the office local network behind NAT and Firewall to prevent hackers and unwanted users from accessing them. As a result, GitHub also cannot access your Jenkins build server, by invoking the Jenkins webhook URL to notify about GitHub code commits.

Notice that the IP address you use to access your Jenkins server "http://10.166.0.2:8080" is a private IP address. This IP address is not reachable from the internet.

How to make GitHub talk to your Jenkins server running in a private network ?

Solution: SocketXP Webhook Relay

The solution to this problem is to use the SocketXP Webhook Relay feature to interconnect your private Jenkins server with the public GitHub repository.

SocketXP gets you a public URL for your private web services.

github-webhook-localhost-jenkins
SocketXP Relays GitHub Webhook Notifications to Local Jenkins Server

Here are the steps you need to follow to install and configure SocketXP to relay the webhook notifications from GitHub to your Jenkins server.

1. Download and install SocketXP client

Download and install a simple and secure SocketXP client on any machine in your private network with reachability to your local Jenkins server. You can even install SocketXP client on the same server where your Jenkins runs.

Follow the instructions here to download and setup SocketXP client for your OS.

2. Relay webhooks using SocketXP

Next, configure SocketXP to relay webhook notifications from GitHub to your local Jenkins server.

You could use SocketXP to relay webhook notifications from any online service to your private network.

Use the below command to request SocketXP to relay GitHub webhook notifications to your private Jenkins. SocketXP will provide a public URL for your private webhook URL.

$ socketxp -relay http://10.166.0.2:8080/github-webhook/

Connected.
Public URL -> https://webhook.socketxp.com/ebab2937-750a-4363-83dd-738ee308afbb

SocketXP relays webhook notifications sent to the public URL (https://webhook.socketxp.com/ebab2937-750a-4363-83dd-738ee308afbb) to your private webhook URL (http://10.166.0.2:8080/github-webhook/).

Use the above public URL provided by SocketXP to register with GitHub webhook notification system as shown below.

github-webhook-localhost-jenkins

Test and Verify Webhook Notifications:

Finally, commit a test code to your GitHub project and make sure that GitHub sends a webhook notification and gets a successful response message (200 OK) as shown below.


github-webhook-localhost-jenkins

First, check if SocketXP Cloud Gateway has received the webhook notification. Go to https://portal.socketxp.com/#/webhook to check the webhooks received. Click entries to expand to see the details of the webhook received.

Secondly, check if the SocketXP agent running along with your Jenkins server has received the webhook notification.

$ socketxp -relay http://10.166.0.2:8080/github-webhook/

Connected.
Public URL -> https://webhook.socketxp.com/ebab2937-750a-4363-83dd-738ee308afbb

Webhook received:
POST / HTTP/1.1
Host: webhook.socketxp.com
Accept: */*
Accept-Encoding: gzip
Content-Length: 9397
Content-Type: application/json
User-Agent: GitHub-Hookshot/f2f2346
X-Github-Delivery: 4aba8354-c279-11ea-980e-6cd6cfda12a3
X-Github-Event: push
X-Hub-Signature: sha1=14e1f0a3d368d9b5dadd7d41cd575af2f45065ce

{"ref":"refs/heads/master","before":"2f12153ef944b939bc81712e1d047d281d2deabe","after":"1765b2ecd58ad3c6d3fec15c7b3198063b543405","repository":{"id":278398971,"node_id":"MDEwOlJlcG9zaXRvcnkyNzgzOTg5NzE=","name":"hello-world",
...
]}}

Next, ensure that Jenkins server has received the webhook notification and processed the same as shown below.


github-webhook-localhost-jenkins

Also check if a new build got triggered for your project in your Jenkins server.

Conclusion:

Jenkins is a great tool to automate, build and implement a Continuous Integration (CI) system for development teams.

GitHub is a great online source code management (SCM) tool.

SocketXP is a secure, powerful and easy to use cloud-based SaaS service to integrate a privately run localhost Jenkins with GitHub.

You can build a powerful CI/CD system using these three toolsets.

Bonus: Get any SocketXP Tunnels Subscription Plans at 50% discount using the coupon code - WHSOCKETXP2020

Next Steps:

Push your built and unit-tested application to a local test server or a Kubernetes Cluster for further product tests or integration tests. You coud also push your app to a production server or Kubernetes Cluster in the Cloud. Learn more here:

Continuous Delivery - Autoupdate Kubernetes Deployment on GitHub Webhook