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 ?
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With