Skip to content

Instantly share code, notes, and snippets.

@randylien
Forked from ryanhanwu/README.md
Created December 14, 2013 02:24
Show Gist options
  • Save randylien/7954978 to your computer and use it in GitHub Desktop.
Save randylien/7954978 to your computer and use it in GitHub Desktop.

Auto deployment script for CI with Git/Node.JS/Forever

Usage

1.Simply add the deploy.sh to your application folder

2.Start your service

deploy.sh start myApp.js

3.Config your CI tasks (example: Jenkins) to run this every 10 minutes

./deploy.sh update

Full usage

Start Application

./deploy.sh start testApp.js

Update from Git (if any commit)

./deploy.sh update

Display Status

./deploy.sh status

Stop application

./deploy.sh stop
#!/bin/bash
# Change following variables for your application
PIDFILENAME=application #This will be used to identify forever process
LOGPATH=log #Log files
# DEBUG
#set -x
PROJ_PATH=`pwd`
APPJS=$2
usage () {
echo "Usage: "
echo " ./deploy.sh {start|update|status} yourApp.js"
}
[ -d $LOGPATH ] || mkdir $LOGPATH
case "$1" in
start)
if [ $# != 2 ]; then
echo "Wrong Parameters"
usage
exit 1
fi
forever start -p $PROJ_PATH --pidFile $PROJ_PATH/$PIDFILENAME.pid -a -l $LOGPATH/server.log -o $LOGPATH/out.log -e $LOGPATH/err.log $APPJS
exit 0
;;
status)
forever list
exit 0
;;
update)
GIT_CHANGES=`git pull | wc -l`
if [ $GIT_CHANGES -gt 1 ]; then
npm install
PID=`cat $PROJ_PATH/$PIDFILENAME.pid`
FID=`forever list | grep $PID | cut -f6 -d' '`
forever restart $FID
echo "Application Updated."
fi
echo "Already up-to-date."
exit 0
;;
stop)
PID=`cat $PROJ_PATH/$PIDFILENAME.pid`
FID=`forever list | grep $PID | cut -f6 -d' '`
forever stop $FID
exit 0
;;
*)
usage
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment