Last active
December 20, 2015 09:09
-
-
Save diyan/6105390 to your computer and use it in GitHub Desktop.
Git. Hooks. Post update hook to deploy site from repo host to the remote host or web-farm
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
NEW | |
#!/bin/bash | |
set -e | |
declare -r SSH_USER=www-data | |
declare -r SSH_KEY=~/.ssh/www-data.pem | |
declare -r GIT_URL=ssh://[email protected]/sample_repo.git | |
declare -r GIT_KEY=/var/www/.ssh/deploy_push.pem | |
declare -r GIT_DIR=/usr/local/sample_app | |
declare -r DEV_SERVERS=weblinux-dev01 | |
declare -r STAGE_SERVERS=weblinux-stage | |
declare -r PROD_SERVERS="weblinux01 weblinux02" | |
declare -r CARBON_HOST=graphite | |
declare -r -i CARBON_PORT=2003 | |
deploy_git_access_key() { | |
local hostname=$1; shift | |
ssh -i $SSH_KEY -o 'StrictHostKeyChecking=no' \ | |
$SSH_USER@$hostname 'bash -s' << DEPLOY_KEY_EOF | |
set -e | |
cat > ~/.ssh/deploy_push.pem << RSA_EOF | |
-----BEGIN RSA PRIVATE KEY----- | |
TODO put your rsa key here | |
-----END RSA PRIVATE KEY----- | |
RSA_EOF | |
chmod u=rw,go-rwx ~/.ssh/deploy_push.pem | |
cat > ~/.ssh/config << SSH_CONF_END | |
Host * | |
IdentityFile ~/.ssh/deploy_push.pem | |
SSH_CONF_END | |
cat > ~/.ssh/known_hosts << KNOWN_HOSTS_END | |
TODO put known host fingerprints here | |
KNOWN_HOSTS_END | |
# Switch Git for use https:// instead of git:// | |
git config --global url.https://.insteadOf git:// | |
echo [$hostname] ssh. git access key was deployed | |
DEPLOY_KEY_EOF | |
} | |
sync_changes() { | |
local hostname=$1; shift | |
local branch=$1; shift | |
ssh -i $SSH_KEY $SSH_USER@$hostname 'bash -s' << SYNC_REPO_EOF | |
set -e | |
#rm -rf ${GIT_DIR} #TODO this is only for debugging | |
if [ ! -d ${GIT_DIR}/.git ]; then | |
echo [$hostname] git init. local repo not found, init new repo | |
git init --quiet $GIT_DIR | |
cd $GIT_DIR | |
git remote add -f origin $GIT_URL | |
git checkout $branch | |
fi | |
cd $GIT_DIR | |
git remote set-url origin $GIT_URL | |
echo [$hostname] git fetch. getting all changes from origin repository | |
git fetch --quiet | |
SYNC_REPO_EOF | |
} | |
apply_changes() { | |
local hostname=$1; shift | |
local branch=$1; shift | |
local config_text=$1; shift | |
ssh -i $SSH_KEY $SSH_USER@$hostname 'bash -s' << CHECKOUT_EOF | |
set -e | |
echo [$hostname] config. writing local_settings.py file | |
echo $config_text | tee $GIT_DIR/sample_api/local_settings.py | |
echo [$hostname] api. ensure dependencies installed | |
source $GIT_DIR/sample_api/bin/activate | |
pip install -r $GIT_DIR/sample_api/requirements.txt --quiet --exists-action=w | |
pip install uwsgi==1.9.20 --quiet | |
echo [$hostname] git reset/clean/merge. applying updates on server | |
cd $GIT_DIR | |
git reset --hard --quiet | |
git clean --force -d | |
git merge --quiet --strategy-option=theirs origin/$branch | |
if [ "$branch" == "stage" ]; then | |
sed -i 's/api-dev/api-stage/g' $GIT_DIR/sample_ui/src/config.js | |
echo [$hostname] ui. config.js switched on stage/prod | |
elif [ "$branch" == "prod" ]; then | |
sed -i 's/api-dev/api/g' $GIT_DIR/sample_ui/src/config.js | |
echo [$hostname] ui. config.js switched on prod | |
fi | |
sudo /etc/init.d/nginx reload | |
sudo /etc/init.d/sample_app restart | |
echo [$hostname] app. code deployed, nginx reloaded, workers restarted | |
echo [$hostname] api. running tests | |
cd $GIT_DIR/sample_api | |
py.test -v --cov . --cov-report=html tests/ | grep FAILED || true | |
cd $GIT_DIR/sample_ui | |
echo [$hostname] ui. ensure dependencies installed | |
npm install > /dev/null | |
bower install --quiet | |
echo [$hostname] ui. running tests | |
grunt test --force | grep FAILED || true | |
CHECKOUT_EOF | |
} | |
track_on_graphite() { | |
local hostname=$1; shift | |
local branch=$1; shift | |
local unix_time=`date +%s` | |
echo [$hostname] graphite. record deploy event on Graphite | |
echo "events.deploy.$hostname.sample_app.$branch 1 $unix_time" \ | |
| nc $CARBON_HOST $CARBON_PORT | |
} | |
main() { | |
local branch=`echo $1 | sed s:refs/heads/::`; shift | |
local hostnames="" | |
local config_text="" | |
case $branch in | |
master) | |
hostnames=$DEV_SERVERS | |
config_text="CONFIG_CLASS_NAME=\'settings.DevConfig\'" | |
;; | |
stage) | |
hostnames=$STAGE_SERVERS | |
config_text="CONFIG_CLASS_NAME=\'settings.ProdConfig\'" | |
;; | |
prod) | |
hostnames=$PROD_SERVERS | |
config_text="CONFIG_CLASS_NAME=\'settings.ProdConfig\'" | |
;; | |
*) | |
echo [gitlab] update was on $branch branch. skip deployment and exit | |
exit | |
esac | |
for hostname in $hostnames; do | |
deploy_git_access_key $hostname | |
sync_changes $hostname $branch | |
apply_changes $hostname $branch $config_text | |
track_on_graphite $hostname $branch | |
done | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment