It's been awhile since my last blog entry and one of the reasons for that was my miserable deployment setup. This all started with how I wanted blog set up. The goals were for the entire site to be able to run on any machine I'm using with minimal configuration and setup while also making sure all my content is under version control. Those goals have been achived and all I have to do to get set up on a new machine, whether it's a Windows or Mac machine, is clone the repository, run
npm install and then fire it up with node. For public hosting I got everything set up on AWS which worked out nicely with minimal configuration and no worry about Heroku shutting down your server when it's inactive.
The miserable part about deployment was this manual process:
- ssh onto the machine
- git reset the local db changes
- git pull
- npm install
- restart ghost
Wouldn't it be nice if that was automated? Thankfully there's a not too difficult way to do this using GitHub Webhooks to hit a deploy endpoint and redeploy the site. I took this approach instead of trying to set up AWS as another git remote and then using a git hook to trigger a deploy because of the trouble I had hosting a git server on AWS. I'm happy with how simple this solution turned out to be too.
Step 1: Run Ghost as a Module
Thankfully, since the last time I blogged, Ghost has been updated and you can now run it as a module. This is great since we can add extra express routes to handle a deploy action. Here's what my index file ended up as, which as you can see is very similar to the tutorial.
Step 2: Create a Deploy Handler
When GitHub fires off a hook telling us that master has been updated we want to first check that something has actually been updated (so Joe Shmoe can't arbitrarily restart the site), then update our local source and restart the site. Bluebird nicely changes node callbacks into promises which makes this chain of async actions easy to follow.
Step 3: Set up GitHub
In your GitHub project settings under Webhooks & Services add a new Webhook with the url pointing to your deploy endpoint.
And that's it! When master is pushed github fires off a request to the site to redeploy and then magic happens, no logging in necessary. Check out the full source on GitHub