Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrittonWinterrose/c0285f3dddb8f123f1c82b6ae63a7c96 to your computer and use it in GitHub Desktop.
Save BrittonWinterrose/c0285f3dddb8f123f1c82b6ae63a7c96 to your computer and use it in GitHub Desktop.
7 easy steps to automated git push deployments. With small and configurable bash only post-receive hook

How-to setup a simple git push deployment

Questions check out the original here: https://gist.github.com/thomasfr/9691385

On the server (example.com)

  1. We use the user account created in cpanel.

  2. Use the SSH public key used to connect via Putty and FTP.

  3. Create a git bare repo for your project:

cd /home/appAccount/var/git
$ mkdir AppName
$ cd AppName
## /home/git/testapp
$ git init --bare
  1. Copy the post-receive script from this gist to the hooks dir of the created bare repo.
$ vim AppName/hooks/post-receive
## Paste the post-receive script from this gist and save
## If you do not need to execute a 'build' and/or 'restart' command,
## just delete or comment the lines 'UPDATE_CMD' and 'RESTART_CMD'
$ chmod +x testapp/hooks/post-receive
  1. Set ownership and permissions of the DEPLOY_ROOT directory manually.
Set Octal to 0775

On the client

  1. Create a git repo and add our newly created remote:
$ mkdir testapp
$ cd testapp
$ git init
$ git remote add deploy SSH://username@serveripaddress:22/home/appdirectory/repositorydirectory

ex. ssh://pinkstra@199.168.173.244:22/home/pinkstra/var/git/PinkStrands

  1. Check remote locations to make sure it added deploysuccessfully:
  git remote -v
  1. Pull from master and push to production:
$ git checkout master
$ git pull
$ git push deploy master
pinkstra@199.168.173.244's password:
  stdin: is not a tty
  Counting objects: 231, done.
  Delta compression using up to 8 threads.
  Compressing objects: 100% (97/97), done.
  Writing objects: 100% (231/231), 205.67 KiB | 0 bytes/s, done.
  Total 231 (delta 158), reused 158 (delta 126)
  remote: hooks/post-receive: line 48: ip: command not found
  remote: githook: Tue May  3 15:13:31 CDT 2016: Welcome to 'server.donnabellahairextensions.com' ()
  remote:
  remote: githook: I will deploy 'master' branch of the '' project to '/home/pinkstra/var/git/PinkStrands'
  remote: Already on 'master'
  remote: HEAD is now at 3cc2567 fixed encrypted file decrypt by adding .travis 2
  remote:
  remote: githook: Tue May  3 15:13:34 CDT 2016: See you soon at 'server.donnabellahairextensions.com' ()
  To ssh://pinkstra@199.168.173.244:22/home/pinkstra/var/git/PinkStrands
   * [new branch]      master -> master
  • Repeat: Develop, test, commit and push :)
cd localmachine/AppDirectory
git pull
git push deploy master
**Enter SSH Passphrase**

Congratulations, you just setup git push deployment with automated build and service restart

#!/bin/bash
#
# Author: "FRITZ Thomas" <fritztho@gmail.com> (http://www.fritzthomas.com)
# GitHub: https://gist.github.com/thomasfr/9691385
#
# The MIT License (MIT)
#
# Copyright (c) 2014-2015 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.
#
# This is the root deploy dir.
export DEPLOY_ROOT="/home/pinkstra/var/git/PinkStrands"
# 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