├── .gitignore ├── README.md ├── app.js ├── deploy.sh └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Deployed apps should consider commenting this line out: 24 | # see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git 25 | node_modules 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Webhook_Example 2 | =============== 3 | 4 | Simple example showing how to use github webhooks with NodeJS 5 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | /* 2 | App JS 3 | Developer: 4 | Luis Matute - luis.matute@me.com 5 | Description: 6 | This is the server configuration. 7 | */ 8 | 9 | var express = require('express'), 10 | http = require('http'), 11 | 12 | app = express(); 13 | 14 | // all environments 15 | app.set('port', process.env.PORT || 8080); 16 | 17 | app.get('/', function (req, res, next) { 18 | res.send('Hello World!') 19 | }) 20 | 21 | app.post('/deploy/', function (req, res) { 22 | var spawn = require('child_process').spawn, 23 | deploy = spawn('sh', [ './deploy.sh' ]); 24 | 25 | deploy.stdout.on('data', function (data) { 26 | console.log(''+data); 27 | }); 28 | 29 | deploy.on('close', function (code) { 30 | console.log('Child process exited with code ' + code); 31 | }); 32 | res.json(200, {message: 'Github Hook received!'}) 33 | }); 34 | 35 | http.createServer(app).listen(app.get('port'), function(){ 36 | console.log('Express server listening on port ' + app.get('port')); 37 | }); 38 | -------------------------------------------------------------------------------- /deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Description: 4 | # Script will receive 3 Arguments (Project Name, Environment, Branch) 5 | # Various things happening here: 6 | # 1.) Changing to the repo directory 7 | # 2.) Resetting the local master branch to look exactly like remote origin master branch 8 | # 3.) Clean off any files that don't need to be there 9 | # 4.) Pull just to be doubly sure we have all the latest changes 10 | # 5.) Switches branch to master just to be extra sure that we are on the right 11 | # branch and our working tree has the right files. 12 | # 13 | # Written by: LM 14 | # 15 | 16 | if [[ -n "$1" ]] ; then 17 | PROJECT_NAME=$1 18 | else 19 | PROJECT_NAME="lm-blog" 20 | fi 21 | 22 | if [[ -n "$2" ]] ; then 23 | BRANCH=$2 24 | else 25 | BRANCH="master" 26 | fi 27 | 28 | cd /webroot/$PROJECT_NAME 29 | git reset --hard origin/$BRANCH 30 | git clean -f 31 | git pull 32 | git checkout $BRANCH 33 | 34 | # forever stop index.js 35 | # NODE_ENV=production forever start index.js 36 | echo "#-----------------------------------------------#" 37 | echo "# Execution Completed #" 38 | echo "#-----------------------------------------------#" -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "application-name", 3 | "version": "0.0.1", 4 | "private": true, 5 | "scripts": { 6 | "start": "node app.js" 7 | }, 8 | "dependencies": { 9 | "express": "3.5.1", 10 | "jade": "*" 11 | } 12 | } --------------------------------------------------------------------------------