I finally took the time to ditch my old WordPress blog and start fresh with a hip, new blogging engine powered by Markdown. Why make it easy on myself to simply install a new blog and put it on some server, when I could decide to learn continuous integration/deployment in the process.

I made this blog using everything I’m about to describe, so feel free to jump straight into the source.

Tech Used

NearlyFreeSpeech

NearlyFreeSpeech is a great, low cost web host that I tend to use for any small scale personal projects. You can host a static site, like a Jekyll blog, for extremely cheap.

Jekyll

Jekyll is a static website generator powered by Ruby. Customizable and easy to set up, supports Markdown and get’s your blog into version control.

$ gem install jekyll
$ jekyll new myblog
$ cd myblog
$ ~/myblog $ jekyll serve
# Blog now served at localhost:4000

Jekyll has some great docs that can help you get your blog up and running.

Travis CI and HTML-Proofer

Travis CI is a continuous integration platform that makes it easy to build, test and deploy whenever changes are pushed to GitHub. The cibuild script will use html-proofer to check the _site folder for any errors, such as dead links.

Add html-proofer to the Gemfile

gem "html-proofer"

Create a .travis.yml file

language: ruby
rvm: 2.2.1
install: gem install jekyll html-proofer
script: jekyll build && ./script/cibuild
branches:
  #any branches you'd like Travis CI to test.
  only:
  - pages
  - master
  - "/pages-(.*)/"
env:
  global:
  - NOKOGIRI_USE_SYSTEM_LIBRARIES=true

## before installing app
## Turn off strict host checking
## unzip encrypted ssh private key and _config.yml
before_install:
    - echo -e "Host ssh.phx.nearlyfreespeech.net\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
    - openssl aes-256-cbc -K $encrypted_dc941821cb90_key -iv $encrypted_dc941821cb90_iv
      -in secrets.tar.enc -out secrets.tar -d
    - tar xvf secrets.tar

after_success:
    - chmod 600 .travis/ssh_key
    - eval "$(ssh-agent -s)"
    - ssh-add .travis/ssh_key
    - rake deploy

Create a Ruby script at script/cibuild

#!/usr/bin/env ruby

require 'html/proofer'
HTML::Proofer.new("./_site").run

I’m using rsync to deploy my _site folder to my NearlyFreeSpeech /home/public/ folder.

Sample Rakefile file

task :deploy do
  user = 'site_username'
  server = 'ssh.phx.nearlyfreespeech.net'
  sh "rsync -crz --delete _site/ #{user}@#{server}:/home/public"
end

To keep ssh keys and Rakefile settings out of the repo, zip them together and then encrypt them so TravisCI can decrypt them.

First login to travis

$ travis login

Zip the files and encrypt them

# saves files .travis/ssh_key and Rakefile to secrets.tar
$ tar cvf secrets.tar Rakefile .travis/ssh_key
# encrypts and automatically adds it to .travis.yml
$ travis encrypt-file secrets.tar --add

Make sure to add any encrypted files to .gitignore!

Recap!

  • Create a blog with the jekyll command line constructer.
  • Add blog content and add it to a git repo.
  • Signup for Travis CI and enable the blog repo on GitHub.
  • Add a .travis.yml file
  • Create a script for html-proofer
  • Add Rakefile with NearlyFreeSpeech username
  • Edit _config.yml
  • Create an ssh key and add the public key to the NearlyFreeSpeech blog.
  • Encrypt .travis/private_key and Rakefile
  • Add and commit all the changes.
  • Push the branch to GitHub and let Travis CI take care of testing and deploying.

Now you can brag about build passing like all the cook kids.

Build Status

But kidding aside, if you’re at all new to continuous integration/deployment this is a great way to get your feet wet.