Have you ever thought about integrating your GitHub source code repository with a localhost Jenkins build server so that automated builds could be triggered upon code commit.
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 development
1. Install the GitHub plugin in Jenkins
To receive and handle the 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.
Search the “GitHub” plugin in the available list of plugins and install the plugin, as shown below.
2. Jenkins GitHub Webhook URL
Jenkins GitHub webhook URL looks like this:
Let’s say you access your Jenkins via the following Jenkins URL:
Add “/github-webhook/” to the Jenkins URL to create the Jenkins GitHub webhook, as shown below.
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.
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.
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.
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/ 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.
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.
Next, ensure that Jenkins has received the webhook notification and processed the same as shown below.
Also check if a new build got triggered for your project in your Jenkins.
Jenkins is a great tool to automate build and implement continuous integration continuous development system (CI/CD).
GitHub is a great online source code management (SCM) tool.
SocketXP is a secure, powerful and easy to use cloud-based middleware service to integrate a privately run Jenkins with GitHub.