1 安装

gem install capistrano

// For mutiple stages
gem install capistrano-ext

2 准备

capify .

这个命令会创建Capfile,和 config/deploy.rb 文件。Capfile会帮助加在一些合适的库文件和配置。

配置deploy.rb

set :application, "applciation_name"

//git
set :scm, :git
set :repository, "git@www.xxx.com:/repository.git"
set :scm_passphrase, "" //sub version
set :scm, :subversion
set :repository, "https://account.svn.beanstalkapp.com/repository" // user name
set :user, "server-user-name"

多stage需要包含文件

require 'capistrano/ext/multistage'

然后

set :stages, ["staging", "production"]
set :default_stage, "staging"

然后在config目录下创建一个文件夹deploy 然后创建两个文件 production.rb staging.rb

production.rb

server "xxx.com", :app, :web, :db, :primary => true
set :deploy_to, "/var/www/application_name"

staging.rb

server "xxx.com", :app, :web, :db, :primary => true
set :deploy_to, "/var/www/application_staging"

3 一些设置

cap deploy:setup

这条命令SSH到服务器,创建deploy_to的目录。

errors:

    [xxx.com] executing command
*** [err :: xxx.co] sudo
*** [err :: xxx.co] :
*** [err :: xxx.co] sorry, you must have a tty to run sudo
*** [err :: xxx.co]
command finished in 718ms
failed: "sh -c 'sudo -p '\\''sudo password: '\\'' mkdir -p /var/www/TTQuan_staging /var/www/xxx_staging/releases /var/www/xxx_staging/shared /var/www/xxx_staging/shared/system /var/www/TTQuan_staging/shared/log /var/www/xxx_staging/shared/pids'" on xxx.co

要添加

default_run_options[:pty] = true

确信你的设置没有问题

cap deploy:check

 You appear to have all necessary dependencies installed

最后部署

cap deploy
cap production deploy

5 Tips & Tricks

1) Capistrano 第一次部署都会clone/export respository,然后之后每次git pull/svn up就会替代clone/export。如果你经常deploy 那这样会加速部署

set :deploy_via, :remote_cache

2)创建tasks

namespace :deploy do
task :restart, :roles => :web do
run "touch #{ current_path }/tmp/restart.txt"
end task :restart_daemons, :roles => :app do
sudo "monit restart all -g daemons"
end
end

Capistrano 不仅仅是通过ssh复制文件。完成复制文件之后你还可以配置一些事件和命令,例如通过自定义脚本重启服务器。Capistrano叫这些为“tasks”。

这里例子很简单。restart就是部署完成之后运行的。 “touch tmp/restart.txt“ 是用passenger时候使用的,但是如果你使用unicorn就换成unicron的就可以了。

下面的task 不会自动运行

after "deploy", "deploy:restart_daemons"

3)关联环境的branches

production.rb

set :branch, 'production'

staging.rb

set :branch, 'staging'

cap deploy 默认是staging

cap production deploy

参考 https://github.com/capistrano/capistrano/wiki

最新文章

  1. 导入 cocoapods引入的第三方库头文件,提示找不到
  2. Nginx反向代理的工作方式
  3. 亿级Web系统搭建——单机到分布式集群
  4. AFNetworking讲解
  5. js:数据结构笔记4--队列
  6. Android网络通信之WiFi Direct
  7. POJ 3320
  8. maven中scope参数说明
  9. [Mugeda HTML5技术教程之1] HTML5: 生存还是毁灭
  10. XML 学习之保存节点
  11. uCOS-iii 中定义的一些常量
  12. 【论文:麦克风阵列增强】Speech Enhancement Based on the General Transfer Function GSC and Postfiltering
  13. MySQL连接方式及大小写问题
  14. Java 第二章 变量、数据类型和运算符
  15. 「雅礼集训 2017 Day5」矩阵
  16. Vue.js中集成summernote
  17. 如何让写得html页面自动刷新
  18. 【算法】LeetCode算法题-Valid Parentheses
  19. Latex小技巧
  20. C# WinForm 菜单项的大小、高宽的手动控制

热门文章

  1. 【玩转Golang】reflect.DeepEqual
  2. 【WP8】同步执行异步代码
  3. wordpress主题升级之后返回到原来版本主题的方法
  4. QT编译错误:cannot find file: *.pro
  5. 【转】ZooKeeper学习第二期--Zookeeper命令操作
  6. 【转】细谈Redis和Memcached的区别
  7. 8 -- 深入使用Spring -- 2...4 使用@PostConstruct和@PreDestroy定制生命周期行为
  8. GoF--装饰者设计模式
  9. 处理特殊格式的GET传参
  10. centos 上不了网了