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

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


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 requestfile_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


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/shGIT_WORK_TREE=/home/path/to/your/wwwexport GIT_WORK_TREEgit 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).


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