How to Automatically Deploy from GitHub to Local Server on Git Push

As developers (oh yeah, as devops guys), it’s a no-brainer to automate our workflows to make life simple and easy for us. In this article, I’ll demonstrate how to automatically deploy a nodejs web app from GitHub to a production or test server on git push. You can follow the same instructions to deploy any application written in any language.

Tools used in this demo

We use quite a few tools in this demo. Each one has a specific role to play in the Continuous Deployment(CD) pipeline we are about to setup. Each of these tools are briefly explained below.

Nodemon

Monitors any changes in your node.js application and automatically restarts the server. Though Nodemon is typically used in development for quick re-spins, we’ll use it in production in this demo to simply explain you the overall concept.

Webhook

Incoming webhooks handler which can execute shell commands.

SocketXP

Enables us to receive webhooks from the internet and relay it into your local network without exposing the app to the internet. SocketXP eliminates the need to own a public IP.

Demo app

We’ll use the following simple nodejs web application for this demo.

You can find the source code used in this demo at the following github repo:
https://github.com/socketxp-com/webhook-autoupdate

# Git clone the demo app

First clone the demo app from GitHub.

# Install Nodemon

Change the working directory to the “webhook-autoupdate” git folder you just cloned above. Install nodemon globally and run it.

Curl the demo web app

Access the NodeJS web app running at http://localhost:8080 using curl.

# Install Webhook app

To handle push notifications (or webhooks) from github (or any webhook sender application for that matter), you don’t need to implement a webhook handler yourself. There are apps available to offload the job. One such app is Webhook app.

Using the Webhook app easily create HTTP endpoints (webhooks handler) on your server, which you can use to execute configured commands. Webhook app also allows you to specify rules which must be satisfied in order for any hook to be triggered.

Download the Webhook app (choose one appropriate for your OS and Platform) from the github repo release folder

You’ll find a “hooks.json” file in the “webhook-autoupdate” GitHub repository that you cloned in the previous section. Update the hooks.json file with the correct directory names and secret value, as shown by the marked lines below.

Use this hooks.json file to kickstart the webhook app.

Note: The above webhook URL “http://0.0.0.0:9000/hooks/{id}" is a localhost url. Github cannot send notifications to this IP address because it is not a publicly reachable IP address. Github needs a publicly reachable public URL to send push notifications.

This is where SocketXP solution comes in handy. We can use the SocketXP Secure Reverse Proxy Tunneling Service to relay webhooks notifications from Github to the localhost Webhook app.

# Install SocketXP agent

Download and install SocketXP agent on your system following the instructions here — SocketXP Downaload and Install.

After SocketXP agent is installed, request the agent to relay the github webhook to your local webhook URL as shown below.

Now copy the above SocketXP Public URL and use it to update the webhook settings in the Github page of your project repository as shown below. Also make sure you select the payload content type to “applicatin/json” in the dropdown box. Save the changes.

Begin Auto Deploy Demo

Let’s begin the demo by committing a code change into Github.

Git commit and push

Commit a code change into your Github project. I have bumped up the version string in my http-server.js” to version 2.0 and committed the code changes.

SocketXP webhook relay agent

Check if the SocketXP agent has received the webhook notifications from Github.

Check the webhook app

Check if the webhook app has received the webhook notification from the SocketXP agent and processed the webhook notification.

Check Nodemon has restarted the web server

Curl the web server

The hello-world application displays “v2.0” in the output, which proves that the auto deployment works as we expected.

That’s all folks!

You can receive webhooks from any online service to your local server using SocketXP. SocketXP eliminates the need to own a public IP address to receive webhook notifications from online services. Moreover, SocketXP has a free-tier for developers.

Originally published at https://www.socketxp.com on June 25, 2020.

Ganesh Velrajan is the founder of SocketXP. Learn more about SocketXP Remote Access Solutions at https://www.socketxp.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store