Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save scottstanfield/9700098 to your computer and use it in GitHub Desktop.
Save scottstanfield/9700098 to your computer and use it in GitHub Desktop.
#!/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

== On the server ==

  • Create a user on the server, which we (the git client) uses to connect (push) to this server. I am using gid und uid higher than normally, if i want to restrict the user more than other user accounts, i could check for uid>10000 or gid>10000
sudo groupadd -g 10001 git
sudo useradd -u 10001 -g 10001 -d /home/git -m -s /usr/bin/git-shell git
  • Create a git bare repo for your project:
sudo -u git bash
cd ~
mkdir testapp
cd testapp
git init --bare
  • Copy the post-receive script from this gist to the hooks dir of the created bare repo.
vim testapp/hooks/post-receive
# Paste the script from this gist
chmod +x testapp/hooks/post-receive
  • Set ownership and permissions of the DEPLOY_ROOT directory:
sudo chown root:git -R /var/www
sudo chmod 775 /var/www
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment