Skip to content

Instantly share code, notes, and snippets.

@pospi
Last active August 29, 2015 13:55
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 pospi/8746364 to your computer and use it in GitHub Desktop.
Save pospi/8746364 to your computer and use it in GitHub Desktop.
A generic git post-receive hook for auto-deploying applications when pushed to.
#!/bin/bash
#------------------------------------------------------------------------------#
# CONFIGURATION
# You will need the following variables set in the git repository to deploy from:
# git config --bool receive.denyCurrentBranch false
# git config --bool core.bare false
# git config --path core.worktree [DEPLOYMENT_DIR]
#------------------------------------------------------------------------------#
# Read standard input to work out the branch (or tag) we've just pushed
while read oldrev newrev refname
do
DEPLOY_BRANCH=`echo $refname | cut -d/ -f3`
done
echo "Pushed branch $DEPLOY_BRANCH"
# Setup environment for git
unset GIT_DIR
DEPLOY_DIR=`git config --get core.worktree`
# Check required directories are present
if [ ! -d "$DEPLOY_DIR" ]; then
echo "Could not locate deployment directory $DEPLOY_DIR"
exit 1
fi
# move into working copy
cd $DEPLOY_DIR
# Check for local modifications, abort and notify if found
git diff --quiet
EXIT_CODE=$?
if [[ $EXIT_CODE -ne 0 ]]; then
echo "Local changes to tracked files detected, aborting deployment. Files changed:"
git diff --name-only
exit 1
fi
# Run the deployment of the main repo
git checkout -f $DEPLOY_BRANCH
# Run the deployment for submodules
git submodule update --init --recursive --force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment