When I setup Jekyll, I wanted to make it so it was possible to deploy new
posts automatically. I wanted to be able to drop a file in my _posts directory
in any of my Dropbox enabled devices and have the site rebuild itself. Plus,
I wanted my _drafts folder to go to my dev site that show my Draft posts.
The most obvious solution would be to setup a cron job that runs every 5 or 10 minutes. Of course, not only does this make the site build unnecessarily, but it also kills any immediate satisfaction of seeing my posts immediately. I needed to find another way.
Incron to the Rescue!
After some clever Google-fu, I stumbled across incron. The website doesn’t quite do it justice, or even do a good job of explaining what it does. The basic concept is that it’s a cron-like daemon, but it fires if there are file system changes. A file is added, removed, edited, etc. – see where this is going?
To get started, I installed incron from apt. Everyone that can use incron has to have their username added to /etc/incron.allow by the root user like so:
1
| |
Then as your user, fire up incrontab -e. After messing around with it
for a long time, I found the proper entry is something like:
1
| |
The first section is the directory to watch, which in my case is my
_posts directory. The next section are the various flags. In this case,
I want it to fire whenever a file is saved, moved or deleted. The
IN_NO_LOOP is especially important, because it will allow my script to
only run once at a time, even if mutliple events fire. The last bit is
obviously the script that fires.
Because I use RVM system-wide, the script is a little complicated. This is because incron only sends a very minimal amount of environment variables to the script when it’s invoked. RVM relies heavily on environment variables, and since they’re not being set, we have to write them by hand. I haven’t found a better way – and I tried a lot of them. Here is my script… it’s using rvm installed ruby 1.9.2.
1 2 3 4 5 6 7 8 9 | |
I got the majority of this information from rvm info while logged in
as the same user that would run the script. I also pump the output to
tmp files for debugging purposes, because incron doesn’t really handle
any script output at all – it’s terribly difficult to debug.
That being said, except for a few hiccups, it’s a nice stable system. I now have my site automatically updating whenever I change a file, which is stupidly convenient.
Bonus Dropbox Trick
I won’t take credit for this one, as it was suggested by Dan Benjamin of 5by5. He’s a smart dude.
Chances are that you really don’t want to have your whole personal Dropbox sitting on a server somewhere that you might not have a full amount of control over. Of course, you do want to be able to edit your posts from various machines, and get them onto the server, though.
What to do? What to do?
It’s pretty simple, actually. Create a second Dropbox account for your site. Create a directory for your site, and put your posts and drafts directories in there. Then, share that directory with your main Dropbox account.
On the server, sync to the 2nd account instead so that you’re only syncing over just the files needed for that site. Don’t feel bad about this, though. You’re not really “getting one over” on Dropbox. Files in shared directories are subtracted from the quotas of ALL the users that share that directory, so in essence, except for some overhead for Dropbox having to keep track of a 2nd accounts details, you’re not really taking very much advantage of their kindness.
For more information on my interaction with Jekyll, check out the Jekyll tag on my blog here. I’d also love any Dropbox referrals you might be able to give, click here.