Rails application deployment automation with mina
In this post, I will introduce a really fast deployment tool -
Mina, and show you how to deploy a rails application which runs on
nginx, also I’ll show you how to organize your mina deployment tasks.
All code in this post can be find here.
“Mina is a readlly fast deployer and server automation tool”, this how the team who built it describes it. The concept behind
Mina is to connect to a remote server and executes a set of shell command that you define in a local deployment file(
config/deploy.rb). One of its outstanding feature is it is really fast because all bash instructions will be performed in one SSH connection.
To use mina automating deployment, you need to get the following things ready.
- a remote sever
- create a user for deployment (e.g. deployer) and add this user into sudoer list.
- generate a ssh key pair, add the generated public key into GitHub.
- create a deployment target fold on the remove server(e.g. ‘/var/www/example.com’)
once you got these things done, run
mina init in your project directory, this will generate a deployment file -
config/deploy.rb, then set the server address, deployment user, deployment target and other settings in deployment file, like the following:
set :user, 'deployer' set :domain, ENV['on'] == 'prod' ? '<prod ip>' : '<qa ip>' set :deploy_to, '/var/www/example.com' set :repository, 'firstname.lastname@example.org:your_company/sample.git' set :branch, 'master'
mina setup, this will create deployment folders, which looks like this:
/var/www/example.com/ # The deploy_to path |- releases/ # Holds releases, one subdir per release | |- 1/ | |- 2/ | |- 3/ | '- ... |- shared/ # Holds files shared between releases | |- logs/ # Log files are usually stored here | `- ... '- current/ # A symlink to the current release in releases/
It is very common to setup a new server and deploy application on it, it will be good if we can automating this process, here comes the provision task:
to be able run this taks multi times, I create some helper method detecting whether an executable exists or not. e.g.
exits('gem') these helper method will return if the executable exits, otherwise, it will run next command to install the executable.
With this task, you can get a server ready for deployment in seveural minutes.
Once the server is provisioned, you can deploy your application with
mina deploy, here is a typical deploy task:
Unicorn and nginx
to run our application on unicorn and nginx, we need to create our own unicorn and nginx configuration file and start nginx and unicorn with our configuration. here is the task:
With these tasks:
mina deploy, mina helps you deploying easily with less mistake, have fun with mina!