Deploy rails application with Mina, Nginx and Puma
What is Mina?
Mina is a deployment tool, similar to Capistrano. The major advantage of Mina is its speed. Mina works exceptionally fast because it’s a deploy Bash script generator. It generates an entire procedure as a Bash script, and runs it remotely in the server. It also supports smart command execution, so if you do not change assets (CSS, JS, images), it will not execute assets precompile. The same goes for migrations.
Let’s take a look at setting up Mina with Puma. First, you’ll need to add Mina and mina-puma in Gemfile, which is located in the development section. Mina recently changed its maintainer; a new repository is here, and it is best to install it from Git.
Add Puma gem to the global section if you are using a Rails version below 5.
Then, install gems and execute the initial Mina command for generating a config/deploy.rb file.
bundle
mina init
You can find an example of a Mina 1.0.0 default config here. Click here for the full config in that configuration.
Detailed explanations of the Mina deploy file
#Set the domain or ip address of the remote server.
set :domain, 'yourdomain.ru'
#Set the folder of the remote server where Mina will deploy your application.
set :deploy_to, 'path/to/directory'
#Set a link to the repository. Example: git@bitbucket.pixelpoint/myapp.git
set :repository, 'git@…'
#Set the name of a branch you plan to deploy as default master.
set :branch, 'master'
#Fill in the names of the files and directories that will be symlinks to the shared directory.
#All folders will be created automatically on Mina setup.
#Don’t forget to add a path to the uploads folder if you are using Dragonfly or CarrierWave.
#Otherwise, you will lose your uploads on each deploy.
set :shared_dirs, fetch(:shared_dirs, []).push('log', 'tmp/pids', 'tmp/sockets', 'public/uploads')
set :shared_files, fetch(:shared_files, []).push('config/database.yml', 'config/secrets.yml', 'config/puma.rb')
#Username of ssh user for access to the remote server.
set :user, 'username'
#This is not a required field. You can use it to set an application name for easy recognition.
set :application_name, 'MyApplication'
#Set ruby version. If you have RVM installed globally, you’ll also need to set an RVM path,
#like: set :rvm_use_path, '/usr/local/rvm/scripts/rvm'.
#You can find the RVM location with the rvm info command.
task :environment do
invoke :'rvm:use', 'ruby-2.4.0@default'
end
By default, Mina will create all folders mentioned in shared_dirs and shared_files. In setup, however, you can add section auto-creation of empty files and fill them later.
Generate new ssh keys on your server by using that command ssh-keygen. Then, export it to the deploys keys of your github/bitbucket project.
Mission Completed
That’s it. Now you’re ready to deploy your application. Run mina deploy if you have already executed mina setup and wait.
Troubleshooting
If you have problems on git clone, check that you have exported ssh pub key to your repository, and try to clone the repository directly from the server.
Use Mina puma:start on first launch instead of phased_restart.