Skip to content

Instantly share code, notes, and snippets.

@dexterbt1
Created October 17, 2013 16:39
Show Gist options
  • Save dexterbt1/7028163 to your computer and use it in GitHub Desktop.
Save dexterbt1/7028163 to your computer and use it in GitHub Desktop.
Fabric helper function: deploy a git repo by pulling a clone locally, then create remote destination hidden repo + hooks + worktree, and lastly, push to it
def git_push(repo=None, branch='master', dest_name=None, dest_base_path='opt', host_string=None):
# create local clone
user, host, port = normalize(host_string)
tmpprojdir = os.path.join(tempfile.gettempdir(), 'deploy', host, 'port-'+port, user )
if not local('ls %s/%s/.git && echo OK; true' % (tmpprojdir, dest_name), capture=True).endswith('OK'):
local('mkdir -p %s' % tmpprojdir)
local('(cd %s && git clone -q %s %s && cd %s && git checkout branch)' % (tmpprojdir, repo, dest_name, dest_name, branch))
with lcd('%s/%s' % (tmpprojdir, dest_name)):
local('git fetch -q origin')
local('git reset -q --hard origin/%s' % branch )
user_home = run('pwd')
run('mkdir -p %s/%s' % (dest_base_path, dest_name))
if not run('test -d .gitpush/%s.git && echo OK; true' % dest_name ).endswith('OK'):
### http://caiustheory.com/automatically-deploying-website-from-remote-git-repository
run('mkdir -p .gitpush/%s.git' % dest_name)
with cd('.gitpush/%s.git' % dest_name):
run('git init --bare -q')
run('git --bare update-server-info')
run('git config --bool core.bare false')
run('git config --path core.worktree %s/%s/%s' % (user_home, dest_base_path, dest_name))
run('git config receive.denycurrentbranch ignore')
run("""echo '#!/bin/sh' > hooks/post-receive""")
run("""echo 'git checkout -f' >> hooks/post-receive""")
run('chmod 755 hooks/post-receive')
with lcd('%s/%s' % (tmpprojdir, dest_name)):
if not local('git remote | grep dest | head -n1', capture=True).endswith('dest'):
local('git remote add dest ssh://%s@%s:%s%s/.gitpush/%s.git' % (user, host, port, user_home, dest_name))
local('git push dest +master:refs/heads/%s' % branch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment