Skip to content

Instantly share code, notes, and snippets.

@smashwilson
Last active December 31, 2015 18:59
Show Gist options
  • Save smashwilson/8030278 to your computer and use it in GitHub Desktop.
Save smashwilson/8030278 to your computer and use it in GitHub Desktop.
This is how I do deployments on `git push` for azurefire.net.
# ${HOME}/bin/azure-deploy
#!/bin/sh
#
# Deployment script for azurefire.
AZURE_HOME="/var/webapp/azurefire"
export PATH=${PATH}:${HOME}/.rbenv/bin:${HOME}/.rbenv/shims
AZURE_GIT="--git-dir ${AZURE_HOME}/.git --work-tree ${AZURE_HOME}"
echo ">> Deploying azurefire."
# Pull the latest code.
git ${AZURE_GIT} pull --ff-only origin master
echo ">> Updating dependencies."
# Re-run bundler.
cd ${AZURE_HOME}
bundle install --deployment --binstubs
# Make sure everything is readable by www-data.
# With a giant hammer.
sudo chown -R :www-data ${AZURE_HOME}
echo ">> Recompiling sass templates."
bundle exec rake sass
echo ">> Kicking the unicorn."
# Kick the unicorn
sudo kill -USR2 `cat ${AZURE_HOME}/tmp/pids/unicorn.pid`
echo ">> Success!"
# /var/git/azurefire.git/hooks/post-receive
#!/bin/bash
MASTER=0
while read oldrev newrev refname
do
BRANCH=$(git rev-parse --symbolic --abbrev-ref ${refname})
if [ "${BRANCH}" == "master" ]; then
MASTER=1
fi
done
if [ "${MASTER}" != "1" ]; then
exit 0
fi
# New code has been pushed to master. Update the live checkout and ping Unicorn for a restart.
DEPLOY=${HOME}/bin/azure-deploy
if [ ! -x ${DEPLOY} ]; then
echo "Unable to execute ${DEPLOY}."
exit 0
fi
exec ${DEPLOY}

Setup

  1. bare repository in /var/git/azurefire.git

    initialized with:

    git clone --bare git@github.com:smashwilson/azurefire.git /var/git/azurefire.git
    
  2. live working copy in /var/webapp/azurefire

    initialized with:

    git clone /var/git/azurefire.git /var/webapp/azurefire
    
  3. set up the post-receive hook in the bare repository

    See /var/git/azurefire.git/hooks/post-receive below, which calls ${HOME}/bin/azure-deploy to finalize the deployment. I'm not sure why I put azure-deploy in my home directory; probably so I can trigger it manually?

    Also, these should probably be running set -e to fail on the first nonzero exit. Hmm.

  4. to do a deployment, add the bare repository as a remote:

git remote add live smash@azurefire.net:/var/git/azurefire.git

and trigger deployment with a push:

git push live master
@rgbkrk
Copy link

rgbkrk commented Dec 18, 2013

kick the unicorn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment