Steven Mercatante

Steven Mercatante

Publishing and Deploying for Pelican with Fabric

I recently switched to Pelican to build this site. I'm very happy with everything it offers, but wanted to tweak how the site gets deployed to the remote server. This is where Fabric comes in. Fabric is a "library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks." I've used Fabric for plenty of automation tasks before, so I knew it'd fit the bill perfectly.

If you use the pelican-quickstart command, it offers to build a fabfile.py that comes with a publish method. I just needed to change the default implementation; I wanted to push everything to GitHub and then have the remote server pull down the changes.

Here's what I ended up with:

@hosts(production)
def publish():
local('pelican -s publishconf.py')
local('git add .')
local('git commit -am "Automated publish - {}"'.format(time.strftime('%m/%d/%Y %H:%M')))
local('git push')
with cd(dest_path):
run("git pull origin master")

(dest_path should already be defined in fabfile.py for you, but you'll need to import time.)

I could've made it simpler by rsyncing the output directory, but I knew that I was going to keep a backup of everything on GitHub, and I like the idea of keeping the entire project source on the remote server in case I ever want to SSH in and make changes directly.