Created
December 9, 2013 19:17
-
-
Save brianfeister/7879079 to your computer and use it in GitHub Desktop.
Example fabric deploy tasks with git on prod server via github
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fabric.api import env, run, cd, task, get | |
from fabric.colors import * | |
from time import gmtime, strftime | |
import subprocess, os | |
global tag_time | |
tag_time = strftime("%Y-%m-%d-%H-%M-%S", gmtime()) | |
global www_prod | |
www_prod = '/srv/www' | |
env.user = "linux-user" | |
env.hosts = ["server-name"] | |
env.use_ssh_config = True | |
@task | |
def deploy(branch='master'): | |
global www_prod | |
with cd(www_prod): | |
# Always see what's latest | |
git("fetch origin") | |
# Check to see if there's any different | |
output = git("diff --shortstat origin/%s" % branch) | |
# This diff comes clean... but we have to use a ghetto hack to see it | |
if not output.splitlines()[1]: | |
output = git("rev-parse %s" % branch) | |
sha = output.stdout | |
msg( "No deploy necessary. %s is up to date (%s)." % (branch,sha), 'success' ) | |
return; | |
else: | |
# The diff failed, so we're probably not checking out against origin | |
if output.failed is True: | |
msg( "No remote branch found." ) | |
# Create a tag we can revert to. | |
else: | |
print output | |
global tag_time | |
deploying_master = True | |
git("tag %s" % tag_time ) | |
# Attempt to check things out | |
output = git("checkout -f %s" % branch) | |
if output.failed is True: | |
msg("Checkout failed for %s" % branch ) | |
exit() | |
# these branches can be fast-forwarded | |
safe_branches = ['master','develop','devops'] | |
for safe_branch in safe_branches: | |
if branch == safe_branch: | |
git("pull --rebase origin "+safe_branch) | |
git("submodule update --init --recursive") | |
git("submodule foreach --recursive 'git reset --hard'") | |
output = git("rev-parse %s" % branch) | |
sha = output.stdout | |
run("wp rewrite flush --soft",quiet=True) | |
msg("Flushed rewrite rules.") | |
success = "Project deployed to %s (%s)." % (branch,sha) | |
if 'deploying_master' in locals(): | |
success += " Revert with 'deploy:%s'" % tag_time | |
msg( success, 'success' ) | |
@task | |
def provision(): | |
run('sudo salt-call --local --config=/srv/www/config state.highstate') | |
msg( 'Server has been provisioned.', 'success' ) | |
def msg(message,msg_type="run"): | |
if "run" == msg_type: | |
output = blue( "Running: " ) + message | |
elif "success" == msg_type: | |
output = green( "Success: " ) + message | |
elif "error" == msg_type: | |
output = red( "Error: " ) + message | |
if env.host_string: | |
output = '[' + env.host_string + '] ' + output | |
print output | |
def git(command): | |
git_command = "git %s" % command | |
msg( git_command ) | |
return run( git_command, quiet=True ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment