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…
The code above is called a task. Each task consists of three parts: a description, a name, and the code that will be executed.
- This is just to let us know what the code does. You can have more than one task in your Rakefile.
- This is the name that we’ll use to run our task.
- This is where the magic happens, but let’s break this down a bit more.
putswill display whatever text you have written out next to it (it’s a way to show you where the task is currently at).
pathare 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.
shis 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.
-rtzhis calling the remote shell, basically the terminal on your server.
--deletedoes 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.
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!
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.
I’ve taken the liberty of combining my explanation above into a short screencast. Enjoy!
- 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 firstname.lastname@example.org. 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
_siteand place it on your server.