部署 发布生产环境时,由于资源使用的是 UPYUN,自动化部署

dothide · 2015年05月21日 · 最后由 quakewang 回复于 2015年05月21日 · 1715 次阅读

由于使用了 UPYUN 做资源存储,替换资源方便不少,在 production.rb 中已经进行了如下设置:

config.action_controller.asset_host = Settings.upyun_url

但每次发布后,都需要重新 FTP 发布资源,在寻求自动化方法的路上,我找到了 gem 'rails-assets-for-upyun', '>= 0.0.9',可以用命令上传方便多了,但仍然没有解决自动化部署,于是考虑在 mina 的 deploy.rb 中写一段自动部署脚本,于是就解决了。

# => Deploy task
# ===========================================================================================
desc "Deploys the current version to the server."
task :deploy => :environment do
  deploy do
    # Put things that will set up an empty directory into a fully set-up
    # instance of your project.
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rails:db_migrate'
    invoke :'rails:assets_precompile'
    invoke :'rails:assets_publish_assets_upyun'
    invoke :'deploy:cleanup'

    to :launch do
      invoke :'unicorn:restart'
    end
  end
end
namespace :rails do
  set :publish_upyun, %{
    cd #{app_path}
    bundle exec rake assets:publish_assets_upyun RAILS_ENV=production
  }
  task :assets_publish_assets_upyun do
    queue 'echo "-----> Assets To UPYUN"'
    queue! publish_upyun
  end
end

Rakefile

...
namespace :assets do
  task :publish_assets_upyun do
    RailsAssetsForUpyun.publish 'bucket', 'username', 'password'
  end
end

我们的项目现在也是用的这种方法,但是有一个问题是在上传到又拍云的过程中由于图片文件过多或者过大导致超时,经常会出现这种情况,还有就是这个过程不能回滚,如果在上传完之后某个流程失败,更换了的图片就无法回滚到上一个版本。

upyun 现在不是有 cdn 功能吗?在 upyun 那边配置一个源地址就可以了。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号