cult3

Understanding and Using Ruby Rake

Aug 05, 2015

Table of contents:

  1. Installing Rake
  2. Creating a Rake file
  3. Tasks can have prerequisites
  4. Namespacing
  5. Describing tasks
  6. Rake in the wild

Something that you will see a lot in Ruby projects is Rake. Rake is a build tool for automating tasks.

This means you can define a set of tasks that should be carried out as part of a build process, or perhaps a set of tasks that should be executed every time you run a specific command.

Rake allows you to create tasks that depend on one another or have prerequisites. This makes creating automated processes very easy.

In today’s tutorial we will be looking at Rake, how it works and what you will find yourself using it for.

Installing Rake

Like all good things Ruby, Rake is available as a Gem that you can install on your computer:

gem install rake

If you’ve already got your Ruby environment set up, then there’s really nothing else much to say about installation!

Creating a Rake file

The first thing we need to do is to create a Rakefile (without an extension). This file is where you list the tasks that you want to perform. Rakefiles are defined using Ruby, so if you’re familiar with the Ruby syntax, there should be nothing too crazy about this:

task default: %w[hello]

task :hello do
  puts 'Hello world!'
end

In this Rakefile we have two distinct things.

Firstly we have a task called default. All Rakefiles will have a default task. In this case, we’re saying that when the default task is run, we should execute the hello task.

Secondly we have the hello task. When this task is run it will simply print Hello world! to the screen.

If you save the Rakefile and run the following command from terminal, you should see the correct output:

$ rake
Hello world!

Tasks can have prerequisites

So as you can see, it is very easy to define an automated task using Rake because it is just Ruby code.

However, the real power of Rake is when you start using tasks with prerequisites. This makes it possible to run a list of tasks in a given order.

For example, imagine if we wanted to automate deploying our website to our web server. We might have the following tasks:

task :ssh_into_server do
  puts 'ssh into server'
end

task :move_to_directory do
  puts 'cd to the correct directory'
end

task :pull_code do
  puts 'pulling code from Github'
end

task :get_dependencies do
  puts 'getting the dependencies'
end

task :migrate_the_database do
  puts 'migrating the database'
end

task :set_file_permissions do
  puts 'setting the file permissions'
end

task :symlink_the_new_version do
  puts 'symlinking the new version'
end

We can state that these tasks should be performed in the correct order by creating a new task that has prerequisites.

task deploy: %i[
       ssh_into_server
       move_to_directory
       pull_code
       get_dependencies
       migrate_the_database
       set_file_permissions
       symlink_the_new_version
     ] do
  puts "deploying the website! "
end

Now with one single command we can completely automate the deployment of the website to the production server:

$ rake deploy

Namespacing

A couple of weeks ago we looked at namespacing our code using Modules.

Namespacing is also an important topic in Rake as bigger projects could potentially have rake tasks that have conflicting names.

To solve this problem we can use namespaces. For example we might namespace our deployment process from above like this:

namespace :ship_it do
  task :ssh_into_server do
    puts 'ssh into server'
  end

  # The rest of the tasks
end

Now to run the deploy task you would include the namespace as part of the command:

$ rake ship_it:deploy

Describing tasks

When you are first getting to grips with an existing project, it can be useful to check out the existing rake tasks to see what has been defined.

To list out the Rake tasks you can run the following command from terminal:

$ rake -tasks

However, if you do that now you won’t see any output. To provide additional help to the user we can write a short description about the task:

desc 'SSH into the server.'
task :ssh_into_server do
  puts 'ssh into server'
end

Now when you run the command again, you should see the following output:

$ rake —tasks
rake ship_it:ssh_into_server # SSH into the server

Rake in the wild

You will find Rake in just about every Ruby project out in the wild. Rails makes good use of Rake and whenever you create a new Ruby gem, Rake will be included (more on that next week!).

In today’s tutorial we’ve looked at a hypothetical way of deploying a website to a server using Rake to automate a series of steps.

However, there is already a Rake-like tool out there called Capistrano that does this exact job, but much better.

Capistrano basically follows the same methodology of Rake by allowing you to define the tasks that should be invoked when deploying your website. However, Capistrano has an ecosystem of add ons and plugins that you can make use of, rather than rolling your own set of tasks.

For a long time, Capistrano has been a very popular way of deploying Ruby projects. However it can also be effectively used for any other type of project.

Philip Brown

@philipbrown

© Yellow Flag Ltd 2024.