Easy SSH deployment with Python fabric

By: Juraj Marusiak Published: Aug 24, 2016

This article will explain a process of an easy deployment with python fabric


For a long time already, I was trying to find a decent way of deploying my web applications on to my hosting. I have tried several solutions, most of the time based on my custom scripts. Last time I also tried an open source PHP package called Rocketeer, but none of those solutions where sufficient. Some of them were too complex and some of them were not reliable enough. At the end, I ended up with a python solution - a library called python fabric.


Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks. I find it's power in the simplicity.


This is how my deployment script looks like (it's a fabfile.py file in the root of my project):


from __future__ import with_statement
from fabric.api import *

env.hosts = ["www.mydomain.sk"]
env.user = "username"
env.password = "password"
deploy_dir = "/home/username/www/"

def commit():
  local("git add -p && git commit")

def push():
  local("git push")

def prepare_deploy():

def deploy():
  with settings(warn_only=True):
    run("git clone git@bitbucket.org:username/cms.git %s" % deploy_dir)
  with cd(deploy_dir):
    run("git pull")
    run("composer install")


I can then execute the following:


# fab prepare_deploy


This will make sure, that all my local work is ready to be deployed. First my work is commited locally and then deployed to my Bitbucket VCS server. Aftery everything is prepared, I simply run:


# fab deploy


What this script does is, it SSH on to my www.mydomain.sk server, it pulls my source code from my repository and it installs all necessary project dependencies, whether it's composer, npm, bower or others ...


Easy as that.

© 2016 Yuma.sk