From ca7e130d71022c6ee766d88c2f0ed7bd5cc1897b Mon Sep 17 00:00:00 2001 From: shinchika34 <58651375+shinchika34@users.noreply.github.com> Date: Mon, 2 Mar 2020 22:12:46 +0900 Subject: [PATCH] Auto-deployment --- Capfile | 44 ++++++++++++++++++++++++++ config/credentials.yml.enc | 2 +- config/deploy.rb | 34 +++++++++++++++++++++ config/deploy/production.rb | 1 + config/deploy/staging.rb | 61 +++++++++++++++++++++++++++++++++++++ config/unicorn.rb | 26 ++++++---------- 6 files changed, 150 insertions(+), 18 deletions(-) create mode 100644 Capfile create mode 100644 config/deploy.rb create mode 100644 config/deploy/production.rb create mode 100644 config/deploy/staging.rb diff --git a/Capfile b/Capfile new file mode 100644 index 0000000..efe36e4 --- /dev/null +++ b/Capfile @@ -0,0 +1,44 @@ +# Load DSL and set up stages +require "capistrano/setup" + +# Include default deployment tasks +require "capistrano/deploy" + +# Load the SCM plugin appropriate to your project: +# +# require "capistrano/scm/hg" +# install_plugin Capistrano::SCM::Hg +# or +# require "capistrano/scm/svn" +# install_plugin Capistrano::SCM::Svn +# or +require "capistrano/scm/git" +install_plugin Capistrano::SCM::Git + +# Include tasks from other gems included in your Gemfile +# +# For documentation on these, see for example: +# +# https://github.com/capistrano/rvm +# https://github.com/capistrano/rbenv +# https://github.com/capistrano/chruby +# https://github.com/capistrano/bundler +# https://github.com/capistrano/rails +# https://github.com/capistrano/passenger +# +# require "capistrano/rvm" +# require "capistrano/rbenv" +# require "capistrano/chruby" +# require "capistrano/bundler" +# require "capistrano/rails/assets" +# require "capistrano/rails/migrations" +# require "capistrano/passenger" + +require 'capistrano/rbenv' +require 'capistrano/bundler' +require 'capistrano/rails/assets' +require 'capistrano/rails/migrations' +require 'capistrano3/unicorn' + +# Load custom tasks from `lib/capistrano/tasks` if you have any defined +Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r } diff --git a/config/credentials.yml.enc b/config/credentials.yml.enc index 490af11..64b7974 100644 --- a/config/credentials.yml.enc +++ b/config/credentials.yml.enc @@ -1 +1 @@ -Xq5kVC/AkvsJ0RMff9QjHSbdenECqbOkb4arQXxEkx6ESPwladLidt84Ju3jxpi9RAmovSwzuPdLFA0Kbqq7LeIt7NBz09Z9Yw0ajOtLJl0kJbfBQ8HXVVQ8gcLnUAAkEkVZ5UurdVRf7qUTSEH0S1FjwtzAMCFiBogfVXzYRsNd8T5pvTHZOvLNR+fWm8EynonouuWfnb15Sj8iLeTAYKp6qjl25MWfoE9Uj3yv5WE0WyCGK+4rtlG/mN4/aCpQ62KSqhK4bHVGAtrRBNAK77mhpCeDC1CGa08PHiLTDQlraGxf6akc0DxHT2A3qBVnMftGCGiI1RMopoU8UUEBt3xu5jn8eT5CinKDbFRTe95fod//ZQMIO+GmAmb5V4iFrQdEm+wvJtOJCYOclZVsymA7oXUy59hLdXzX--x/EXYxVgrg/QhZF+--XHVaw08P3on7axLhaKDZPw== \ No newline at end of file +N6/q5IsA60SKTXwvQi7w0pzXSWVpgd1xMTOTRvJngqTGUtwNsv78iPmZ+DywtT/3avgH0v1s5EGgW3lriofEty+SDeUm98RKlg65JQGdbFDSKeNQIcqpaOSXf7ETE1mCb2Ni7RVuAIquUmHRoSKFfRMhHYLWyZ7T6O/1NGL1W+3JUC/1bMXjXmDwdQCQFuDE3IFDm0fUCj+p7yhZ6/UAMY+JDZ7E/2IXaZa2/sYmi9Ec1Gd82IRzhdQFmTz0+xe9dTLJQ0VyncridKDAbKoj6zJxqw8FcTltFAGa1aX3S2A/LxP9+d6ZJgrqgRYL1qyiWz7XMMPVFcEvdUNOB1D/bN2BYyVbhzIezx8xh78OwO1j3pi356u3f6cQJzlxPDWTIa+MhkgsHEClmfxkpG1b8U4=--yeS1D2EZpQq2McAG--Jz0OqdZPjjvPpwOR9ugZ2Q== \ No newline at end of file diff --git a/config/deploy.rb b/config/deploy.rb new file mode 100644 index 0000000..64e2126 --- /dev/null +++ b/config/deploy.rb @@ -0,0 +1,34 @@ +# config valid only for current version of Capistrano +# capistranoのバージョンを記載。固定のバージョンを利用し続け、バージョン変更によるトラブルを防止する +lock '3.12.0' + +# Capistranoのログの表示に利用する +set :application, 'freemarket_sample_65a' + +# どのリポジトリからアプリをpullするかを指定する +set :repo_url, 'git@github.com:ToruHirashima/freemarket_sample_65a.git' + +# バージョンが変わっても共通で参照するディレクトリを指定 +set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/uploads') + +set :rbenv_type, :user +set :rbenv_ruby, '2.5.1' #カリキュラム通りに進めた場合、2.5.1か2.3.1です + +# どの公開鍵を利用してデプロイするか +set :ssh_options, auth_methods: ['publickey'], + keys: ['~/.ssh/freemarket_sample_65a.pem'] + +# プロセス番号を記載したファイルの場所 +set :unicorn_pid, -> { "#{shared_path}/tmp/pids/unicorn.pid" } + +# Unicornの設定ファイルの場所 +set :unicorn_config_path, -> { "#{current_path}/config/unicorn.rb" } +set :keep_releases, 5 + +# デプロイ処理が終わった後、Unicornを再起動するための記述 +after 'deploy:publishing', 'deploy:restart' +namespace :deploy do + task :restart do + invoke 'unicorn:restart' + end +end \ No newline at end of file diff --git a/config/deploy/production.rb b/config/deploy/production.rb new file mode 100644 index 0000000..f4c59a1 --- /dev/null +++ b/config/deploy/production.rb @@ -0,0 +1 @@ +server '52.193.148.9', user: 'ec2-user', roles: %w{app db web} \ No newline at end of file diff --git a/config/deploy/staging.rb b/config/deploy/staging.rb new file mode 100644 index 0000000..4bb7a71 --- /dev/null +++ b/config/deploy/staging.rb @@ -0,0 +1,61 @@ +# server-based syntax +# ====================== +# Defines a single server with a list of roles and multiple properties. +# You can define all roles on a single server, or split them: + +# server "example.com", user: "deploy", roles: %w{app db web}, my_property: :my_value +# server "example.com", user: "deploy", roles: %w{app web}, other_property: :other_value +# server "db.example.com", user: "deploy", roles: %w{db} + + + +# role-based syntax +# ================== + +# Defines a role with one or multiple servers. The primary server in each +# group is considered to be the first unless any hosts have the primary +# property set. Specify the username and a domain or IP for the server. +# Don't use `:all`, it's a meta role. + +# role :app, %w{deploy@example.com}, my_property: :my_value +# role :web, %w{user1@primary.com user2@additional.com}, other_property: :other_value +# role :db, %w{deploy@example.com} + + + +# Configuration +# ============= +# You can set any configuration variable like in config/deploy.rb +# These variables are then only loaded and set in this stage. +# For available Capistrano configuration variables see the documentation page. +# http://capistranorb.com/documentation/getting-started/configuration/ +# Feel free to add new variables to customise your setup. + + + +# Custom SSH Options +# ================== +# You may pass any option but keep in mind that net/ssh understands a +# limited set of options, consult the Net::SSH documentation. +# http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start +# +# Global options +# -------------- +# set :ssh_options, { +# keys: %w(/home/rlisowski/.ssh/id_rsa), +# forward_agent: false, +# auth_methods: %w(password) +# } +# +# The server-based syntax can be used to override options: +# ------------------------------------ +# server "example.com", +# user: "user_name", +# roles: %w{web app}, +# ssh_options: { +# user: "user_name", # overrides user setting above +# keys: %w(/home/user_name/.ssh/id_rsa), +# forward_agent: false, +# auth_methods: %w(publickey password) +# # password: "please use keys" +# } diff --git a/config/unicorn.rb b/config/unicorn.rb index 5035d89..c7781a2 100644 --- a/config/unicorn.rb +++ b/config/unicorn.rb @@ -1,22 +1,14 @@ -app_path = File.expand_path('../../', __FILE__) +app_path = File.expand_path('../../../', __FILE__) -#アプリケーションサーバの性能を決定する worker_processes 1 - -#アプリケーションの設置されているディレクトリを指定 -working_directory app_path - -#Unicornの起動に必要なファイルの設置場所を指定 -pid "#{app_path}/tmp/pids/unicorn.pid" - -#ポート番号を指定 -listen "#{app_path}/tmp/sockets/unicorn.sock" - -#エラーのログを記録するファイルを指定 -stderr_path "#{app_path}/log/unicorn.stderr.log" - -#通常のログを記録するファイルを指定 -stdout_path "#{app_path}/log/unicorn.stdout.log" +# currentを指定 +working_directory "#{app_path}/current" + +# それぞれ、sharedの中を参照するよう変更 +listen "#{app_path}/shared/tmp/sockets/unicorn.sock" +pid "#{app_path}/shared/tmp/pids/unicorn.pid" +stderr_path "#{app_path}/shared/log/unicorn.stderr.log" +stdout_path "#{app_path}/shared/log/unicorn.stdout.log" #Railsアプリケーションの応答を待つ上限時間を設定 timeout 60