Skip to content

Instantly share code, notes, and snippets.

@0robustus1
Last active December 12, 2015 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0robustus1/915a3348209e0539870c to your computer and use it in GitHub Desktop.
Save 0robustus1/915a3348209e0539870c to your computer and use it in GitHub Desktop.
Mina deploy script allows public-private-key system access to git repository, as explained in this article: https://rightsrestricted.com/posts/rails-deployment-with-mina-utilizing-git-slash-ssh
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
# specifies path to file on the server
def file_to(file)
File.join(deploy_to, deploy_set_loc, file)
end
# builds the ssh command for the git-ssh-bashscript
# depends on git_ssh_options being set as a hash
def build_ssh_cmd
string = "ssh "
git_ssh_options.each_pair do |switch,value|
string << "#{switch} \"#{value}\" "
end
string << "$1 $2"
end
# require 'mina/rbenv' # for rbenv support. (http://rbenv.org)
# require 'mina/rvm' # for rvm support. (http://rvm.io)
# Basic settings:
# domain - The hostname to SSH to.
# deploy_to - Path to deploy into.
# repository - Git repo to clone from. (needed by mina/git)
# branch - Branch name to deploy. (needed by mina/git)
# identity_file - ssh -key for server to SSH to.
# git-ssh deploy settings:
# git_ssh_file - local ssh key file for connection to git repository
# git_ssh_key - string containing the actual ssh key
# deploy_set_loc - location on the server, relative to deploy_to, in which deployment settings are stored
# git_ssh_option - options passed to the ssh command used by git
set :domain, 'my_domain_name.com'
set :deploy_to, 'path/to/rails/app'
set :repository, 'git@git-server:the_repository.git'
set :branch, 'master'
set :identity_file, ENV['HOME']+'/.ssh/id_rsa'
set :git_ssh_file, File.join(File.dirname(__FILE__), 'deployment/ssh_key')
set :git_ssh_key, File.read(git_ssh_file)
set :deploy_set_loc, "/shared/config/deployment/"
set :git_ssh_options, {
"-i" => "#{file_to("ssh_key")}",
"-o" => 'StrictHostKeyChecking no'
}
# Manually create these paths in shared/ (eg: shared/config/database.yml) in your server.
# They will be linked in the 'deploy:link_shared_paths' step.
set :shared_paths, ['config/database.yml', 'log']
# Optional settings:
set :user, 'deployer' # Username in the server to SSH to.
# set :port, '30000' # SSH port number.
# This task is the environment that is loaded for most commands, such as
# `mina deploy` or `mina rake`.
task :environment do
# If you're using rbenv, use this to load the rbenv environment.
# Be sure to commit your .rbenv-version to your repository.
# invoke :'rbenv:load'
# For those using RVM, use this to load an RVM version@gemset.
# invoke :'rvm:use[ruby-1.9.3-p125@default]'
# The script file used by git to establish the ssh connection
queue! %[export GIT_SSH="#{file_to('clone.sh')}"]
# Adding gem binaries to PATH
queue! %[export PATH=/var/lib/gems/1.9.1/bin/:$PATH]
end
task :provider do
# creating the deployment directory
queue! %[mkdir -p "#{File.join(deploy_to, deploy_set_loc)}"]
# writing the key to a file
queue! %[echo "#{git_ssh_key}" > #{file_to('ssh_key')}]
# setting correct permissions of ssh key
queue! %[chmod g-rwx,o-rwx "#{file_to('ssh_key')}"]
# echoing debug info
queue %[echo "-----> Integrated git ssh key."]
queue %[echo "-----> \\$GIT_SSH is set to $GIT_SSH"]
# building the ssh command and writing it to a bash script
queue! %[echo '#{build_ssh_cmd}' > #{file_to('clone.sh')}]
# setting the x-bit correctly
queue! %[chmod u+x,g+x "#{file_to('clone.sh')}"]
# echoing debug info
queue %[echo "-----> built the git-ssh command."]
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, :provider] 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"]
invoke :provider
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 tmp/]
queue 'touch tmp/restart.txt'
end
end
end
# For help in making your deploy script, see the Mina documentation:
#
# - http://nadarei.co/mina
# - http://nadarei.co/mina/tasks
# - http://nadarei.co/mina/settings
# - http://nadarei.co/mina/helpers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment