Search code examples
ruby-on-railsherokuamazon-cloudfront

Heroku Pipelines + Amazon Cloudfront and multiple environments


I've set up a Rails app with Heroku Pipelines to be able to automatically promote our staging environment to production. The workflow of master push -> CI run -> staging deploy -> production promote works really well for us.

Our assets are being distributed with Amazon Cloudfront. I setup separate distributions for the staging and production environment, which I configure in the environment:

# Environment variable configured per environment
config.action_controller.asset_host = ENV['ASSET_HOST']

One problem arises with this setup. Even though I configure the asset_host per environment, the assets are being precompiled in the staging environment and -- when promoting with Heroku Pipelines -- will be used in production as well. This causes issues with our custom fonts, CORS..

I thought about configuring a single Cloudfront distribution for multiple origins but could not figure it out. Also, I would really like to keep our current workflow since it enables CD for us.


Solution

  • Configuring Cloudfront with both the staging as well as the production environment as origins and behaviours was the solution.

    For this to work the asset_host should be the same for staging and production.

    Heroku explains this very well:

    For many applications sharing a CDN between staging and production is surprising, however causes no problems. You can manually configure both to have the same “asset_host”. This works because even if the copy of the assets has been modified on the staging app, all assets are fingerprinted, and the “production” app will point browsers at the correct fingerprints. The staging app will serve up to 3 old copies of assets, and the CDN should keep assets cached indefinitely.