Elastic Beanstalk is causing my Rails 6 app precompiled assets to break when adding/removing ENV variables

ⅰ亾dé卋堺 提交于 2021-01-27 22:41:43

问题


I can make code deployments no problem, everything works just fine. The problem is when I use either the Elastic Beanstalk web configuration form or the EB CLI to add/remove/modify ENV variables.

Elastic Beanstalk reports back that the change was made successfully however when I visit the web application in the browser I standard Rails error "Sorry, something went wrong".

SSH'ing into the server looking at the log files shows errors related to missing assets. Looking in the "public" folder of the application there is no longer an "assets" folder containing the precompiled assets which is normally present when I deploy a code change.

I have the following ENV settings in place which are likely relevant:

RAILS_SERVE_STATIC_FILES: true
RAILS_SKIP_ASSET_COMPILATION: false
RACK_ENV: production
RAILS_ENV: production

My production environment config:

Rails.application.configure do
  config.cache_classes = true
  config.eager_load = true
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true
  config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present?
  config.assets.compile = false
  config.active_storage.service = :amazon
  config.force_ssl = ENV.fetch("FORCE_SSL", false)
  config.ssl_options = { redirect: { exclude: ->(request) { request.path =~ /health-check/ } } }
  config.log_level = :debug
  config.log_tags = [:request_id]
  config.action_mailer.perform_caching = false
  config.i18n.fallbacks = true
  config.active_support.deprecation = :notify
  config.log_formatter = ::Logger::Formatter.new

  if ENV["RAILS_LOG_TO_STDOUT"].present?
    logger           = ActiveSupport::Logger.new(STDOUT)
    logger.formatter = config.log_formatter
    config.logger    = ActiveSupport::TaggedLogging.new(logger)
  end

  config.active_record.dump_schema_after_migration = false
end

To fix the issue I just re-deploy the last committed code and the deployment process takes care of generating the missing precompiled assets.

Has anybody encountered this issue or have any insight into what is going on here? Thanks!


回答1:


I met a same problem.

Before 2020 Aug, We couldn't hook these events, but after the issue fixed and platform released for AL2, it became manageable.

You can hook beanstalk configuration changes with confighooks.

Create .platform/confighooks/predeploy/01_assets_precompile.sh like...

#!/bin/bash
source /root/.bash_profile
source <(sed -E -n 's/[^#]+/export &/ p' /opt/elasticbeanstalk/deployment/env)
cd /var/app/staging; bundle exec rake assets:precompile
chown -R webapp:webapp /var/app/staging/

Deploy these scripts, and change beanstalk's env vars.

See also.

  • https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html
  • https://github.com/aws/elastic-beanstalk-roadmap/issues/59


来源:https://stackoverflow.com/questions/62600427/elastic-beanstalk-is-causing-my-rails-6-app-precompiled-assets-to-break-when-add

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!