#!/bin/bash # # Original Source: gist.github.com/thomasfr/9691385 # Author: “FRITZ Thomas” <fritztho@gmail.com> (www.fritzthomas.com) # GitHub: gist.github.com/thomasfr/9691385 # # The MIT License (MIT) # # Copyright © 2014-2017 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. # export DEPLOY_APP_NAME=`whoami` export DEPLOY_ROOT=“${HOME}/work” export DEPLOY_ALLOWED_BRANCH=“master” export GIT_DIR=“$(cd $(dirname $(dirname $0));pwd)” export GIT_WORK_TREE=“${DEPLOY_ROOT}” #PRE_UPDATE_CMD='cd ${DEPLOY_ROOT} && predeploy.sh' POST_UPDATE_CMD='cd ${DEPLOY_ROOT} && chmod +x deploy.sh && ./deploy.sh'

IP=“$(ip addr show eth0 | grep 'inet ' | cut -f2 | awk '{ print $2}')” echo “GITSERVER: $(date): Welcome to '$(hostname -f)' (${IP})” && echo mkdir -p “${DEPLOY_ROOT}” # Ensure directory exists.

# 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 [ "$DEPLOY_NEWREV" = "0000000000000000000000000000000000000000" ]; then
  echo "GITSERVER: This ref has been deleted" && exit 1
fi

if [ "${DEPLOY_ALLOWED_BRANCH}" != "$DEPLOY_BRANCH" ]; then
  echo "GITSERVER: Branch '$DEPLOY_BRANCH' of '${DEPLOY_APP_NAME}' app will not be deployed. Exiting." && exit 1
fi

# eval $PRE_UPDATE_CMD || exit 1
echo "GITSERVER: deploying '${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
eval $POST_UPDATE_CMD || exit 1

done

echo && echo “GITSERVER: $(date): See you soon at '$(hostname -f)' (${IP})” exit 0