Skip to content

Instantly share code, notes, and snippets.

@darwing1210
Last active August 9, 2019 23:29
Show Gist options
  • Save darwing1210/d28d3b4fd972200828955bb1c8b87347 to your computer and use it in GitHub Desktop.
Save darwing1210/d28d3b4fd972200828955bb1c8b87347 to your computer and use it in GitHub Desktop.
A Python Fabfile to deploy a wordpress site over SSH
import os
from time import time
from fabric.api import sudo, run, task, env, cd, get, local
from fabric.context_managers import shell_env
env.hosts = ['yourhost']
env.user = 'yoursshuser'
env.password = 'yoursshpassword'
# env.key_filename = '~/yourkey.pem'
REPO_PATH = "/var/www/repopath"
THEME_PATH = "{0}/wp-content/themes/yourtheme".format(REPO_PATH)
UPLOADS_PATH = "{0}/wp-content/uploads".format(REPO_PATH)
LOCAL_DOMAINS = ['http://domain.local', '//localhost:3000']
DEV_DOMAIN = 'http://{0}'.format(env.hosts[0])
POST_EXPORT_PATH = 'posts-exports/'
@task
def download_files():
local('ssh-add {0}'.format(env.key_filename))
local('rsync -avz --progress {1}@{0}:{2}/* ./wp-content/uploads/'\
.format(env.hosts[0], env.user, UPLOADS_PATH)
)
@task
def upload_files():
local('ssh-add {0}'.format(env.key_filename))
local('rsync -avz --progress --no-perms --omit-dir-times ./wp-content/uploads/* {1}@{0}:{2}/'\
.format(env.hosts[0], env.user, UPLOADS_PATH)
)
@task
def search_replace():
for domain in LOCAL_DOMAINS:
run('wp search-replace "{0}" "{1}" --all-tables'.format(domain, DEV_DOMAIN))
@task
def update_site():
upload_files()
with cd(REPO_PATH):
run('git pull origin master')
search_replace()
with cd(THEME_PATH):
run('gulp dist')
run('exit')
@task
def sync_posts(*posts): # Post IDs as arguments example. fab sync_posts:621,79
for post in posts:
local('wp export --dir={0} --post__in={1} --filename_format={1}.xml'.format(POST_EXPORT_PATH, post))
local('git add {0}'.format(POST_EXPORT_PATH))
local("git commit -m 'Adding Posts {0}'".format(posts))
local('git push origin master')
with cd(REPO_PATH):
run('git pull origin master')
for post in posts:
run('wp post delete {0} --force'.format(post))
run('wp import {0}{1}.xml --authors=create'.format(POST_EXPORT_PATH, post))
search_replace()
upload_files()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment