Skip to content

Instantly share code, notes, and snippets.

@wadehammes
Last active February 2, 2016 02:54
Show Gist options
  • Save wadehammes/fecfe4e399e09b0b85cc to your computer and use it in GitHub Desktop.
Save wadehammes/fecfe4e399e09b0b85cc to your computer and use it in GitHub Desktop.
My fabfile for deploying my Wordpress instances.
from fabric.api import *
import datetime, time
env.colorize_errors = 'true'
### Remote Path ###
env.work_dir = '/var/www/html'
### Environment Utilities ###
@task
def _set_host(environment):
"""
Sets the environment to either local or the remote host specified.
If remote, specifies the project path and key file needed to connect.
"""
env.environment = environment
@task
def staging():
""" Use development server settings """
_set_host('staging')
env.hosts = ['user@ip:22']
@task
def production():
""" Use production server settings """
print('******************************')
print('******** WARNING ********')
print('******** PRODUCTION ********')
print('******************************')
_set_host('production')
env.hosts = ['user@ip:22']
### Get Date for Commit Message ###
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
### Server Commands ###
@task
def server_update():
""" Updates server with new packages """
with settings(warn_only=True):
with cd(env.work_dir):
check = raw_input('\nWarning you are about to deploy code to {}.\n'.format(
env.environment) + 'Please confirm the environment that you wish to '
'push to if you want continue: ')
if check == env.environment:
run('sudo apt-get -y update')
run('sudo apt-get -y upgrade')
run('sudo apt-get -y dist-upgrade')
@task
def update():
""" Updates NPM modules with new packages """
with cd(env.work_dir):
run('rm -rf node_modules')
run('npm install')
run('gulp build')
@task
def status():
""" Navigates to the site directory and executes `git status` """
with cd(env.work_dir):
run('git status')
@task
def pull(branch):
""" Navigates to the site directory and executes `git pull` """
with cd(env.work_dir):
run('git add .')
run('git pull origin {}'.format(branch))
run('gulp build')
@task
def push(branch):
""" Navigates to the site directory and executes `git push origin <branch>` """
with cd(env.work_dir):
run('git push origin {}'.format(branch))
@task
def deploy(branch='master'):
""" Navigates to the site directory and deploys code (master branch on production)` """
with settings(warn_only=True):
with cd(env.work_dir):
check = raw_input('\nWarning you are about to deploy code to {}.\n'.format(
env.environment) + 'Please confirm the environment that you wish to '
'push to if you want continue: ')
if check == env.environment:
if env.environment == 'staging':
run('git stash')
run('git reset --hard HEAD')
run('git checkout master')
run('git pull origin master')
run('git fetch')
if branch != 'master':
run('git branch -D {}'.format(branch))
run('git checkout {}'.format(branch))
run('git pull origin {}'.format(branch))
run('gulp build')
else:
run('git add -A')
run('git commit -m "Production Commit on {}"'.format(st))
run('git pull origin master')
run('git push origin master')
run('gulp build')
@task
def revert(commit='HEAD'):
""" Revert git via reset --hard <commit hash> """
with cd(env.work_dir):
run('git reset --hard {}'.format(commit))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment