Deploying with Fabric and Subversion

Deploying sites with Fabric is a cinch.

Here’s a sample fabfile for deploying to production and staging systems directly from Subversion.

def production():
    config.fab_hosts = ['server1.example.com', 'server2.example.com']
    config.app_path = '/path/to/app/'

def staging():
    config.fab_hosts = ['staging.example.com']
    config.app_path = '/path/to/app/'

def svn_update():
    "Updates the repository."
    sudo("cd $(app_path); svn up")

def reboot():
    "Reboot Apache2 server."
    sudo("/usr/sbin/apachectl restart")

def update():
    require('fab_hosts', provided_by=[production])
    invoke(svn_update)

def deploy():
    require('fab_hosts', provided_by=[production])
    invoke(svn_update)
    invoke(reboot)

Now, deploying is easy. After committing my changes to Subversion, I simply instantiate:

bash$ fab production deploy

This is equivalent to:

bash$ fab production update reboot