Rails application deployment automation with mina
TLDR:
In this post, I will introduce a really fast deployment tool - Mina
, and show you how to deploy a rails application which runs on unicorn
with nginx
, also I’ll show you how to organize your mina deployment tasks.
Note:
All code in this post can be find here.
About mina
“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.
Init
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, 'git@github.com:your_company/sample.git'
set :branch, 'master'
Setup
Then run 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/
Provision
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. ruby_exists
, 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.
Deploy
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:
Conclusion
With these tasks: mina init
, mina provision
, mina deploy
, mina helps you deploying easily with less mistake, have fun with mina!