Skip to content

Instantly share code, notes, and snippets.

@sunny
Last active September 5, 2015 00:24
Show Gist options
  • Save sunny/407687 to your computer and use it in GitHub Desktop.
Save sunny/407687 to your computer and use it in GitHub Desktop.
$ git deploy
#!/bin/bash
# Git push then pull over ssh
#
# Supposing you have these environments on the same ssh git remote:
# project/origin.git
# project/dev/.git
# project/prod/.git
#
# You can then push the current branch and pull it in dev/ and prod/ by doing:
# $ git deploy dev
# $ git deploy prod
#
# Installation with ~/bin in your $PATH:
# $ curl http://gist.github.com/raw/407687/git-deploy.sh > ~/bin/git-deploy
# $ chmod +x ~/bin/git-deploy
# $ git config --global alias.deploy '!git-deploy'
set -e
info(){
echo -e "* \033[1m""$@""\033[0m"
}
REMOTE='origin'
ENV=${1:-dev} # first argument or default to dev
# For local scripts before deploy (knock, for example)
if [ -f __scripts/deploy_before ]; then
info "__scripts/deploy_before $ENV"
__scripts/deploy_before "$ENV"
fi
BRANCH=`git branch 2> /dev/null | sed -n '/^\*/s/^\* //p'`
REMOTE_URL=`git config --get remote.$REMOTE.url`
HOST=${REMOTE_URL%%:*}
DIR=${REMOTE_URL#*:}
ENV_DIR="${DIR%/*}"/$ENV
info "Tag $ENV"
git tag -f $ENV
info "Sending $BRANCH"
git push $REMOTE $BRANCH
git push $REMOTE -f --tags
ssh -T $HOST "
set -e
cd $ENV_DIR
git fetch $REMOTE
CURRENT_BRANCH=\`git branch 2> /dev/null | sed -n '/^\*/s/^\* //p'\`
(git branch 2> /dev/null | grep -n '^ $BRANCH') && HAS_BRANCH=1 || HAS_BRANCH=0
# Change branch
if [ $BRANCH != \$CURRENT_BRANCH ]; then
# Create remote tracked branch
if [ \$HAS_BRANCH != 1 ]; then
echo '** Creating $BRANCH branch in $ENV'
git checkout -t $REMOTE/$BRANCH
# Switch to it
else
echo '** Switching to $BRANCH branch in $ENV'
git checkout $BRANCH --
fi
fi
# Pull
echo '** Pulling $BRANCH in $ENV'
git merge $REMOTE/$BRANCH --ff-only
# For Passenger-like apps
if [ -f 'tmp/restart.txt' ]; then
echo '** Restarting'
touch tmp/restart.txt
fi
"
# For compile scripts (Compass, CoffeeScript, etc…)
if [ -f __scripts/compile ]; then
info "__scripts/compile $ENV"
ssh -T $HOST "cd $ENV_DIR && __scripts/compile $ENV"
fi
# For local extra deploy scripts (database, for example)
if [ -f __scripts/deploy ]; then
info "__scripts/deploy $ENV"
__scripts/deploy "$ENV"
fi
@sunny
Copy link
Author

sunny commented May 21, 2010

Thanks, fixed ! Guess I have to learn some new shell syntax

I tried making all this into just a single git alias, didn't manage :(

@mpg
Copy link

mpg commented May 21, 2010

For shell syntax reference, I tend to prefer this one for portability or this one for personal use.

No idea about making it a single git alias...

@sunny
Copy link
Author

sunny commented May 21, 2010

My problem with making it a single git alias was passing parameters (made it work with deploy = !sh -c "… $1" -) and escaping enough so that we I pass the full cd and git command to ssh…

@sunny
Copy link
Author

sunny commented May 26, 2011

Brand new and fresh version that creates remote tracked branches \o/

@sunny
Copy link
Author

sunny commented Jan 21, 2013

New version that updates a tag with the name of the deployed direction.

For example git deploy prod adds a prod tag.

@sunny
Copy link
Author

sunny commented Jan 21, 2013

Now after a deploy calls __scripts/compile remotely and __scripts/deploy locally.

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