|
#!/bin/bash |
|
# |
|
# Author: "FRITZ Thomas" <[email protected]> (http://www.fritzthomas.com) |
|
# GitHub: https://gist.github.com/thomasfr/9691385 |
|
# |
|
# The MIT License (MIT) |
|
# |
|
# Copyright (c) 2014 FRITZ Thomas |
|
# |
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
|
# of this software and associated documentation files (the "Software"), to deal |
|
# in the Software without restriction, including without limitation the rights |
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
# copies of the Software, and to permit persons to whom the Software is |
|
# furnished to do so, subject to the following conditions: |
|
# |
|
# The above copyright notice and this permission notice shall be included in all |
|
# copies or substantial portions of the Software. |
|
# |
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
# SOFTWARE. |
|
# |
|
|
|
|
|
# Received branch will be checked against this. Only deploy when receiving this branch. |
|
DEPLOY_BRANCH="master" |
|
|
|
# This is the root deploy dir. |
|
DEPLOY_ROOT="/var/www" |
|
|
|
# This will be 'eval'ed after git checkout -f step. |
|
# Use this to call your build/update script or run make, grunt, gulp, etc. |
|
# If empty or not set this step gets ignored. |
|
UPDATE_CMD='cd "${DEPLOY_TO}" && make "update"' |
|
|
|
# Command gets executed after 'UPDATE_CMD' has finished. Use this to restart running services |
|
# or daemons. If empty or unset this step gets ignored. |
|
RESTART_CMD='sudo systemctl restart "${PROJECT_NAME}.service" && sudo systemctl status "${PROJECT_NAME}.service"' |
|
|
|
|
|
########################################################################################### |
|
|
|
export GIT_DIR="$(cd $(dirname $(dirname $0));pwd)" |
|
export PROJECT_NAME="${GIT_DIR##*/}" |
|
export DEPLOY_TO="${DEPLOY_ROOT}/${PROJECT_NAME}" |
|
export GIT_WORK_TREE="${DEPLOY_TO}" |
|
IP="$(ip addr show eth0 | grep 'inet ' | cut -f2 | awk '{ print $2}')" |
|
|
|
|
|
echo "+++++++++++++++++++++++ Welcome to '${HOSTNAME}' (${IP}) ++++++++++++++++++++++++" |
|
echo |
|
|
|
# Make sure directory exists. Maybe its deployed for the first time. |
|
mkdir -p "${DEPLOY_TO}" |
|
|
|
# Loop, because it is possible to push more than one branch at a time. (git push --all) |
|
while read oldrev newrev refname |
|
do |
|
|
|
branch=$(git rev-parse --symbolic --abbrev-ref $refname) |
|
|
|
if [ "${DEPLOY_BRANCH}" == "$branch" ]; then |
|
# 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 '${PROJECT_NAME}' project to '${DEPLOY_TO}'" |
|
git checkout -f |
|
|
|
if [ ! -z "${UPDATE_CMD}" ]; then |
|
echo |
|
echo "githook: UPDATE (CMD: '${UPDATE_CMD}'):" |
|
eval $UPDATE_CMD |
|
fi |
|
|
|
if [ ! -z "${RESTART_CMD}" ]; then |
|
echo |
|
echo "githook: RESTART (CMD: '${RESTART_CMD}'):" |
|
eval $RESTART_CMD |
|
fi |
|
|
|
else |
|
echo "githook: I am NOT going to deploy the '${branch}' branch of the '${PROJECT_NAME}' project. Expected branch to deploy is '${DEPLOY_BRANCH}'." |
|
fi |
|
|
|
done |
|
|
|
echo |
|
echo "++++++++++++++++++++ See you soon at '${HOSTNAME}' (${IP}) ++++++++++++++++++++++" |
|
exit 0 |