To help facilitate my blogging workflow, I wanted to go from written to published post quickly. My general workflow for writing a post for this blog looks like this:

  1. Create a post in _posts
  2. Write the post
  3. Run fab sync

Here is the repo

fab sync is a custom command that uses the magic of Fabric to stage, commit and push changes in my blog repo to Github. Next, Fabric uses an ssh session in the Python process to connect to the server on which my blog is hosted, pull down the newest changes from the blog repo and finally, build the Jekyll blog so that the changes are immediately reflected on this site.

A nice part about Fabric is it requires relatively little code to accomplish a lot. It also integrates with the system ssh config so you don’t need to supply server credentials in yet another place, you can just configure the Fabric env hosts.

 1 from __future__ import with_statement
 2 from fabric.api import local, run, cd, env, abort
 3 from fabric.contrib.console import confirm
 4 
 5 import os
 6 
 7 # path to blog on my server
 8 from local_config import BLOG_DIR
 9 
10 env.BLOG_DIR = BLOG_DIR
11 env.use_ssh_config = True
12 
13 # specfic Host from ssh config
14 env.hosts = ['dod']
15 
16 def add():
17     '''
18     Check git branch status.
19     If branch is dirty, add all unstaged files.
20     '''
21     res = local('git status', capture=True)
22     if 'Untracked files' in res or 'Changes not staged for commit' in res:
23         print res
24         if confirm('Changes not staged. Add files?'):
25             local('git add .')
26         else:
27             abort('Aborted. Changes not staged.')
28 
29 
30 def commit():
31     local("git commit")
32 
33 
34 def push(b_from='origin', b_to='master'):
35     local("git push -u {} {}".format(b_from, b_to))
36 
37 
38 def deploy():
39     '''
40     Pull down changes from repo and build the blog on the server
41     '''
42     code_dir = env.BLOG_DIR
43     with cd(code_dir):
44         res = run("git pull")
45         if not 'Already up-to-date.' in res:
46             run("jekyll build")
47 
48 
49 def sync():
50     add()
51     commit()
52     push()
53     deploy()

Happy automating!