Deploy Ruby on Rails applications using Rake
So you are a Rails developer (or just starting to test Rails), and you would like to find an easy way to deploy a new version of your web application to your production machine, just uploading the files you have changed, and not the entire project.
Rake is Ruby's version of Make, and uses Rakefiles (clever, huh?). You can use Ruby to make your own Makefiles. We can then use Rake to deploy our application.
You will need the rsync client (you don't need the server) installed in your destination server. Otherwise, the process will fail.
Create a new file, named deploy.rake in lib/tasks/, containing the following code:
This will sync both app/ and public/ directories (the application itself, images and stylesheets), but you can add as much directories as you need. The second line changes the permissions to the user that runs the web server, and the third, as you may see, reloads Apache to force the server to refresh the controllers.
And what if you have several servers where to deploy? We can use some Ruby style loops:
Now, in your main application directory, just run rake deploy and the remote host will be synced with your local application.
Still, if you don't have already enabled key authentication for your ssh account, you will be asked for a password each time you deploy your application.
If you want to do this process without having to enter your password, you can generate your own authentication key:
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/daniel/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/daniel/.ssh/id_rsa.
Your public key has been saved in /home/daniel/.ssh/id_rsa.pub.
The key fingerprint is:
e3:c6:4d:88:c0:75:7b:2a:65:52:91:19:b4:99:e1:8f daniel@client
If you don't specify a password, you won't need to enter it to unlock the private key. It is safe to leave it blank, but don't let anyone get to your id_rsa private key, or they will be able to log in to your servers.
The public key is contained in .ssh/id_rsa.pub. This is the key you have to upload to your servers.
$ scp .ssh/id_rsa.pub root@server:
root@server's password:
id_rsa.pub 100% 233 0.2KB/s 00:00
Log in now to your server as the user you want to have authorized, and copy the public key to the authorized_keys file:
# cat id_rsa.pub >> .ssh/authorized_keys
Rake is Ruby's version of Make, and uses Rakefiles (clever, huh?). You can use Ruby to make your own Makefiles. We can then use Rake to deploy our application.
You will need the rsync client (you don't need the server) installed in your destination server. Otherwise, the process will fail.
Create a new file, named deploy.rake in lib/tasks/, containing the following code:
task :deploy => :environment do
puts "Deploying to server"
system("rsync -rltvz -e ssh app public root@server:/var/www/webapp/")
system("ssh root@server chown -R www-data:www-data /var/www/webapp/")
system("ssh root@server /etc/init.d/apache2 reload")
end
puts "Deploying to server"
system("rsync -rltvz -e ssh app public root@server:/var/www/webapp/")
system("ssh root@server chown -R www-data:www-data /var/www/webapp/")
system("ssh root@server /etc/init.d/apache2 reload")
end
This will sync both app/ and public/ directories (the application itself, images and stylesheets), but you can add as much directories as you need. The second line changes the permissions to the user that runs the web server, and the third, as you may see, reloads Apache to force the server to refresh the controllers.
And what if you have several servers where to deploy? We can use some Ruby style loops:
task :deploy => :environment do
servers = %w{server1.domain server2.domain server3.domain}
servers.each do |server|
puts "Deploying app to #{server}"
system("rsync -rltvz -e ssh app public root@#{server}:/var/www/webapp/")
system("ssh root@#{server} chown -R www-data:www-data /var/www/webapp/")
system("ssh root@server /etc/init.d/apache2 reload")
end
end
servers = %w{server1.domain server2.domain server3.domain}
servers.each do |server|
puts "Deploying app to #{server}"
system("rsync -rltvz -e ssh app public root@#{server}:/var/www/webapp/")
system("ssh root@#{server} chown -R www-data:www-data /var/www/webapp/")
system("ssh root@server /etc/init.d/apache2 reload")
end
end
Now, in your main application directory, just run rake deploy and the remote host will be synced with your local application.
Still, if you don't have already enabled key authentication for your ssh account, you will be asked for a password each time you deploy your application.
If you want to do this process without having to enter your password, you can generate your own authentication key:
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/daniel/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/daniel/.ssh/id_rsa.
Your public key has been saved in /home/daniel/.ssh/id_rsa.pub.
The key fingerprint is:
e3:c6:4d:88:c0:75:7b:2a:65:52:91:19:b4:99:e1:8f daniel@client
If you don't specify a password, you won't need to enter it to unlock the private key. It is safe to leave it blank, but don't let anyone get to your id_rsa private key, or they will be able to log in to your servers.
The public key is contained in .ssh/id_rsa.pub. This is the key you have to upload to your servers.
$ scp .ssh/id_rsa.pub root@server:
root@server's password:
id_rsa.pub 100% 233 0.2KB/s 00:00
Log in now to your server as the user you want to have authorized, and copy the public key to the authorized_keys file:
# cat id_rsa.pub >> .ssh/authorized_keys











First of all, the 


