Deploying a Jekyll site with a Rakefile

So, you have an awesome website built with Jekyll, but you need an easy way to publish it. For those unaware, Jekyll is a static site generator written in Ruby. With that in mind let’s talk about a little utility called Rake. It’s a Make-like program implemented in Ruby. This means you can create a Rakefile that contains a set of build rules. Simply put, it’s a file that you write some tasks in. The main task we want to achieve is publishing our site from a local machine to a server.

First thing you need to do is create a Rakefile in the root of your Jekyll project. It should be named “RAKEFILE”, no need to include an extension on the file, but make sure you have the name in all-caps. Now we’ll write the task that will be placed inside that file, but first I want to show you the final code…

desc 'Publishing the website via rsync'
task :deploy do
  puts 'Publishing your website, silence is golden...'
  user = 'example.com'
  server = 's00000.gridserver.com'
  path = 'domains/example.com/html'
  sh "rsync -rtzh --delete _site/ #{user}@#{server}:#{path}"
  puts 'Your website is now published!’
end
Fig. 1.0 | The entire Rakefile’s final code.

The code above is called a task. Each task consists of three parts: a description, a name, and the code that will be executed.

Definition
This is just to let us know what the code does. You can have more than one task in your Rakefile.

desc 'Publishing the website via rsync'
Fig. 2.0 | Rake task description
Name
This is the name that we’ll use to run our task.

task :deploy
Fig. 3.0 | Deployment task line within our Rakefile
Code
This is where the magic happens, but let’s break this down a bit more. puts will display whatever text you have written out next to it (it’s a way to show you where the task is currently at). user, server, & path are variables. The variables in this example are what I use to deploy my site (yours will differ). As an extra note, the path is the path on the server to your website. sh is a built in Rake function that runs a system command in the shell. The first part of the shell command is rsync, which is a utility that synchronizes files and directories from one location to another. -rtzh is calling the remote shell, basically the terminal on your server. --delete does exactly what it sounds like. The last two pieces are where the files are being copied from and where they’re being sent to. In a nutshell we’re connecting to the remote server via a shell command to sync the files.

do
  puts 'Publishing your website, silence is golden...'
  user = example.com'
  server = 's00000.gridserver.com'
  path = 'domains/example.com/html'
  sh "rsync -rtzh --delete _site/ #{user}@#{server}:#{path}"
  puts 'Your website is now published'
end
Fig. 4.0 | The main code of our Rakefile

Running the Rakefile

Let’s put the Rakefile to work and run our task. Open up terminal and navigate to the root directory of your project. Now for the easy part, type it out!

rake deploy
Fig. 5.0 | Deploying with the rake command via Terminal

Now press enter. Keep in mind that “deploy” is the name of our task. The terminal will now ask you for a password to the server (you should enter that info now). Congrats! you’ve just published your site via a Rakefile. It’s that simple!

This is how I deploy my personal site Kennedy’s Garage. You can checkout my code that I currently use on Github. Just as an fyi, the language I use is… colorful… and kinda nsfw. What can I say, I love pushing new code. If you have any questions feel free to hit me up on twitter @kennedysgarage.

Screencast

I’ve taken the liberty of combining my explanation above into a short screencast. Enjoy!

Fig. 6.0 | A short screencast of said process

faq

  • Q: It didn’t work
  • A: First make sure you you can connect to your server via SSH. I’m using Media Temple and I had to read “Connecting via SSH to your server” to get started. You can also check out an older post on this blog about speeding up your ssh connection workflow. In order for me to connect to the server via ssh I have to type ssh kg41.com@s107775.gridserver.com. You can see that line of code is very similar to what we have in our Rakefile. If it’s still not working, shoot me a tweet @kennedysgarage and we will figure it out together.
  • Q: It’s working, but the files are not updating.
  • A: Make sure your Jekyll server was ran after you’ve updated your code. The task inside our RAKEFILE uses the latest code from _site and place it on your server.

Kennedy

I'm an award-winning interactive designer with a keen eye for details while keeping a clear vision of the big picture. I'm a founding partner of Theron Studios, Status Chart, Grade Spotter, and Kenvak. I've also worked with powerful brands such as Google, Wine Spectator, Porsche, Coca-Cola, Spotify, and MLB. My main focus is designing mobile, tablet, and wearable apps. Follow me on Twitter.
  1. I get the following error when I run the rake task:

    unexpected tIDENTIFIER, expecting end-of-input
      server = 's777777.gridserver.com'
  2. It seems that a Prime Symbol was converted to an Single Quote Mark in the post. Checkout the end of the second to last line in Fig 4.0. Another reason to always retype out the work. Please let me know if this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *

show formatting examples
<pre class="language-[markup | sass | css | php | javascript | ruby | clike | bash]"><code>
…code example goes here…
</code></pre>

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comment Preview

  1. John Doe shouted this comment preview:
    2014/08/12