Auto Deployment for Node.js and GitHub

node.js Jan 3, 2017

Auto deployment of code would not only save me time, it would ensures that my repository is deployed in the same manner with every push; without the need of ssh'ing into the server. I recently spent some time exploring the best solution to auto deployment with GitHub using private token keys (I did not want to use SSH for this).

For a project I am currently working on, I needed the ability to push (or commit) updates to my master Git repository and then forward to them to a server. There are many solutions out there, but they were over engineered and complex. I just wanted a simple solution that works with node.js. This would enable me to embed the update within my current API structure without the need of running additional node.js instances.

After some searching I found a package called node-cd, which is a great little repo that is suitable for GitHub, BitBucket and Contentful. However, I wanted to customise it to function on private (but also public) repositories in GitHub, to function with npm install on my node.js application seamlessly and to integrate into my project.

You can find my solution on GitHub here. This is a scrappy hack that I would not use in a production enviorment.

I will briefly explain some aspects of the code and what you need to get started. The code has been tested using a MEAN image on a Digital Ocean droplet.

GitHub Private Token

Personal access tokens function like ordinary OAuth access tokens. They can be used instead of a password for Git over HTTPS, such as when we are required to pull a repository. A guide to create your personal access token is available here.

Web Hooks

To automate the process you will need to create a Webhook with the Git repository. This will broadcast to a defined URL when a commit/push has been performed. You will need to ensure that it is a totally anonymous link that no one can guess. See the router.js for an example.

If you are seeking to use this in production I would recommend you add additional security features.

Shell Script

To automate the install process, I decided to utilise a shell script; this will run every time there is a commit to the repository. An example of the code is below

cd /opt/

#Clone the repo
sudo git clone https://<token>

#Stop all processes
sudo forever stopall

#Move back and prepare to rename the Git Repo
sudo rm -r /opt/www
sudo mv /opt/repo-api-server /opt/www

#Now start the app
cd /opt/www
sudo npm install
sudo forever -w /opt/www/app.js

The script does the following tasks (in order)

  • Ensure we are in the correct directory (one up from the node.js app)
  • Clone the repository using https
  • Stop all node processes
  • Remove our current www folder
  • Rename the Git repo to www
  • Change directory into www
  • Install dependancies using npm install
  • Start up all processes using forever

I store this file in the directory one up from the www folder.

First Install

On the first use of this script you will have to manual execute the above commands.

Every time you commit an update to your master branch it is not auto installed on your server. This is not the most elegant solution, but it works.

I welcome any comments or suggestions below, or via the repository