Skip to content

Instantly share code, notes, and snippets.

@grega
Last active April 19, 2021 13:47
Show Gist options
  • Save grega/7035605 to your computer and use it in GitHub Desktop.
Save grega/7035605 to your computer and use it in GitHub Desktop.
Sample Fabric 'fabfile' for deploying to multiple hosts. Using: `fab deploy stage` or `fab deploy live`. Deploy to live prompts the user to confirm (with 'y' or 'n') that they actually want to deploy. Deploy tasks runs git pull & any number of utility commands (eg. asset compilation, checking / fixing folder permissions etc).
from __future__ import with_statement
from fabric.api import env, local, settings, abort, run, cd, sudo
from fabric.contrib.console import confirm
# Hosts
def stage():
env.user = 'deploy_user'
env.hosts = ['xxx.xxx.xxx.xxx:22']
global code_dir
code_dir = '/var/www/stage.website.com' # no trailing slash
def live():
if confirm('Are you sure you want to deploy to live?'):
env.user = 'deploy_user'
env.hosts = ['xxx.xxx.xxx.xxx:22']
global code_dir
code_dir = '/var/www/website.com' # no trailing slash
else:
abort('Aborting, not deploying to live')
# Tasks
def deploy():
with cd(code_dir):
# reset any local changes & grab the latest version
run('git reset --hard HEAD')
run('git pull')
# compile css
run('sass --style compressed --update sass/global.scss:css/style.css')
# minify app.js
run('php -f inc/jsmin.php')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment