Skip to content

Instantly share code, notes, and snippets.

@mm53bar
Last active December 14, 2015 02:49
Show Gist options
  • Save mm53bar/5016199 to your computer and use it in GitHub Desktop.
Save mm53bar/5016199 to your computer and use it in GitHub Desktop.
Simple bash based deployment script
# deploy to:
SERVER=server.com
APP=app_name
DEPLOY=/srv/$APP
ENVIRONMENT=production
# clone source from:
REPO=ssh://[email protected]/username/repo.git
BRANCH=master

Bash Deployment

Run bin/deploy!

Config Settings

Add these to an .env file:

 # deploy to:
 SERVER=server.com
 APP=app_name
 DEPLOY=/srv/$APP
 ENVIRONMENT=production
 
 # clone source from:
 REPO=ssh://[email protected]/username/repo.git
 BRANCH=master

Branches

If you want to deploy a branch, just call bin/deploy --rev=branch_name.

What's next?

This is really just a proof of concept. Extra flags could be added and more functions could be to come. But this proves how easy it is to start.

#!/usr/bin/env bash
set -e
# get config settings
source .env
# process arguments
while test $# != 0; do
case "$1" in
--rev=*)
REF="${1#--rev=}"
esac
shift
done
echo "Processing deploy"
if [ -z "$REF" ]; then
REMOTE_REF=origin/$BRANCH
else
REMOTE_REF=origin/$REF
fi
scp .env $APP@$SERVER:$DEPLOY/.env
ssh $APP@$SERVER "sh -l -c 'cd $DEPLOY && \
git fetch origin && git reset --hard $REMOTE_REF && \
(git rev-parse HEAD > $DEPLOY/REVISION) && \
bin/bundle install --deployment --quiet --binstubs \
--without development test && \
bin/foreman run rake assets:precompile && \
bin/bluepill --no-privileged -c $DEPLOY/tmp restart'"
#!/usr/bin/env bash
set -e
# get config settings
source .env
echo "Migrating database"
ssh $APP@$SERVER "sh -l -c 'cd $DEPLOY && \
bin/foreman run rake db:migrate'"
@mvandenbeuken
Copy link

Looking good. If you add in some database migration handling it'd cover most deployment scenarios.

@mm53bar
Copy link
Author

mm53bar commented Feb 23, 2013

@mvandenbeuken Just updated it to be a bash script instead of a sh script and to use the .env file rather than specify its own settings.

By using the .env file (which we use already with foreman), I can now create a separate migrate script that uses the same settings.

@mm53bar
Copy link
Author

mm53bar commented Feb 23, 2013

Just added the migrate script

@mm53bar
Copy link
Author

mm53bar commented Feb 24, 2013

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