Created
March 15, 2016 17:33
-
-
Save arcostasi/1b00ff992a0e14850f58 to your computer and use it in GitHub Desktop.
git automatic push post-receive
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/sh | |
# Application Name: | |
export DEPLOY_APP_NAME=`NOME DO APP` | |
# This is the root deploy dir. | |
export DEPLOY_ROOT="/usr/local/www/application_web" | |
# When receiving a new git push, the received branch gets compared to this one. | |
# If you do not need this, just add a comment | |
export DEPLOY_ALLOWED_BRANCH="master" | |
# You could use this to do a backup before updating to be able to do a quick rollback. | |
# If you need this just delete the comment and modify to your needs | |
#PRE_UPDATE_CMD='cd ${DEPLOY_ROOT} && backup.sh' | |
# Use this to do update tasks and maybe service restarts | |
# If you need this just delete the comment and modify to your needs | |
#POST_UPDATE_CMD='cd ${DEPLOY_ROOT} && make update' | |
########################################################################################### | |
export GIT_DIR="$(cd $(dirname $(dirname $0));pwd)" | |
export GIT_WORK_TREE="${DEPLOY_ROOT}" | |
IP="$(ip addr show eth0 | grep 'inet ' | cut -f2 | awk '{ print $2}')" | |
echo "githook: $(date): Welcome to '$(hostname -f)' (${IP})" | |
echo | |
# Make sure directory exists. Maybe its deployed for the first time. | |
mkdir -p "${DEPLOY_ROOT}" | |
# Loop, because it is possible to push more than one branch at a time. (git push --all) | |
while read oldrev newrev refname | |
do | |
export DEPLOY_BRANCH=$(git rev-parse --symbolic --abbrev-ref $refname) | |
export DEPLOY_OLDREV="$oldrev" | |
export DEPLOY_NEWREV="$newrev" | |
export DEPLOY_REFNAME="$refname" | |
if [ ! -z "${DEPLOY_ALLOWED_BRANCH}" ]; then | |
if [ "${DEPLOY_ALLOWED_BRANCH}" != "$DEPLOY_BRANCH" ]; then | |
echo "githook: Branch '$DEPLOY_BRANCH' of '${DEPLOY_APP_NAME}' application will not be deployed. Exiting." | |
exit 1 | |
fi | |
fi | |
if [ ! -z "${PRE_UPDATE_CMD}" ]; then | |
echo | |
echo "githook: PRE UPDATE (CMD: '${PRE_UPDATE_CMD}'):" | |
eval $PRE_UPDATE_CMD || exit 1 | |
fi | |
# Make sure GIT_DIR and GIT_WORK_TREE is correctly set and 'export'ed. Otherwhise | |
# these two environment variables could also be passed as parameters to the git cli | |
echo "githook: I will deploy '${DEPLOY_BRANCH}' branch of the '${DEPLOY_APP_NAME}' project to '${DEPLOY_ROOT}'" | |
git checkout -f "${DEPLOY_BRANCH}" || exit 1 | |
git reset --hard "$DEPLOY_NEWREV" || exit 1 | |
if [ ! -z "${POST_UPDATE_CMD}" ]; then | |
echo | |
echo "githook: POST UPDATE (CMD: '${POST_UPDATE_CMD}'):" | |
eval $POST_UPDATE_CMD || exit 1 | |
fi | |
done | |
echo | |
echo "githook: $(date): See you soon at '$(hostname -f)' (${IP})" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment