Skip to content

Instantly share code, notes, and snippets.

@stevenyap
Created February 21, 2014 08:45
Show Gist options
  • Save stevenyap/9130807 to your computer and use it in GitHub Desktop.
Save stevenyap/9130807 to your computer and use it in GitHub Desktop.
Mina - Faster deployment than Capistrano!!!

Introduction

Mina Deploy.rb

require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rvm'

# Basic settings:
set :domain, 'api.openseas.com.sg'
set :app, 'stapi'
set :repository, 'ssh://[email protected]/~/git/stapi.git'
set :rvm_path, '/usr/local/rvm/scripts/rvm'
set :user, 'stapi'

# You need to manually upload your database.yml file as Mina will not deploy for you - 12factors
set :shared_paths, ['config/database.yml', 'log']

# This task is the environment that is loaded for most commands, such as
# `mina deploy` or `mina rake`.
task :environment do
  # Ensure that a server has been set
  stage = ENV['to']
  case stage
    when 'staging'
      set :branch, 'staging'
    when 'production'
      set :branch, 'master'
    else
    print_error "Please specify a stage. eg. mina deploy to=production"
    exit
  end

  set :rails_env, stage
  set :deploy_to, "/var/www/#{app}/#{stage}"

  invoke :"rvm:use[ruby-2.0.0-p353@#{app}]"
end

# Put any custom mkdir's in here for when `mina setup` is ran.
# For Rails apps, we'll make some of the shared paths that are shared between
# all releases.
task :setup => :environment do
  queue! %[mkdir -p "#{deploy_to}/shared/log"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/log"]

  queue! %[mkdir -p "#{deploy_to}/shared/config"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/config"]

  queue! %[touch "#{deploy_to}/shared/config/database.yml"]
  queue  %[echo "-----> Be sure to edit 'shared/config/database.yml'."]
end

desc "Deploys the current version to the server."
task :deploy => :environment do
  deploy do
    # Put things that will set up an empty directory into a fully set-up
    # instance of your project.
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rails:db_migrate'
    invoke :'rails:assets_precompile'

    to :launch do
      queue "mkdir -p #{deploy_to}/#{current_path}/tmp"
      queue "touch #{deploy_to}/#{current_path}/tmp/restart.txt"
    end
  end
end

desc "Seed data to the database"
task :seed => :environment do
    queue "cd #{deploy_to}/#{current_path}/"
    queue "bundle exec rake db:seed RAILS_ENV=#{rails_env}"
    queue  %[echo "-----> Rake Seeding Completed."]
end

Extra

  • If the git repository is in the same server, you can change to: set :repository, '/full/path/to/git/appname.git'
  • To force unlock, mina deploy:force_unlock
  • Mina deploy by creating and symlinking the entire release folder with a new folder from git:
    • If you are storing files as data (such as Paperclip to store uploaded attachments) or need to maintain folders that cannot be overwritten by git,
    • put the folder in shared/{path_to_folder}
    • add the file path in set :shared_paths
    • Eg. set :shared_paths, ['config/database.yml', 'log', '{path_to_folder}']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment