Skip to content

Instantly share code, notes, and snippets.

@MadFaill
Last active August 29, 2015 14:05
Show Gist options
  • Save MadFaill/3568703a4a00a1d28262 to your computer and use it in GitHub Desktop.
Save MadFaill/3568703a4a00a1d28262 to your computer and use it in GitHub Desktop.
my deploy
#! /bin/bash
# path% /usr/local/sbin/deploy
# directory for deployment
# here will cloned tags
BASEDIR="/project/versions";
# path to project home
PROJECT="/project/www";
# url for repository
REPOSITORY="[email protected]:MadFaill/new.madfaill.git";
# composer executable
COMPOSER="/usr/local/sbin/composer"
# path to uploads
UPLOADS_PATH="/project/uploads";
# crontab user
CRONTAB_USER='madfaill'
# define print function
function print () {
local exp=$1;
local color=$2;
if ! [[ $color =~ '^[0-9]$' ]] ; then
case $(echo $color | tr '[:upper:]' '[:lower:]') in
black) color=0 ;;
red) color=1 ;;
green) color=2 ;;
yellow) color=3 ;;
blue) color=4 ;;
magenta) color=5 ;;
cyan) color=6 ;;
white|*) color=7 ;; # white or invalid color
esac
fi
tput setaf $color;
echo $exp;
tput sgr0;
}
# check exists install path
if [ ! -d "$BASEDIR" ]; then
mkdir $BASEDIR;
chmod -R 0774 $BASEDIR;
fi
# if tag was not specified - exit
# nothing to pull
if ! [[ $2 =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then
print 'need tag: -t <tag>' red;
exit;
fi
print "Deploy project by tag $2" green;
TAG_PATH="tag-$2";
# if not revert - clone
if [ ! -d "$BASEDIR/$TAG_PATH" ]; then
print "clone repository from $REPOSITORY" yellow;
vari=`git clone $REPOSITORY $BASEDIR/$TAG_PATH 2>&1`;
fi
# need to enter basedir for current tag
# all manipulations inside it
# echo "enter directory";
cd "$BASEDIR/$TAG_PATH";
# checkout =)
print "checkout tags/$2" green;
vari=`git checkout tags/$2 2>&1`;
# if need to update or install
# composer dependencies
print "install composer" green;
vari=`php $COMPOSER install 2>&1`;
# create sym-links for NGINX
# ==== =)
if [ -L "$PROJECT" ]; then
print "unlink exists project" yellow;
rm -rf "$PROJECT";
fi
# htdocs path
print "link $BASEDIR/$TAG_PATH/htdocs as project" green;
eval "ln -s $BASEDIR/$TAG_PATH/htdocs $PROJECT";
# chmod -R 0777 /project/versions/<tag>/app/temporary/
print "add permissions to temporary" yellow
out=`chmod -R 0777 $BASEDIR/$TAG_PATH/app/temporary/ 2>&1`
# unlink uploads (may have reload)
if [ -L "$PROJECT/uploads" ]; then
print "unlink uploads" yellow;
rm -rf "$PROJECT/uploads";
fi
# mad uploads injection
eval "ln -s $UPLOADS_PATH $PROJECT/uploads";
# periodical workers
print "add crontab" green;
eval "crontab -u $CRONTAB_USER $BASEDIR/$TAG_PATH/devel/crontab/workers";
# setup access for www-data
print "access setup" green;
access=`chown -R www-data:www-data $BASEDIR/$TAG_PATH/htdocs 2>&1`;
access+=`chown -R www-data:www-data $PROJECT 2>&1`;
if [[ $access != "" ]]; then
print "change project owner failed" red;
print "INFO: $access" blue
fi
print 'setup nginx configs' green;
out=`cp /etc/nginx/deployed-sites /etc/nginx/deployed-sites.tmp 2>&1`;
out=`rm -rf /etc/nginx/deployed-sites 2>&1`;
out=`ln -s $BASEDIR/$TAG_PATH/devel/sites-enabled /etc/nginx/deployed-sites 2>&1`;
out=`chown -R www-data:www-data /etc/nginx/deployed-sites 2>&1`;
INPUT=`nginx -t -c /etc/nginx/nginx.conf 2>&1`
#echo $INPUT;
if [[ $INPUT == *"syntax is ok"* ]]
then
print "restart service" yellow;
out=`rm -rf /etc/nginx/deployed-sites.tmp 2>&1`;
out=`service nginx restart 2>&1`;
print "restart php5-fpm" yellow;
out=`service php5-fpm restart`;
else
print 'NGINX config test failed' red
print 'rollback' red
out=`rm -rf /etc/nginx/deployed-sites 2>&1`
out=`cp /etc/nginx/deployed-sites.tmp /nginx/deployed-sites 2>&1`;
out=`rm -rf /etc/nginx/deployed-sites.tmp 2>&1`;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment