设为首页 收藏本站
查看: 808|回复: 0

[经验分享] Rails + passenger + git + capistrano实现自动化部署

[复制链接]

尚未签到

发表于 2018-9-19 06:03:20 | 显示全部楼层 |阅读模式
  首先要:yum install sudo which #如果有就算了
  1,我们需要关心的主要是config/deploy.rb这个文件,我们先来看看程序自动为我们生成了些什么:
  [root@master blog]# cat config/deploy.rb
  set :application, "set your application name here"
  set :repository,  "set your repository location here"
  set :scm, :subversion
  # Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
  role :web, "your web-server here"                          # Your HTTP server, Apache/etc
  role :app, "your app-server here"                          # This may be the same as your `Web` server
  role :db,  "your primary db-server here", :primary => true # This is where Rails migrations will run
  role :db,  "your slave db-server here"
  # If you are using Passenger mod_rails uncomment this:
  # if you're still using the script/reapear helper you will need
  # these http://github.com/rails/irs_process_scripts
  # namespace :deploy do
  #   task :start do ; end
  #   task :stop do ; end
  #   task :restart, :roles => :app, :except => { :no_release => true } do
  #     run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
  #   end
  可以看到系统已经提示我们一些基本的设置了
  首先,设置application
  set :application, "blog" #在这里我给他起名字叫blog,当然这个根据具体情况了
  设置repository,我用的是git:
  set :repository,  "ssh://root@www.weekface.info/usr/local/system/repository/blog.git"
  set :scm, :git
  set :scm_command, "/usr/local/git/bin/git" #这个也很关键啊,否则会找不到git命令了
  #set :scm_passphrase, "xxxx" 没有密码的就不用设置了
  接下来就是设置你的server,我的是在同一台机器上面,所以都相同了:
  role :web, "www.weekface.info"                     # Your HTTP server, Apache/etc
  role :app, "www.weekface.info"                          # This may be the same as your `Web` server
  role :db,  "www.weekface.info", :primary => true # This is where Rails migrations will run
  再接下来是部署的位置:
  set :deploy_to, "/usr/local/system/www/#{application}"
  部署的时候是用ssh public key方式连接合服务器,这个在以前的文章中说明过
  下面是使用者相关的配置:
  set :user, "root"
  set :runner, "root"
  大部分部署指令会以:user设定的身份去运行,但和启动应用服务器相关的指令会以:runner设置的身份去运行.
  最后,由于我用的是passenger,所以来设置一下启动passenger的相关设置吧:
  namespace :deploy do
  task :start do ; end
  task :stop do ; end
  task :restart, :roles => :app, :except => { :no_release => true } do
  run "touch #{File.join(current_path,'tmp','restart.txt')}"
  end
  end
  2,部署
  第一次部署需要运行下面的命令:
  cap deploy:setup
  报错了:
  sh: sudo: command not found
  #或者
  sudo: sorry, you must have a tty to run sudo
  google了一下,可以这样解决:
  [root@li96-10 ~]# vim /etc/sudoers
  # Disable "ssh hostname sudo ", because it will show the password in clear.
  #         You have to run "ssh -t hostname sudo ".
  #
  #Defaults    requiretty #将这行注释掉
  继续执行:
  [root@master blog]# cap deploy:setup
  * executing `deploy:setup'
  * executing "sudo -p 'sudo password: ' mkdir -p /usr/local/system/www/blog /usr/local/system/www/blog/releases /usr/local/system/www/blog/shared /usr/local/system/www/blog/shared/system /usr/local/system/www/blog/shared/log /usr/local/system/www/blog/shared/pids && sudo -p 'sudo password: ' chmod g+w /usr/local/system/www/blog /usr/local/system/www/blog/releases /usr/local/system/www/blog/shared /usr/local/system/www/blog/shared/system /usr/local/system/www/blog/shared/log /usr/local/system/www/blog/shared/pids"
  servers: ["www.weekface.info"]
  [www.weekface.info] executing command
  command finished
  这个命令会在服务器上配置好相关的目录结构.然后再运行:,以检查目录结构是否配置正确:
  [root@master blog]# cap deploy:check
  * executing `deploy:check'
  * executing "test -d /usr/local/system/www/blog/releases"
  servers: ["www.weekface.info"]
  [www.weekface.info] executing command
  command finished
  * executing "test -w /usr/local/system/www/blog"
  servers: ["www.weekface.info"]
  [www.weekface.info] executing command
  command finished
  * executing "test -w /usr/local/system/www/blog/releases"
  servers: ["www.weekface.info"]
  [www.weekface.info] executing command
  command finished
  * executing "which git"
  servers: ["www.weekface.info"]
  [www.weekface.info] executing command
  www.weekface.info: sh: which: command not found
  command finished
  The following dependencies failed. Please check them and try again:
  --> `git' could not be found in the path (www.weekface.info) #见上面的解决办法:set :scm_command, "/usr/local/git/bin/git"
  再试一下:
  [root@master blog]# cap deploy:check
  * executing `deploy:check'
  * executing "test -d /usr/local/system/www/blog/releases"
  servers: ["www.weekface.info"]
  [www.weekface.info] executing command
  command finished
  * executing "test -w /usr/local/system/www/blog"
  servers: ["www.weekface.info"]
  [www.weekface.info] executing command
  command finished
  * executing "test -w /usr/local/system/www/blog/releases"
  servers: ["www.weekface.info"]
  [www.weekface.info] executing command
  command finished
  * executing "which /usr/local/git/bin/git"
  servers: ["www.weekface.info"]
  [www.weekface.info] executing command
  command finished
  You appear to have all necessary dependencies installed
  这就代表检查成功了!
  下面就开始第一次部署:
  [root@master blog]# cap deploy:cold
  * executing `deploy:cold'
  * executing `deploy:update'
  ** transaction: start
  * executing `deploy:update_code'
  executing locally: "/usr/local/git/bin/git ls-remote ssh://www.weekface.info/usr/local/system/repository/blog.git HEAD"
  * executing "/usr/local/git/bin/git clone -q ssh://www.weekface.info/usr/local/system/repository/blog.git /usr/local/system/www/blog/releases/20100107141957 && cd /usr/local/system/www/blog/releases/20100107141957 && /usr/local/git/bin/git checkout -q -b deploy a2546709a9109af3d6f098030c2fb7e3acc89b12 && (echo a2546709a9109af3d6f098030c2fb7e3acc89b12 > /usr/local/system/www/blog/releases/20100107141957/REVISION)"
  servers: ["www.weekface.info"]
  [www.weekface.info] executing command
  ** [www.weekface.info :: err] Host key verification failed. #这里这里
  ** [www.weekface.info :: err] fatal: The remote end hung up unexpectedly
  command finished
  *** [deploy:update_code] rolling back
  * executing "rm -rf /usr/local/system/www/blog/releases/20100107141957; true"
  servers: ["www.weekface.info"]
  [www.weekface.info] executing command
  command finished
  failed: "sh -c '/usr/local/git/bin/git clone -q ssh://www.weekface.info/usr/local/system/repository/blog.git /usr/local/system/www/blog/releases/20100107141957 && cd /usr/local/system/www/blog/releases/20100107141957 && /usr/local/git/bin/git checkout -q -b deploy a2546709a9109af3d6f098030c2fb7e3acc89b12 && (echo a2546709a9109af3d6f098030c2fb7e3acc89b12 > /usr/local/system/www/blog/releases/20100107141957/REVISION)'" on www.weekface.info
  解决办法是修改config/deploy.rb,加入:
  set :branch, 'master'
  set :deploy_via, :copy
  继续:
  [root@master blog]# cap deploy:cold
  ...
  ...
  * executing "cd /usr/local/system/www/blog/releases/20100107143738; rake RAILS_ENV=production  db:migrate"
  servers: ["www.weekface.info"]
  [www.weekface.info] executing command
  *** [err :: www.weekface.info] sh: rake: command not found
  command finished
  failed: "sh -c 'cd /usr/local/system/www/blog/releases/20100107143738; rake RAILS_ENV=production  db:migrate'" on www.weekface.info
  google了一下,在config/deploy.rb加入:
  set :rake, "/usr/local/system/ruby/rake"
  然后再继续:
  [root@master blog]# cap deploy:cold
  ...
  ...
  library: no such file to load -- system_timer #解决办法 : gem install system_timer
  呵呵,以后再部署就可以直接用:
  cap deploy
  如果发生问题要rollback可以用:
  cap deploy:rollback
  到此为止…睡觉去
  Rails启动时有个错:uninitialized constant User::Authentication,结果发现是restful_authentation这个插件目录里是空的,不知道怎么搞的,重新把文件拷贝过去就行了.
  接下来还有在deploy.rb文件里自定义一些任务(task),完成一些其他任务,比如建立public/photos的连结等等:
  首先,从git中删除不需要版本控制的文件:
  在本机的blog目录下新建文件.gitignore:
  config/database.yml
  config/environment.rb
  public/photos
  log
  nbproject
  *~
  *.cache
  *.log
  *.pid
  tmp/**/*
  以上这些文件都不需要版本控制,然后push,同时在服务器上要将config/database.yml,config/environment.rb,public/photos/*文件给他复制到shared目录下,并且将这些文件目录从git中删除,一会我们会给他 在部署的时候作link
  [root@master blog]# git add .gitignore
  [root@master blog]# git commit -am "add .gitignore"
  [root@master blog]# git push
  编辑deploy.rb文件,加入一下tasks:

  desc "Symlink shared configs and folders on each>  task :symlink_shared do
  run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
  run "ln -nfs #{shared_path}/config/environment.rb #{release_path}/config/environment.rb"
  run "ln -nfs #{shared_path}/public/photos #{release_path}/public/photos"
  #run "export RAILS_ENV=production && #{release_path}/script/workling_client start"
  end
  after 'deploy:update_code', 'deploy:symlink_shared'
  基本就是这样了,以后还会介绍在实际使用过程中遇到的一些问题:
  Rails Error: Unable to access log file. Please ensure that /usr/local/system/www/blog/releases/20100109050547/log/production.log exists and is chmod 0666. The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
  这个问题主要是因为shared/log目录没有可写权限,解决办法:
  [root@li96-10 shared]# chmod -R 0666 log
  还有一个问题:重启workling时报错:/usr/bin/env: ruby no such file or directory
  * executing "cd /usr/local/system/www/blog/releases/20100109062537 && RAILS_ENV=production ./script/workling_client stop"
  servers: ["www.weekface.info"]
  [www.weekface.info] executing command
  *** [err :: www.weekface.info] /usr/bin/env: ruby
  *** [err :: www.weekface.info] : No such file or directory
  原因是找不到ruby,下面是暂时解决办法:
  [root@master blog]# vim script/workling_client
  #将第一行的 :#!/usr/bin/env ruby
  #修改为: #!/usr/local/system/ruby-1.8.7-p72/bin/ruby
  #就是hard code了,呵呵
  [root@master blog]# git commit -am "xx"  #然后提交到git
  就可以了


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-593556-1-1.html 上篇帖子: android git gerrit 一些基本概念和问题 下篇帖子: git svn comparision
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表