Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Script to init a git repository on a server to easily deploy several website via git (think Heroku!). Useful for Apache + PHP for instance. Bonus: Deployment versioning and custom "install.sh" script run if foud.
#!/bin/sh
# Copyright Sébastien Saunier (@ssaunier) - 2013
# License: MIT
#
# Script to run at the root of an apache folder where you want to deploy with git.
# It will create a repository where you can push, and set up a post-receive script
# with is run each time this server gets pushed a new version:
#
# 1. post-receive compute the new version number based on existing ones
# in the $DEPLOYMENT_TARGET_ROOT folder
# 2. post-receive checks out a fresh version of the repo to the newest version folder
# 3. post-receive keeps only last $VERSIONS_TO_KEEP versions and delete the other ones
# 4. post-receive executes the ./install.sh script contained in the repo, in the
# folder $APACHE_ROOT, passing as first argument $DEPLOYMENT_TARGET_ROOT/current
# 5. post-receive symlink the latest revision to $DEPLOYMENT_TARGET_ROOT/current
#
# Say you have to VHOSTS in your git repository:
# - site1
# - site2
# - install.sh
#
# On the server, in the $APACHE_ROOT, you will have this:
#
# - web.git -> the git repository
# - site1 -> symlinked to deployment/current/site1
# - site2 -> symlinked to deployment/current/site2
# - deployment
# +-- 3
# +-- 4
# +-- 5
# +-- 6
# +-- 7
# +-- current -> symlinked to 7
REPO_NAME=web.git
DEPLOYMENT_TARGET_ROOT=~/deployment
VERSIONS_TO_KEEP=5
APACHE_ROOT=~/
mkdir -p $REPO_NAME
cd $REPO_NAME
git --bare init
cat <<EOF >hooks/post-receive
#!/bin/sh
GREEN="\\033[1;32m"
NORMAL="\\033[0;39m"
DEPLOYMENT_TARGET_ROOT=$DEPLOYMENT_TARGET_ROOT
VERSION=1
if [ -d \$DEPLOYMENT_TARGET_ROOT ]; then
VERSION=\$((\`ls \$DEPLOYMENT_TARGET_ROOT | sort -n | tail -n 1\` + 1))
fi
DEPLOYMENT="\$DEPLOYMENT_TARGET_ROOT/\$VERSION"
mkdir -p \$DEPLOYMENT
echo "-----> Updating website"
echo -e " New version will be: \$GREEN\$VERSION\$NORMAL"
GIT_WORK_TREE=\$DEPLOYMENT git checkout -f
echo " New version in place, waiting to be symlinked"
echo "-----> Keeping only $VERSIONS_TO_KEEP last versions on server"
COUNTER=1
LAST_KEPT_VERSION=\$((\$VERSION-$VERSIONS_TO_KEEP-1))
until [ \$COUNTER -ge \$LAST_KEPT_VERSION ]; do
rm -rf \$DEPLOYMENT_TARGET_ROOT/\$COUNTER
let COUNTER+=1
done
for version in \`ls \$DEPLOYMENT_TARGET_ROOT | sort -n\`; do
echo " \`stat -c '%n %A %.19z' \$DEPLOYMENT_TARGET_ROOT/\$version\`"
done
echo "-----> Symlinking current version to version \$VERSION"
ln -sfn \$DEPLOYMENT "\$DEPLOYMENT_TARGET_ROOT/current"
echo "-----> Executing install script at \$DEPLOYMENT/install.sh"
cd \$APACHE_ROOT
\$DEPLOYMENT/install.sh \$DEPLOYMENT_TARGET_ROOT/current
echo " Install script completed."
echo "-----> Congratulations! The new version \$VERSION is now deployed!"
EOF
chmod +x hooks/post-receive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment