Skip to content

Instantly share code, notes, and snippets.

@armandfardeau
Last active July 13, 2018 17:12
Show Gist options
  • Select an option

  • Save armandfardeau/8cba1f16a12dd27da5a7edfd86b603f8 to your computer and use it in GitHub Desktop.

Select an option

Save armandfardeau/8cba1f16a12dd27da5a7edfd86b603f8 to your computer and use it in GitHub Desktop.
Rails and Sidekiq post-receive deploy
git init --bare repo.git
vim hooks/post-receive
chmod ug+x hooks/post-receive
#!/bin/bash
# Edit your configuration below
SITE_NAME="Foo"
PRODUCTION_PATH="/var/www/${SITE_NAME}"
STAGING_PATH="/var/www/${SITE_NAME}_staging"
BRANCH="production"
# Use figlet to display fancy banner
if !(command -v figlet >/dev/null 2>&1 ); then
echo "You should install figlet"
exit 1;
fi
echo -e "$SITE_NAME" | figlet;
echo "**** $SITE_NAME [post-receive] hook received. ****"
while read oldrev newrev ref
do
branch_received=`echo $ref | cut -d/ -f3`
echo "**** Received [$branch_received] branch. ****"
# Making sure we received the branch we want.
if [ $branch_received = $BRANCH ]
then
DIR=$PRODUCTION_PATH
else
DIR=$STAGING_PATH
fi
echo "**** Deploying in $DIR directory. ****"
cd $DIR
# Unset to use current working directory.
unset GIT_DIR
echo "**** Checking out branch. ****"
GIT_WORK_TREE=$DIR git checkout -f $BRANCH
done
# prepare app for production
if (command -v rbenv >/dev/null 2>&1 ); then
eval "$(rbenv init -)"
fi
RAILS_ENV=production bundle install
RAILS_ENV=production bundle exec rake db:migrate
RAILS_ENV=production bundle exec rake assets:precompile
# restart passenger
passenger-config restart-app .
# restart sidekiq
pgrep -f sidekiq | awk '{print "kill -9 " $1}' | sh
RAILS_ENV=production bundle exec sidekiq -d -L log/sidekiq.log -C config/sidekiq.yml -e production
echo -e "****\nDone\n****"| figlet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment