使用版本为 capistrano 3.3.5 , Capistrano 安装方法自行 Google.
cap install
会生成如下目录文件:
├── Capfile
├── config
│ ├── deploy
│ │ ├── production.rb
│ │ └── staging.rb
│ └── deploy.rb
└── lib
└── capistrano
└── tasks
其中,
Capfile用来配置 Capistranodeploy.rb定义共用的 tasksproduction.rb/staging.rb定义具体的 stage 的 tasks
cap install STAGES=local,sandbox,qa,production , local,sandbox,qa,production 等可以自行定义
cap -T列出所有 taskscap staging deploy部署 staging 环境cap production deploy部署 production 环境cap production deploy --dry-run模拟部署 production 环境,但实际上不作任何操作 (?)cap production deploy --prereqs列出 task dependenciescap 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
:applicationapplication 名称: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
set :application, 'rails3-bootstrap-devise-cancan-demo'
set :repo_url, 'https://github.com/capistrano/rails3-bootstrap-devise-cancan'
set :branch, 'master'
Capistrano v3 提供了默认的 deploy flow 和 rollback flow. http://capistranorb.com/documentation/getting-started/flow/
%w(a bb ccc)等于["a", "bb", "ccc"]- 在 Capistrano v3 里不能用
cap dev deploy -s branch=en/3.1这种方法来传递参数了,新的方法可以看这里http://stackoverflow.com/questions/21027452/capistrano-3-pulling-command-line-arguments