gpt4 book ai didi

ruby-on-rails - 在亚马逊 EC2 上使用 capistrano 部署到生产环境时, Assets 不会预编译

转载 作者:行者123 更新时间:2023-12-04 00:52:53 24 4
gpt4 key购买 nike

我致力于能够使用 capistrano 部署到生产环境。我面临着几个问题,在解决大部分问题的同时,我们还有最后一个问题。

我们的预编译 Assets 选项没有在生产环境中正确编译它们,因此,我们无法使用最后开发的功能,因为它们严重依赖 JS。

在不试图影响任何人如何分析这个问题的情况下,这是我试图让它发挥作用的一些方法:

  • 本地预编译 Assets ,推送到 github repo,从本地机器部署到 ec2。 cap deploy 是本地的,推送到 ec2 的代码是 github 上的代码。
  • 按照建议尝试使用 capistrano 任务。在 Capfile 中使用 load 'deploy'assets' 并让 cap deploy:setup 任务完成它的工作。
  • 使用选项 cap deploy:assets:clean 然后 cap deploy:assets:precompile
  • 尝试从公共(public)中删除 Assets ,然后在 deploy.rb
  • 中使用 pipeline_precompile 任务
  • 过期的 Assets ,强制 rails 预编译所有改变 application.rb 中 assets.versions 的东西
  • 在 environment/production.rb
  • 中的 config.assets 上尝试了不同的组合
  • 最后,尝试删除生产中的公共(public)/ Assets 并使用 RAILS_ENV=production bundle exec rake assets:precompile
  • 进行预编译。

    该应用程序只是没有使用新的 JS 文件。如果您在 repo 或服务器本身中检查代码,我在 name.js.coffee 中引入了一个简单的注释(第 xxx 行的“# Shows and hides menus based on the data on DB”),这不在在生产中编译的 assets.js。这是一个快速测试,以确保正在使用最近的 Assets 。

    这里的整个问题是 js 和 css 文件,而不是 Rails。这就是为什么它如此难以测试或找到的原因。这也是最近 js 框架流行的原因之一。万一出现问题,您不必为了寻找问题所在而自杀。如果问题出现在 ruby​​ 或 rails 中,通常不会花那么长时间就能找出问题所在。一旦你获得了 js、css 和跨浏览器的兼容性,这就是手头的问题。

    这是我的 deploy.rb 文件。运行 rails 3.2.12 ruby​​-1.9.3-p327:
    # $:.unshift(File.expand_path('./lib', ENV['rvm_path']))

    # Load rvm's capistrono plugins
    require 'rvm/capistrano'

    require 'bundler/capistrano'

    set :rvm_type, :user

    set :user, 'username'
    set :domain, 'ip_address'
    set :application, "app_pro"
    set :keep_releases, 2 # It keeps on two old releases.

    # git repo details
    set :scm, :git # You can set :scm explicitly or Capistrano will make an intelligent guess based on known version control directory names
    set :repository, "git@github.com:user/app.git"
    set :scm_username, 'user'
    set :git_enable_submodules, 1
    set :git_shallow_clone, 1
    set :branch, 'master'

    # Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`


    role :web, domain # Your HTTP server, Apache/etc
    role :app, domain # This may be the same as your `Web` server
    role :db, domain, :primary => true# 'ec2-23-23-156-118.compute-1.amazonaws.com' This is where Rails migrations will run
    # role :db, "your slave db-server here"

    # deply options
    default_run_options[:pty] = true
    set :ssh_options, {:forward_agent => true}
    set :ssh_options, {:auth_methods => "publickey"}
    set :ssh_options, {:keys => ["~/Downloads/key.pem"]}
    set :deploy_to, "/home/user/appdir"
    set :deploy_via, :remote_cache
    set :use_sudo, false

    # if you want to clean up old releases on each deploy uncomment this:
    after "deploy:restart", "deploy:cleanup"

    # if you're still using the script/reaper helper you will need
    # these http://github.com/rails/irs_process_scripts

    # If you are using Passenger mod_rails uncomment this:
    namespace :deploy do
    task :start do
    # run COMMAND="/etc/init.d/nginx restart" invoke SUDO=1
    run "sudo /etc/init.d/nginx restart"
    # exit
    end
    after "deploy:start", "deploy:cleanup"

    task :stop do ; end
    task :restart, :roles => :app, :except => { :no_release => true } do
    run "touch #{File.join(current_path,'tmp','restart.txt')}"
    end

    task :setup_config, roles: :app do
    run "mkdir -p #{shared_path}/config"
    put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"
    puts 'now edit the config file database in #{shared_path}'
    end
    after 'deploy:setup', 'deploy:setup_config'

    desc "Symlink shared resources on each release - not used"
    task :symlink_config, :roles => :app do
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
    end

    after 'deploy:finalize_update', 'deploy:symlink_config'

    desc "It helps to seed database with values"
    task :seed do
    run "cd #{current_path}; bundle exec rake db:seed RAILS_ENV=#{rails_env}"
    end
    task :create_schema do
    run "cd #{current_path}; bundle exec rake db:create RAILS_ENV=#{rails_env} --trace"
    end
    end

    正在运行的新/替代 (deploy_new2.rb) 文件:
    # On-working new/alternative deploy.rb file:

    require 'rvm/capistrano'
    require 'bundler/capistrano'

    set :rvm_type, :user

    set :application, "ip_address"
    set :domain, 'ip_address'

    # Roles
    role :web, domain
    role :app, domain
    role :db, domain, :primary => true

    #deployment details
    set :deploy_via, :remote_cache
    set :user, "username"
    set :copy_compression, :bz2
    set :git_shallow_clone, 1
    set :scm_verbose, true
    set :use_sudo, false
    set :deploy_to, "/home/user/dir"

    default_run_options[:pty] = true
    set :ssh_options, {:forward_agent => true}
    set :ssh_options, {:auth_methods => "publickey"}
    set :ssh_options, {:keys => ["~/Downloads/key.pem"]}

    #repo details
    set :scm, :git
    set :repository, "git@github.com:user/app.git"
    set :scm_username, 'user'
    set :keep_releases, 2
    set :branch, "master"


    namespace :deploy do
    # task :start, :roles => :app, :except => { :no_release => true } do
    # # not need to restart nginx every time
    # # run "service nginx start"
    # run "cd #{release_path} && touch tmp/restart.txt"
    # end

    # after "deploy:start", "deploy:cleanup"
    # after 'deploy:cleanup', 'deploy:symlink_config'

    # You do not need reload nginx every time, eventhought if you use passenger or unicorn
    # task :stop, :roles => :app, :except => { :no_release => true } do
    # run "service nginx stop"
    # end

    # task :graceful_stop, :roles => :app, :except => { :no_release => true } do
    # run "service nginx stop"
    # end

    # task :reload, :roles => :app, :except => { :no_release => true } do
    # run "cd #{release_path} && touch tmp/restart.txt"
    # run "service nginx restart"
    # end

    task :restart, :roles => :app, :except => { :no_release => true } do
    run "cd #{release_path} && touch tmp/restart.txt"
    end

    # If you enable assets/deploy in Capfile, you do not need this
    # task :pipeline_precompile do
    # # run "cd #{release_path}; RAILS_ENV=#{rails_env} bundle exec rake assets:precompile"
    # # precompile assets before deploy and upload them to server
    # # run_locally("RAILS_ENV=#{rails_env} rake assets:clean && RAILS_ENV=#{rails_env} rake assets:precompile")
    # # top.upload "public/assets", "#{release_path}/public/assets", :via =>:scp, :recursive => true
    # end
    end

    # you do not need to this, because you already add require 'bundler/capistrano'
    # before "deploy:assets:precompile", "bundle:install"

    和./Capfile:
    load 'deploy'
    # Uncomment if you are using Rails' asset pipeline
    load 'deploy/assets'
    load 'config/deploy' # remove this line to skip loading any of the default tasks

    预先感谢您的任何帮助!如果您需要更多信息,请告诉我。

    最佳答案

    您不需要自己的 :precompile_assets 任务。您通过在 Capfile 中包含加载“部署/ Assets ”来使用 Capistrano。

    从 deploy.rb 中删除 :precompile_assets 任务可能会解决问题。如果您查看 Capistrano 的源代码,您会发现它实现 :precompile_assets 的方式完全不同:https://github.com/capistrano/capistrano/blob/legacy-v2/lib/capistrano/recipes/deploy/assets.rb

    关于ruby-on-rails - 在亚马逊 EC2 上使用 capistrano 部署到生产环境时, Assets 不会预编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15581820/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com