介紹 Capistrano

deploy manage
deploy rollback

with ci system

manual deplay

進化

automatic deploy

部署要做的事

  • 備份 / 更新資料庫

  • 更新程式碼

  • 更新設定檔

  • 例行性指令

    • 重啟 service

    • 清除 cache

Tool

  • Ansible

  • Chef

  • Capitrano

What is Capistrano?

Capistrano

require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/bundler'
require 'capistrano/rails/migrations'
require 'capistrano/rails/assets'

常見用途

  • automated configuration manage

  • database migration

  • etc...

more detail...

Stages

  • dev

  • QA

  • Staging

  • Production

execute different stage

cap dev deploy
cap sandbox deploy
cap production deploy

Server manage

# server-based syntax
# ======================
server 'dev.horidashi.com', user: 'ubuntu', roles: %w{app db web}, primary: true
# server 'example.com', user: 'deploy', roles: %w{app web}, other_property: :other_value
# server 'db.example.com', user: 'deploy', roles: %w{db}

Role manage

# role-based syntax
# ==================
role :app, %w{ubuntu@dev.horidashi.com}
role :web, %w{ubuntu@dev.horidashi.com}
role :db,  %w{ubuntu@dev.horidashi.com}
set :default_env, { path: "/home/ubuntu/.nvm/versions/node/v4.4.7/bin/:$PATH" }

# Default value for keep_releases is 5
# set :keep_releases, 5
set :keep_releases, 10

namespace :setup do

  desc "Upload config file."
  task :upload_config do
    on roles(:app) do
      execute "mkdir -p #{shared_path}/config"
      upload! StringIO.new(File.read("config/database.yml")), "#{shared_path}/config/database.yml"
    end
  end

end

config/deploy.rb

namespace :deploy do

  task :restart do
    on roles(:web) do
      site_dir = "#{current_path}";
      within site_dir do
        puts capture(:env)
        puts capture(:npm, 'i')
        puts capture(:bower, 'i')
        puts capture(:grunt, 'build')
        puts capture(:pm2, 'restart', 'hub')
      end
    end
  end

  before :deploy, "deploy:env"
  after :deploy, "deploy:restart"
end

Custom task

other config set up

set :ssh_options, {
   keys: %w(/home/ubuntu/.ssh/id_rsa),
   forward_agent: true,
   auth_methods: %w(publickey password)
 }

set :application, 'hub'
set :user, "ubuntu"
set :deploy_to, '/home/ubuntu/service/hub'

set :repo_url, 'git@bitbucket.org:adplayhubteam/hub.git'
set :branch, "master"


after "deploy", "deploy:cleanup"

append :linked_files, 'config/database.yml', 'config/secrets.yml'
append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'public/system'

rollback for what?

  • code

  • database

rollback code

structure

cap production deploy:rollback ROLLBACK_RELEASE=20160614133327

execute commend

rollback database

# db/migrate/20160801115659_adplay.rb

class Adplay < ActiveRecord::Migration[5.0]
  def change
     add_column :adplays, :google_play_url, :string
     add_column :adplays, :itunes_url, :string
  end
end

migration script

cap production db:rollback STEP=1

execute commend

transaction

ci todo

  • vm server

  • webhook

  • Build Scheduler

  • Notification

  • permissions

  • Report

Made with Slides.com