如何在 git 推送之后自动部署应用程序(GitHub 和 node.js) ?

我将我的应用程序(node.js)部署在 VPS (linux)上。我使用 git hub 作为存储库。如何在 git push 上自动部署应用程序?

66564 次浏览

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.

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

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

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

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

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.

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

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.

I found the project for easy deployment uses git.

git-play

I think it's proper way for you.

Check it out.

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!

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/

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

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