Skip to content

Instantly share code, notes, and snippets.

@zxzsaga
Last active April 27, 2021 07:48
Show Gist options
  • Select an option

  • Save zxzsaga/7bd05b5f22f6f367efdd to your computer and use it in GitHub Desktop.

Select an option

Save zxzsaga/7bd05b5f22f6f367efdd to your computer and use it in GitHub Desktop.
Capistrano 使用

使用版本为 capistrano 3.3.5 , Capistrano 安装方法自行 Google.

项目中初始化 Capistrano

cap install

会生成如下目录文件:

├── Capfile   
├── config   
│   ├── deploy   
│   │   ├── production.rb   
│   │   └── staging.rb   
│   └── deploy.rb   
└── lib   
    └── capistrano   
        └── tasks

其中,

  • Capfile 用来配置 Capistrano
  • deploy.rb 定义共用的 tasks
  • production.rb/staging.rb 定义具体的 stage 的 tasks

新建 stages

cap install STAGES=local,sandbox,qa,production , local,sandbox,qa,production 等可以自行定义

用法

  • cap -T 列出所有 tasks
  • cap staging deploy 部署 staging 环境
  • cap production deploy 部署 production 环境
  • cap production deploy --dry-run 模拟部署 production 环境,但实际上不作任何操作 (?)
  • cap production deploy --prereqs 列出 task dependencies
  • cap production deploy --trace 跟踪 task 调用 (?)

服务器文件结构

config/deploy.rb 中可定义文件根目录:

set :deploy_to, '/var/www/my_app_name'

那么部署之后, /var/www/my_app_name 的文件结构将会如下:

├── current -> /var/www/my_app_name/releases/20150120114500/
├── releases
│   ├── 20150080072500
│   ├── 20150090083000
│   ├── 20150100093500
│   ├── 20150110104000
│   └── 20150120114500
├── repo
│   └── <VCS related data>
├── revisions.log
└── shared
    └── <linked_files and linked_dirs>

其中,

  • current 是指向最新版本的指针
  • releases 存放了所有版本的文件,并以时间戳命名。 current 指针指向这些目录中的一个。
  • repo 存放版本管理系统的配置。如果是 Git 则会是一个完整的 git repository.
  • revisions.log 记录每次 deploy 或者 rollback.
  • shared (?)

配置

配置文件

  • 全局: config/deploy.rb
  • 针对特定 stage: config/deploy/<stage_name>.rb

变量存取

set :application, 'MyLittleApplication'

# use a lambda to delay evaluation
set :application, -> { "SomeThing_#{fetch :other_config}" }
fetch :application
# => "MyLittleApplication"

fetch(:special_thing, 'some_default_value')
# will return the value if set, or the second argument as default value

变量定义

  • :application application 名称
  • :deploy_to 远端服务器部署的根目录。 default: -> { "/var/www/#{fetch(:application)}" }
  • :scm 所使用的版本管理系统。 default: :git
  • :repo_url 代码库的 url
  • :repo_path 远端服务器代码存放路径。 default: -> { "#{fetch(:deploy_to)}/repo" }
  • :repo_tree
  • :linked_files
  • :linked_dirs
  • :default_env
  • :branch
  • :keep_releases
  • :tmp_dir
  • :local_user
  • :pty
  • :log_level
  • :format

用户输入

# used in a configuration
set :database_name, ask('Enter the database name:')


# used in a task
desc "Ask about breakfast"
task :breakfast do
  ask(:breakfast, "pancakes")
  on roles(:all) do |h|
    execute "echo \"$(whoami) wants #{fetch(:breakfast)} for breakfast!\""
  end
end

ask语句获取用户输入,可以用echo: false参数让用户输入不显示:

set :database_password, ask('Enter the database password:', 'default', echo: false)

准备应用

配置服务器地址

# using simple syntax
role :web, %w{hello@world.com example.com:1234}

# using extended syntax (which is equivalent)
server 'world.com', roles: [:web], user: 'hello'
server 'example.com', roles: [:web], port: 1234

配置 VCS

set :application, 'rails3-bootstrap-devise-cancan-demo'
set :repo_url, 'https://github.com/capistrano/rails3-bootstrap-devise-cancan'
set :branch, 'master'

Flow

Capistrano v3 提供了默认的 deploy flow 和 rollback flow. http://capistranorb.com/documentation/getting-started/flow/

Tips

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment