Automated deployment of my Ghost blogs

Ghost is one my favorite blogging platforms. I am running this blog on Ghost, and my wife just asked me to host other blogs as well. I decided to automate the deployment of my blogs, so that I can upgrade, track, and add new blogs easily. Another project of mine is hipops, an infrastructure deployment tool that I have been working that makes my life easier (more on that later). I figured the automated deployment of my Ghost blogs is the best candidate for hipops. Here is how I did it...

Ghost setup

My goal was to setup a parent ghost repo that I can fork from to create all of the other repos. This way when I update the source repo, I can automatically pull in the changes from upstream to update all of them. Take a look at my base Ghost 0.5.0 repo, you will see a template folder that contains setup.sh and sync.sh bash scripts. Each of the blogs will also have configure-ghost-app.sh script that will configure the specific blog configuration.

Creating Forks

There are a lot of good articles out there for creating multiple forks out of the one repo, but here is the summary of the steps.

  • Create an emtpy repo. e.g. blog.aminjam.com
  • git clone git@bitbucket.org:aminjam/blogs.git blog.aminjam.com
  • cd blog.aminjam.com
  • git remote -v and you should see the origin is pointing to the base template
  • git remote rename origin upstream
  • git remote add origin git@bitbucket.org:aminjam/blog.aminjam.com.git
  • git push -u origin master
  • git remote -v and you should see upstream is pointing to the base repo, and origin is your blog.

Prepare Blog

Now that we have a base repo, let's add the content folder from Ghost. Let's also add the configure-ghost-app.sh to the template folder, and make it executable.

#!/bin/bash
usage() { echo "Usage: $0 [-f <flag>]" 1>&2; exit 1; }

while getopts ":f:" o; do  
  case "${o}" in
    f)
      flag=${OPTARG}
      ;;
    *)
      usage
      ;;
  esac
done  
shift $((OPTIND-1))

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"  
cd $DIR/..

if [ "$flag" == "reset" ]; then  
  sed "s/blog.aminjam.com/my-ghost-blog.com/g" config.js > config.temp.js
  sed 's/mail: { transport: "SMTP", options: { service: "Mailgun", auth: { user: "email@gmail.com", pass: "P@ssw0rd1" } } }/mail: {}/g' config.temp.js > config.temp2.js
else  
  sed "s/my-ghost-blog.com/blog.aminjam.com/g" config.js > config.temp.js
  sed 's/mail: {}/mail: { transport: "SMTP", options: { service: "Mailgun", auth: { user: "email@gmail.com", pass: "P@ssw0rd1" } } }/g' config.temp.js > config.temp2.js
fi  
mv config.temp2.js config.js  
rm config.temp.js  

Once you are done, add your files, commit, and push them to origin. Let's run template/sync.sh to make sure all of the all files are in sync. Let's do a test run of our blog by running:

  • sudo npm install --production
  • PORT=8083 NODE_ENV=production npm start
  • http://localhost:8083 should now have the new blog

Deploying

hipops helps you with code deployment of your apps. It's in the early stages of development, and I will do a full post later about its capabilities. hipops uses go, ansible and docker to automate the process. Assuming you have ansible installed on your machine. Here are the steps to deploy the app...

Setup

(This will be a lot easier once I am done with the project.)

Let's get the latest version of hipops. go get github.com/aminjam/hipops. Next, modify the config.json based on your blog setup. Under the apps section, you can remove and add your blogs, and under playbooks section modify the first play to have the correct apps. Also make sure you have the correct path for ssh keys on the server.

Deploy

Once configured you can run hipops to orchestrate your machine. Assuming you have an empty Ubuntu 14.04 box with Docker installed in AWS.

go run hipops.go -h /Users/jams/ec2.py -c devops/ansible/scenarios/ubuntuOS.ghostBlogs/config.json -p devops/ansible/playbooks/ -k /Users/jams/.ssh/myawskey.pem

This will deploy your blogs and run them using Docker.

Updating the blogs

If you have a new version of Ghost that you would like to deploy. Here is how you can do it in 4 steps manually, or by adding them to a Jenkins job to do it automatically.

  • Login to AWS, and push the ghost.db changes from your production to your repo.
  • Update and push the base template changes.
  • run template/sync.sh
  • re-run the hipops command to update the changes.
  • Done. Now all of your blogs have been updated to the latest changes.
comments powered by Disqus