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 ?

share|improve this question
4  
have you checked git hooks progit.org/book/ch7-3.html, and also check on github help.github.com/test-webhooks – Pawel Dubiel Feb 3 '12 at 17:36
1  
Update for progit link above: git-scm.com/book/en/Customizing-Git-Git-Hooks – Code Novitiate Feb 18 '14 at 3:40
    
Git 2.10 will add an interesting feature: push options stackoverflow.com/a/38770670/6309 – VonC Aug 4 '16 at 14:58

14 Answers 14

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

share|improve this answer
6  
Hi, thank you a lot. What prevents Bob from executing my deployment script? – Advanced May 17 '13 at 13:07
16  
@Advanced 1 maybe script permissions, execution flag... 2 adding closing tag in PHP is a bad practice. – Pawel Dubiel May 17 '13 at 20:46
3  
@Advanced One technique to make sure Bob doesn't execute your script is to make sure that the POST request comes from Github's servers. Check out the HTTP headers that they send when making the request. Also you can create a 'secret' URL that isn't guessable. – jyap Mar 4 '14 at 20:15
4  
@Purefan Not a joke :) stackoverflow.com/questions/3219383/… – Pawel Dubiel Mar 18 '14 at 12:50
1  
To add to that, Facebook's Hack language (based off PHP) actually totally removes the ending tag :) – Daniel Lo Nigro Apr 9 '14 at 4:42

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

share|improve this answer
    
This is fine, however one thing to be aware of is if you add a new npm to your local app, you'll need to do an npm install for that module on the server as well. If you don't, you'll probably have a crashing app. – k00k Apr 25 '12 at 17:42
2  
Nope.. Any node modules my local app depends on are installed in the node_modules sub directory of my project, which is my local GIT repo, hence when I add, commit, then push to the remote server they get copied as well. – Wes Johnson Apr 28 '12 at 16:57
8  
Right, but what that means is that if any of those modules had code that was compiled (like mhash for example), it might not run on another server that's a different OS and/or architecture. Using package.json to keep track of your dependencies and then a deploy strategy that does a npm install -l on the remote server is smart. This can of course be coupled with your method using post-receive hooks. – k00k Apr 28 '12 at 17:40
    
How would this solution work with Github? – Tri Nguyen May 23 '13 at 21:09
1  
and you can just add the Git work tree into the git checkout command directly : git --work-tree=/var/www/tree --git-dir=/var/repo/deploy.git checkout -f (rather than creating the variable and exporting it in your script. – JasonB Dec 26 '13 at 23:07

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

share|improve this answer
14  
"Probably very late to repond here." Never too late. :) You are actually contributing to the whole community (most of us googlers; wow, just looking at those 20k views!), not the single guy asked the question "some time ago". Time, by itself, is irrelevant: as long as the technology in question is relevant, your answer will be, too. (Thanks for the tip, BTW, checking it out...) – Sz. May 11 '14 at 10:41
1  
thanks for your headup! ;) It worked great for me at that time. Now I prefer to use travis (travis-ci.org), (wherever I can) for automated deployments. @lunakid – Saurabh May 11 '14 at 15:03

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.

share|improve this answer
    
& another +1 for the book recommendation! I'm discovering CI is not to be approached casually. – Merrick Oct 4 '13 at 18:25
    
well, people ask a simple question, you give a full solution :). I have to say this is an overkill. But if you are already using continuous delivery, maybe this is the route to go. – windmaomao Mar 19 '15 at 16:13

I just published a node-based solution to your problem: node-cd

It consists in a simple node app running on your VPS that will receive Github post-receive Hooks and execute a the script you like (for example a shell script that will kill your app, git pull, and restart it).

share|improve this answer
    
+1 because it's pure node.js, so the poster does not have to add anything to their stack, or use a language they are not comfortable with. Also, really nicely laid out code – code_monk Feb 22 '14 at 18:56

Here's another simple nodeJS implementation.

It's a very simple node server that runs on a hostname and port you configure and can be setup to handle GitHub post receive web hooks. And the actual pul/test/deploy actions can be customised to do anything you want. In the current implementation, it is a shell command that is specified inline in the nodeJS server script. And there is a very simple secret_key based security scheme in place as well.

https://github.com/shyam-habarakada/rscds

My staging server already had node installed and running, so writing this up was quick and easy.

share|improve this answer
    
"yourdomain.com:8088/…; - REALLY?! "secret" key passed in the clear in the URL!!!! Nobody should be using that. – Julian Knight Sep 8 '14 at 9:01
1  
Have an aspirin and settle down Julian. Get parameters are encrypted when using https. – Gavin May 16 '15 at 6:07

I found the project for easy deployment uses git.

git-play

I think it's proper way for you.

Check it out.

share|improve this answer

You could use https://github.com/adnanh/webhook

Check out the wiki for detailed description on creating hooks :-)

share|improve this answer

If you want a python/tornado-based solution, I wrote a script to handle POST requests from Github's Webhook Services. You can find it at https://github.com/Akobi/ops/tree/master/autodeploy

It basically uses a JSON config file to list which repos you expect pushes from, which commands you want to run on deploy, and what directory the commands must run in. All you would have to do is modify the config file to your liking and run the script!

In addition, I use Nginx as a reverse proxy to forward these POSTs to my script. You can find the Nginx config in the same Github repo under the 'nginx' folder.

Happy pushing!

share|improve this answer

I'm the founder of https://commando.io and recently we announced an integration with GitHub via a service. The integration allows you to run executions on servers when you push to a GitHub repo. This is a perfect opportunity to automatically run deployment scripts when you push code.

An execution is a script you write inside of Commando.io that can be written in bash, perl, python, ruby, go, or node.js. To read more, and see an example execution script of running git pull, see our blog post announcement: http://blog.commando.io/run-executions-via-github-push/

share|improve this answer

the PHP answer is totally legit in my opinion, but if you prefer Ruby, I blogged a solution. it's the same thing as the PHP answer, just in a different language. you use a web hook and you have a simple script listen for the relevant HTTP requests.

http://gilesbowkett.blogspot.com/2012/06/heroku-style-deployment-on-ec2.html

share|improve this answer

I've created my own rudimentary deployment tool which would automatically pull down new updates from the repo - https://github.com/jesalg/SlimJim - Basically it listens to the github post-receive-hook and uses a proxy to trigger an update script.

share|improve this answer

Deepl.io seems to be new and promising contender in this space.

Features (taken from its website):

  • Catch webhooks from GitLab & GitHub
  • Configure multiple repositories
  • Configure multiple branches per repository
  • Use your own deploy scripts, either PHP, shell or both
  • Sends confirmation emails
share|improve this answer

Also note there are free/inexpensive services out there like REPOMAN.IO that automate almost all of this for you.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.