How can I automatically deploy my app after a git push ( GitHub and node.js)?

I have my application (node.js) deployed on a VPS (linux). I'm using git hub as a repository. How can I deploy the application automatically, on git push ?


Solution 1:

Example in PHP:

Navigate to github into your github repository add click "Admin"

click tab 'Service Hooks' => 'WebHook URLs'

and add

http://your-domain-name/git_test.php

then create git_test.php

<?php 
try
{
  $payload = json_decode($_REQUEST['payload']);
}
catch(Exception $e)
{
  exit(0);
}

//log the request
file_put_contents('logs/github.txt', print_r($payload, TRUE), FILE_APPEND);


if ($payload->ref === 'refs/heads/master')
{
  // path to your site deployment script
  exec('./build.sh');
}

In the build.sh you will need to put usual commands to retrieve your site from github

Solution 2:

There were a few mentions of Git hooks as answers/comments, which has worked for me in the past.. so here's my recipe should someone else require more specifics.

I use a combination of the git post-receive hook and node-supervisor to accomplish simple auto deployment (assuming you're using a git remote repository on that machine).


Setup Your Post-Receive Hook

In your repository: sudo vi hooks/post-receive

And it should look something like:

#!/bin/sh
GIT_WORK_TREE=/home/path/to/your/www
export GIT_WORK_TREE
git checkout -f

Set file permissions: chmod +x hooks/post-receive

Git will refresh the files in your app directory following a push to the repo.


Run Node with Node-Supervisor

You'll need to install Node-Supervisor on your machine as a global node module: sudo npm install supervisor -g

Now simply run your node app with node-supervisor and it'll watch for changes to files in your working directory:

supervisor /home/path/to/your/www/server.js (note supervisor instead of node).

Solution 3:

Probably very late to repond here. But I found this project on github and seems to do what you want to do but in a much cleaner way.

https://github.com/logsol/Github-Auto-Deploy

Check it out. Would be also interested to know what others think of this in terms of comments and upvotes.

Cheers,
S

Solution 4:

In a project I am currently developing I follow the guidelines covered in Jez Humble's brilliant book "Continuous Delivery" (well worth a read).

This means creating a deployment pipeline using some form of continuous integration server (I use Thoughtworks free community edition of Go), that is responsible for first checking your code for quality, complexity and running unit tests. It can then follow a deployment pipeline resulting in a push to your production servers.

This sounds very complicated, but doesn't have to be, and does make the whole process of writing code and it making it's way into production safe and worry free (no scary release days!).

I use a full deployment pipeline for live systems, and a cut down version for npm modules that I write, and both share the same 1-click deployment technique.