问题
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 ?
回答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
回答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
).
回答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
回答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.
回答5:
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).
回答6:
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.
回答7:
I found the project for easy deployment uses git.
git-play
I think it's proper way for you.
Check it out.
回答8:
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!
回答9:
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
回答10:
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.
回答11:
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/
回答12:
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
回答13:
Also note there are free/inexpensive services out there like REPOMAN.IO that automate almost all of this for you.
来源:https://stackoverflow.com/questions/9132144/how-can-i-automatically-deploy-my-app-after-a-git-push-github-and-node-js