Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 639
Advanced Avatar asked Feb 03 '12 16:02

Advanced


2 Answers

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

like image 84
Pawel Wodzicki Avatar answered Sep 23 '22 23:09

Pawel Wodzicki


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).

like image 40
Wes Johnson Avatar answered Sep 22 '22 23:09

Wes Johnson