Skip to content

Instantly share code, notes, and snippets.

@guiman
Last active October 26, 2018 13:50
Show Gist options
  • Save guiman/93a5e4735a4c25486963 to your computer and use it in GitHub Desktop.
Save guiman/93a5e4735a4c25486963 to your computer and use it in GitHub Desktop.
Example mina deploy script using docker
require 'mina/git'
require 'mina/nginx'
set :application, 'your_app_name'
set :domain, 'your_server'
set :user, 'ubuntu'
set :deploy_to, '/location/to/deploy'
set :app_path, "#{deploy_to}/#{current_path}"
set :repository, 'your_repo'
set :branch, 'master'
set :forward_agent, true # sorting out credentials for private gems
task :environment do
invoke :'important_variables'
end
task :important_variables => :environment do
queue! %[source /home/ubuntu/.secret_key_base]
end
task :setup => :environment do
queue! %[mkdir -p "#{deploy_to}/#{shared_path}/log"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/log"]
queue! %[mkdir -p "#{deploy_to}/#{shared_path}/config"]
queue! %[touch "#{deploy_to}/#{shared_path}/config/database.yml"]
queue! %[touch "#{deploy_to}/#{shared_path}/config/secrets.yml"]
invoke :'nginx:setup'
invoke :'nginx:link'
end
desc "Deploys the current version to the server."
task :deploy => :environment do
deploy do
invoke :'git:clone'
to :launch do
invoke :'docker:build'
invoke :'docker:stop' # stop the previous container
invoke :'docker:run' # run new container with released code
end
end
end
namespace :docker do
set :image_name, ENV.fetch("IMAGE_NAME", "default_image_name")
set :dockerfile, ENV.fetch("DOCKERFILE", "Dockerfile")
set :container_name, ENV.fetch("CONTAINER_NAME", "service")
# Sharing mysql sockets to communicate Host and Container
set :mysql_socket_volume, "/var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock"
# Mount configuration and log folders
set :configuration_volume, "#{deploy_to}/#{shared_path}/config:/config"
set :log_volume, "#{deploy_to}/#{shared_path}/log:/log"
# Rails specific
set :rails_environment, "production"
desc "Build docker image"
task build: :environment do
queue "cd #{deploy_to}/#{current_path}"
queue "sudo docker build --no-cache=true -f #{dockerfile} -t #{image_name} ."
end
desc "Start docker container"
task run: :environment do
queue "sudo docker run -d -p 3000:8080 -e SECRET_KEY_BASE=$SECRET_KEY_BASE -e SECRET_TOKEN=$SECRET_KEY_BASE -e RAILS_ENV=#{rails_environment} -v #{configuration_volume} -v #{log_volume} -v #{mysql_socket_volume} --name #{container_name} #{image_name}"
end
desc "Debug docker container"
task debug: :environment do
queue "sudo docker run -t -i -p 3000:8080 -e SECRET_KEY_BASE=$SECRET_KEY_BASE -e SECRET_TOKEN=$SECRET_KEY_BASE -e RAILS_ENV=#{rails_environment} -v #{configuration_volume} -v #{log_volume} -v #{mysql_socket_volume} #{image_name} /bin/bash"
end
desc "Stop docker container"
task stop: :environment do
queue "if [ ! -z \"$(sudo docker ps | grep '#{container_name}')\" ]; then sudo docker stop #{container_name}; sudo docker rm -f #{container_name}; fi"
end
desc "Remove all stop container"
task clean_containers: :environment do
queue "sudo docker rm $(sudo docker ps -a -q)"
end
end
upstream demo.localhost {
server 127.0.0.1:3000;
}
server {
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
server_name demo.localhost;
location / {
proxy_pass http://demo.localhost;
include /etc/nginx/proxy_params;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment