Skip to content

Instantly share code, notes, and snippets.

@nicholasren
Last active December 25, 2015 04:49
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 nicholasren/6920178 to your computer and use it in GitHub Desktop.
Save nicholasren/6920178 to your computer and use it in GitHub Desktop.
#settings
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'
task :provision do
# add nginx repo
invoke :'nginx:add_repo'
queue "sudo yum install -y git gcc gcc-c++* make openssl-devel mysql-devel curl-devel nginx sendmail-cf ImageMagick"
#install rbenv
queue "source ~/.bash_profile"
queue "#{exists('rbenv')} || git clone https://github.com/sstephenson/rbenv.git ~/.rbenv"
queue "#{exists('rbenv')} || git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build"
queue "#{exists('rbenv')} || echo 'export PATH=\"$HOME/.rbenv/bin:$PATH\"' >> ~/.bash_profile && source ~/.bash_profile"
#install ruby
queue "#{ruby_exists} || RUBY_CONFIGURE_OPTS=--with-openssl-dir=/usr/bin rbenv install #{ruby_version}"
#install bundle
queue "#{ruby_exists} || rbenv local 2.0.0-p247"
queue "#{exists('gem')} || gem install bundle --no-ri --no-rdoc"
#set up deploy to
queue "sudo mkdir -p #{deploy_to}"
queue "sudo chown -R #{user} #{deploy_to}"
end
#helper method
def ruby_exists
"rbenv versions | grep #{ruby_version} >/dev/null 2>&1"
end
def exists cmd
"command -v #{cmd}>/dev/null 2>&1"
end
task :deploy do
deploy do
invoke :'git:clone' #clone code from github
invoke :'deploy:link_shared_paths' #linking shared file with latest file we just cloned
invoke :'bundle:install' #install bundle
invoke :'rails:db_migrate' #run database migration
invoke :'rails:assets_precompile' #compile assets
invoke :'unicorn_and_nginx' #setup nginx and unicorn config
to :launch do
queue '/etc/init.d/unicorn_myapp.sh reload' #reload unicorn after deployment succeed
end
end
end
task :unicorn_and_nginx do
queue! "#{file_exists('/etc/nginx/nginx.conf.save')} || sudo mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.save"
queue! "#{file_exists('/etc/nginx/nginx.conf')} || sudo ln -nfs #{deploy_to}/current/config/nginx.conf /etc/nginx/nginx.conf"
queue! "#{file_exists('/etc/init.d/unicorn_avalon.sh')} || sudo ln -nfs #{deploy_to}/current/scripts/unicorn.sh /etc/init.d/unicorn_myapp.sh"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment