Last active
August 29, 2015 14:05
-
-
Save sergeycherepanov/d0bbc50fb3b3496fe7bd to your computer and use it in GitHub Desktop.
GIt deployment script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
PATH=/sbin:/bin:/usr/sbin:/usr/bin | |
GIT=/usr/bin/git | |
USER=www-data | |
GROUP=www-data | |
BASE_DIR="/var/www" | |
HTML_DIR="public_html" | |
GIT_DIRNAME=".git" | |
LOG_FILENAME="deploy.log" | |
ME=`whoami` | |
execute () { | |
if [ $ME == $USER ]; then | |
/bin/bash -c "$1" 2>&1 | |
else | |
su - $USER -c "$1" 2>&1 | |
fi | |
} | |
usage () { | |
echo ""; | |
echo "For inititlize new project use: "$0" init <project_name> <remote_uri>"; | |
echo "For upgrade project from tag: "$0" upgrade <project_name> tags/<tag_name>"; | |
echo "For upgrade project from branch: "$0" upgrade <project_name> <remote>/<branch>"; | |
echo ""; | |
} | |
upgrade () { | |
PROJECT_NAME=$1 | |
PARAMS=$2 | |
WORK_TREE_DIR=$BASE_DIR"/"$PROJECT_NAME"/"$HTML_DIR | |
GIT_DIR=$BASE_DIR"/"$PROJECT_NAME"/"$GIT_DIRNAME | |
LOG_FILE=$BASE_DIR"/"$PROJECT_NAME"/"$LOG_FILENAME | |
if [ -d $GIT_DIR ]; then | |
execute "$GIT --work-tree=$WORK_TREE_DIR --git-dir=$GIT_DIR fetch --all >> $LOG_FILE" | |
execute "$GIT --work-tree=$WORK_TREE_DIR --git-dir=$GIT_DIR checkout -f $PARAMS >> $LOG_FILE" | |
else | |
echo "Peoject git dir not found in "$GIT_DIR | |
fi | |
} | |
initialize () { | |
PROJECT_NAME=$1 | |
PARAMS=$2 | |
WORK_TREE_DIR=$BASE_DIR"/"$PROJECT_NAME"/"$HTML_DIR | |
GIT_DIR=$BASE_DIR"/"$PROJECT_NAME"/"$GIT_DIRNAME | |
LOG_FILE=$BASE_DIR"/"$PROJECT_NAME"/"$LOG_FILENAME | |
if [ ! $PARAMS ]; then | |
echo "Please provide remote uri!" | |
usage | |
exit | |
fi | |
if [ -d $GIT_DIR ]; then | |
echo "Project already initialized in "$BASE_DIR"/"$PROJECT_NAME | |
exit | |
fi | |
if [ ! -d $WORK_TREE_DIR ]; then | |
execute "mkdir -p $WORK_TREE_DIR" | |
fi | |
if [ ! -d $GIT_DIR ]; then | |
execute "mkdir -p $GIT_DIR" | |
fi | |
execute "$GIT --work-tree=$WORK_TREE_DIR --git-dir=$GIT_DIR init" | |
execute "$GIT --work-tree=$WORK_TREE_DIR --git-dir=$GIT_DIR remote add origin $PARAMS" | |
if [ -d $GIT_DIR ]; then | |
echo "New project initialized in "$BASE_DIR"/"$PROJECT_NAME | |
fi | |
} | |
case "$1" in | |
start) | |
echo 'Nothing to do.' | |
;; | |
stop) | |
echo 'Nothing to do.' | |
;; | |
init) | |
echo 'Trying to initialize new project from git.' | |
initialize $2 $3 | |
;; | |
upgrade) | |
echo 'Upgrade project from git.' | |
upgrade $2 $3 | |
;; | |
restart) | |
$0 stop | |
$0 start | |
;; | |
*) | |
usage $0 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment