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
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 isrsync
, 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
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!
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.
I get the following error when I run the rake task:
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.
Great tutorial! I have never built a Rakefile before, but this guide seemed very easy to follow. Looking forward to trying it out on my site as an alternative to a bash script or Grunt/Gulp.
One question: why do you specify the -h flag in the rsync command? Does that not just print the help message?