Getting Heroku to precompile assets to Amazon aws s3 - heroku

Almost there with my issue of getting AWS S3 to serve my heroku app its static assets
Everything works locally, i run my rake task and then the assets get precompiled to AWS ( though there is never any output in the terminal?)
Then to heroku, where i set my ENV variables like so
heroku config:add aws_access_key=mysecretkey aws_secret_key=mypublickey aws_bucket=mybucketname
I have two buckets, one for dev and one for production. Do i need to set anything else in the heroku:config?
Heres what i have so far
config.rb
AssetSync.configure do |con|
con.fog_provider = 'AWS'
con.fog_region = 'eu-west-1'
con.fog_directory = ENV['aws_bucket']
con.aws_access_key_id = ENV['aws_access_key']
con.aws_secret_access_key = ENV['aws_secret_key']
con.prefix = "assets"
con.public_path = Pathname("./public")
end
Question here, how much of this will heroku read and use, I know i have to set the ENV explicitly in heroku but will it read the rest and carry out the rest of the task, ie will it read prefix, public_path etc ?
Rakefile
require 'bundler/setup'
Bundler.require(:default)
require 'active_support/core_ext'
require './config/env' if File.exists?('config/env.rb')
require './config/config'
namespace :assets do
desc "Precompile assets"
task :precompile do
AssetSync.sync
end
end
When running
heroku run rake assets:precompile
i get the output
`rake assets:precompile` attached to terminal...up, run.2942 The source :rubygems is deprecated because HTTP requests are insecure. Please change your source to 'https://rubygems.org (in /app)
Then it just returns to the terminal, I expected a list of all the assets that where to be compiled listed as the task was being carried out. Upon checking my bucket in aws, it is empty
EDIT
Output after heroku logs --tail
2013-03-19T10:55:50+00:00 heroku[api]: Starting process with command `bundle exec rake assets:precompile` by richlewis14#gmail.com
2013-03-19T10:55:52+00:00 heroku[run.6701]: State changed from starting to up
2013-03-19T10:55:53+00:00 heroku[run.6701]: Awaiting client
2013-03-19T10:55:53+00:00 heroku[run.6701]: Starting process with command `bundle exec rake assets:precompile`
2013-03-19T10:55:59+00:00 heroku[run.6701]: Client connection closed. Sending SIGHUP to all processes
2013-03-19T10:56:00+00:00 heroku[run.6701]: Process exited with status 0
2013-03-19T10:56:00+00:00 heroku[run.6701]: State changed from up to complete
More was added after about 15 minutes
! Heroku client internal error.
! Search for help at: https://help.heroku.com
! Or report a bug at: https://github.com/heroku/heroku/issues/new
Error: An existing connection was forcibly closed by the remote host
(Errno::ECONNRESET)
Command: heroku logs --tail
Version: heroku-gem/2.35.0 (i386-mingw32) ruby/1.9.3

This is a bundler error due to the source stated in the Gemfile. It is not related to the asset compilation per se.
Specify
source 'https://rubygems.org'
at the top of your Gemfile.

Related

capistrano aborts - SSHKit::Runner::ExecuteError and Devise.secret_key was not set

I'm trying to update an app (Rails 4.1.12; ruby 1.9.3p484) by deploying changes from development to production using capistrano. Below is log (I masked the IP address and suggested secret key).
Previously never had a problem running a deploy. Have searched and found posts with similar errors but have not found a solution. I did follow the instructions to add Devise.secret_key to the Devise initializer and restarted the application (dev and prod). Next attempt to deploy received same error with suggestion for different secret key.
All help, hints, suggestions are greatly appreciated. Thanks.
UPDATE:I just tried to deploy using
bundle exec cap production deploy
and did not get errors but still get errors when running
cap production deploy
UPDATE 2: I used Figaro to generate an application.yml file (and append to gitignore). In the devise.rb file I created a line config.secret_key = ENV["config.secret_key"] In the application.yml file I created a line config.secret_key: 'random_128_chr_string' I then restarted the development server and ran cap production deploy Unfortunately got same error.
cap production deploy
.
.
.
[deploy:migrate] Run `rake db:migrate`
[e0c2c251] Running bundle exec rake db:migrate as deployer#123.123.123.12
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as deployer#123.123.123.12: rake exit status: 1
rake stdout: Nothing written
rake stderr: rake aborted!
Devise.secret_key was not set. Please add the following to your Devise initializer:
config.secret_key = 'looong_string_of_numbers_and_letters'
Please ensure you restarted your application after installing Devise or setting the key.
deploy.rb:
lock '>=3.2.1'
set :application, 'pepperslice'
set :repo_url, 'git#github.com:jeffaltman/pepperslice.git'
set :linked_dirs, %w{log tmp public/images/uploaded}
set :linked_files, %w{config/database.yml config/initializers/devise.rb config/initializers/smtp_settings.rb}
set :format, :pretty
set :log_level, :info
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
execute "sudo /usr/sbin/service unicorn restart"
end
end
after :publishing, :restart
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
end
capfile:
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
# Includes tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
# https://github.com/capistrano/rvm
# https://github.com/capistrano/rbenv
# https://github.com/capistrano/chruby
# https://github.com/capistrano/bundler
# https://github.com/capistrano/rails
#
# require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
require 'capistrano/bundler'
# require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
#require 'capistrano/ssh_doctor'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

Assets loading issue on Rails 5 app with Heroku

I am facing asset loading issue in Rails 5 application deployed on Heroku.
App Configuration is,
ruby => ‘2.3.1’
rails => '~> 5.0.1'
When image is stored on path,
app/assets/home/image1.jpg
I am accessing it in view as,
= image_tag('/assets/home/image1.jpg’)
which is working properly in Development ENV, but not in Production ENV.
As per Heroku log,
ActionController::RoutingError (No route matches [GET]
"/assets/home/image1.jpg")
If I am moving image directly to
app/assets/image1.jpg
then its working on Production ENV.
Please guide about it.
Thanks
It looks like you assets are not compile on heroku.
Follow below code:
config/environments/production.rb
config.assets.compile = true
then run commands:
RAILS_ENV=production rake assets:precompile
then push all compiled files with menifest file to heroku.

Can't access Heroku console with app name

I just started a new app and I was able to push it to Heroku, But I it seems, that I can't access the console.
The command, that I am running is:
heroku run console --app myappname
What I get, is:
Running `console` attached to terminal... up, run.3951 Usage: rails
new APP_PATH [options]
Options: -r, [--ruby=PATH] # Path to the Ruby binary of
your choice
# Default: /app/vendor/ruby-1.9.3/bin/ruby -b, [--builder=BUILDER] #
Path to a application builder (can be a filesystem path or URL) -m,
[--template=TEMPLATE] # Path to an application template (can be a
filesystem path or URL)
[--skip-gemfile] # Don't create a Gemfile
[--skip-bundle] # Don't run bundle install -G, [--skip-git] # Skip Git ignores and keeps -O,
[--skip-active-record] # Skip Active Record files -S,
[--skip-sprockets] # Skip Sprockets files -d,
[--database=DATABASE] # Preconfigure for selected database
(options:
mysql/oracle/postgresql/sqlite3/frontbase/ibm_db/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc)
# Default: sqlite3 -j, [--javascript=JAVASCRIPT] # Preconfigure for selected JavaScript
library
# Default: jquery -J, [--skip-javascript] # Skip JavaScript files
[--dev] # Setup the application with Gemfile pointing to your Rails checkout
[--edge] # Setup the application with Gemfile pointing to Rails repository -T, [--skip-test-unit] # Skip
Test::Unit files
[--old-style-hash] # Force using old style hash (:foo = 'bar') on Ruby >= 1.9
Runtime options: -f, [--force] # Overwrite files that already
exist -p, [--pretend] # Run but do not make any changes -q,
[--quiet] # Suppress status output -s, [--skip] # Skip files
that already exist
Rails options: -h, [--help] # Show this help message and quit
-v, [--version] # Show Rails version number and quit
Description:
The 'rails new' command creates a new Rails application with a default
directory structure and configuration at the path you specify.
You can specify extra command-line arguments to be used every time
'rails new' runs in the .railsrc configuration file in your home directory.
Note that the arguments specified in the .railsrc file don't affect the
defaults values shown above in this help message.
Example:
rails new ~/Code/Ruby/weblog
This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
See the README in the newly created application to get going.
I know, that the application name is correct, as I just pushed it and it is loaded.
When I look at the logs on Heroku for the moment, when I tried to hit the console, I see following:
2013-07-01T16:21:58.780979+00:00 heroku[api]: Starting process with command `bundle exec rails console` by mzaragoza#myemail.com
2013-07-01T16:22:09.000482+00:00 heroku[run.2993]: Awaiting client
2013-07-01T16:22:09.055474+00:00 heroku[run.2993]: Starting process with command `bundle exec rails console`
2013-07-01T16:22:10.342966+00:00 heroku[run.2993]: State changed from starting to up
2013-07-01T16:22:13.870963+00:00 heroku[run.2993]: Process exited with status 0
2013-07-01T16:22:13.889703+00:00 heroku[run.2993]: State changed from up to complete
What about heroku run rails c? Does it make any difference if you add rails?
PS: I'm unsure if you want to access Rails' console or just a normal shell, from the comments.
The correct form is => heroku run "any rails command here"
I had this problem when I upgraded to Rails > 4.0.0. The solution is to run locally the following command rake rails:update:bin. This will generate a bin directory in the root of your application. Make sure that it is not in your .gitignore file. Commit and then push the changes to Heroku.
For 3.2.x apps:
This also just happened to me when I removed script/rails from my repo and pushed to Heroku. Reverting the commit and pushing that change made things work again.

ENV variables not being read sinatra

So i am trying to serve my static assets from Amazon s3 locally and for Heroku, I was loading a yml file but that doesn't work as Heroku doesn't accept symlinks.
So i was given the suggestion to use ENV variables as Heroku also uses these. I have a rake task to precompile the assets to AWS. two problems at the moment
1) My ENV variables are not being read.(Fog directory can't be blank, Aws access key can't be blank, Aws secret access key can't be blank
2) When running a rake task i also get the error 'already initialized constant VALID_CHARACTER'
So the activesupport constant is being loaded twice?
My setup
env.rb
ENV['aws_bucket'] = 'bucketname'
ENV['aws_access_key'] = 'myaccesskey'
ENV['aws_secret_key'] = 'mysecretkey'
Rakefile
require 'bundler/setup'
Bundler.require(:default)
require './env' if File.exists?('env.rb')
AssetSync.configure do |con|
con.fog_provider = 'AWS'
con.fog_region = 'eu-west-1'
con.fog_directory = ENV['aws_bucket']
con.aws_access_key_id = ENV['aws_access_key']
con.aws_secret_access_key = ENV['aws_secret_key']
con.prefix = "assets"
con.public_path = Pathname("./public")
end
namespace :assets do
desc "Precompile assets"
task :precompile do
AssetSync.sync
end
Gemfile
source :rubygems
gem 'sinatra'
gem 'pony'
gem 'sinatra-flash'
gem 'heroku'
gem 'asset_sync', git: 'git://github.com/ejholmes/asset_sync.git', branch: 'sinatra'
UPDATE
AssetSync has activesupport in it's gemspec so that's getting included any way. It seems to be conflicting with the constant defined in the mail gem from the pony gemspec.
So with the Pony gem removed i can precompile assets locally, but when i try and compile for heroku nothing happens, it starts the rake task but then just goes back to the terminal ready for a new command.
The other thing is i need Pony for my mailer, how can i get around this?
To get rid of the clash between Pony and when running Rake locally, put the gems into different groups, e.g.
# Gemfile
group :assets do
gem 'asset_sync', git: 'git://github.com/ejholmes/asset_sync.git', branch: 'sinatra'
end
group :mail do
gem "pony"
end
# more…
in the Rakefile
Bundler.require(:assets,:database,:whatever_else_you_need)
in the rackup/app file
Bundler.require(default,:assets,:database,:mail,:whatever_else_you_need)
As to your other problem, you should set the env vars for production via heroku config (see https://devcenter.heroku.com/articles/config-vars) and load them locally using the Rakefile as I said in the other question you asked about this. The env vars will live for the extent of the Ruby process, so if you load them in via Rake and also start the local server within the same Rake process you'll get Sinatra picking up all the env vars.
Edit: env vars will last as long as the process that added them, so if you put them in a dependent task the following task will have access to them:
namespace :assets do
task :environment do
AssetSync.configure do |con|
con.fog_provider = 'AWS'
con.fog_region = 'eu-west-1'
con.fog_directory = ENV['aws_bucket']
con.aws_access_key_id = ENV['aws_access_key']
con.aws_secret_access_key = ENV['aws_secret_key']
con.prefix = "assets"
con.public_path = Pathname("./public")
end
end
desc "Precompile assets"
task :precompile => :"assets:environment" do
AssetSync.sync
end
You might want to split this into different questions. This makes it easier to help you. As for your first question: I assume you didn't put the env.rb under version control?
Why are your env vars not being picked up by Sinatra? Because you configure Fog in the Rakefile, and Simatra never sees that file. It's used by rake only.
I'd suggest you put the Fog configuration into a third file and require that in both Rakefile and Sinatra app.

Heroku does NOT compile files under assets pipelines in Rails 4

Everything goes well in local machine with assets pipeline in Rails 4 and Ruby 2.0. But when deploying to heroku, it is shown that:
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
I, [2013-03-12T03:28:29.908234 #912] INFO -- : Writing /tmp/build_1n6yi8lwna3sj/public/assets/rails-2ee5a98f26fbf8c6c461127da73c47eb.png
I, [2013-03-12T03:28:29.914096 #912] INFO -- : Writing /tmp/build_1n6yi8lwna3sj/public/assets/trash-3c3c2861eca3747315d712bcfc182902.png
I, [2013-03-12T03:28:33.963234 #912] INFO -- : Writing /tmp/build_1n6yi8lwna3sj/public/assets/application-bf2525bd32aa2a7068dbcfaa591b3874.js
I, [2013-03-12T03:28:40.362850 #912] INFO -- : Writing /tmp/build_1n6yi8lwna3sj/public/assets/application-13374a65f29a3b4cea6f8da2816ce7ff.css
Asset precompilation completed (14.36s)
Heroku seems to compile files but put it in /tmp without any errors. My questions are:
How come Heroku compile assets files to /tmp?
My last solution was to run RAILS_ENV=production bundle exec rake assets:precompile locally, but this generated a manifest-xxxxxx.json in public/assets, rather than manifest.yml, so that heroku doesn't detect the JSON manifest file. I sorted it out by manually created a yml from the json file and heroku became happy. Has heroku's approach been outdated?
Heroku's asset plugins no longer work since Rails 4 does not support plugins. You need to use Heroku's asset gems instead. Place this in your Gemfile:
group :production do
gem 'rails_log_stdout', github: 'heroku/rails_log_stdout'
gem 'rails3_serve_static_assets', github: 'heroku/rails3_serve_static_assets'
end
Follow Heroku's guide on getting started with Rails 4.
Update (07/22/2013): Heroku now supplies a different gem to precompile assets.
group :production do
gem 'rails_12factor'
end
You need to config Rails to serve static assets in production: config/environments/production.rb
SampleApp::Application.configure do
.
.
.
config.serve_static_assets = true
.
.
.
end
UPDATE:
In Rails 4 is deprecated, and has been changed by:
config.serve_static_files = true
Since rails 4 replaced manifest.yml with manifest-(fingerprint).json, you'll want to enable static asset serving.
From Getting Started with Rails 4.x on Heroku:
gem 'rails_12factor', group: :production
then
bundle install
and, finally,
git push heroku
Fixed the issue for me. Hope this helps!
I run exactly into the same problem.
I set config.serve_static_assets = true in my environments/production.rb file until heroku wont't support the new manifest format.
So it is a temporal solution until heroku support will be added.
After hours of googling in which none of the guides on Heroku or the suggestions on StackOverFlow helped me, I finally ran into this blog post which offered this clue:
heroku labs:enable user-env-compile --app=YOUR_APP
Without this, the asset pipeline will always try to init the whole app and connect to the database (despite all the things you may have read that rails 4 now longer does this). This exposes your Heroku configuration to Rails so it can boot up successfully and run rake tasks like assets:precompile.
I needed to use this gem:
gem 'rails_12factor', group: :production #need this for rails 4 assets on heroku
And in /config/environments/production.rb I needed to set:
config.assets.compile = true
My understanding is that the rails_12_factor gem sets config.serve_static_assets = true, amongst other things.
In my case, assets compiled following the instructions above but it wasn't picking the bootstrap glyphicons 'fontawesome-webfont' so this worked for me finally after wasting so many hours researching.
Gem file
gem 'rails_12factor', group: :production
bundle install
config/application.rb
config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif,
"fontawesome-webfont.ttf",
"fontawesome-webfont.eot",
"fontawesome-webfont.svg",
"fontawesome-webfont.woff")
config.assets.precompile << Proc.new do |path|
if path =~ /\.(css|js)\z/
full_path = Rails.application.assets.resolve(path).to_path
app_assets_path = Rails.root.join('app', 'assets').to_path
if full_path.starts_with? app_assets_path
puts "including asset: " + full_path
true
else
puts "excluding asset: " + full_path
false
end
else
false
end
end
environment/production.rb
config.serve_static_assets = true
Then finally, I ran
rake assets:precompile RAILS_ENV=production and pushed it to heroku and that worked.
This was an issue with the Heroku Ruby Buildpack, but an update was deployed today (2013-05-21). Please try it out and let us know.
To answer your questions:
#1) This is sprockets output; things are compiled to /tmp and then moved (See here in Sprockets). To my knowledge this has always been done this way, but it wasn't until Sprockets version was updated in Rails that we got this new debug-type output.
#2) Previously assets:precompile genereated a manifest.json file, but now in Rails 4 the manifest file has a fingerprint in it, which wasn't detected previously. This was fixed with #74.
I added this to the top of one of my css.scss files in the assets/stylesheets/ folder.
#import "font-awesome";
then ran..
rake assets:clean
and...
rake assets:precompile RAILS_ENV=production
In Rails 4.2.4 your production.rb has the line:
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
That means, gem 'rails_12factor', group: :production doesn`t need to change it to true, as it can be set through the heroku environment variables. You also will get a warning if you remove the rails_12factor gem.
If you have problems with assets, login to the heroku console heroku run rails console and find out the asset path for a file puts helper.asset_path("application.js") .
One strange behaviour I noticed between development and production, when the file ending is not provided:
With a image /assets/images/image_01.jpg the following output from asset_pathsdiffers:
Development:
development > puts helper.asset_path('profile_01')
=> /assets/profile_01-bbd16aac5ef1d295411af44c103fcc631ab90ee94957414d4c01c3aed1055714.jpg
development > puts helper.asset_path('profile_01.jpg')
=> /assets/profile_01-bbd16aac5ef1d295411af44c103fcc631ab90ee94957414d4c01c3aed1055714.jpg
Production:
development > puts helper.asset_path('profile_01')
=> /profile_01
development > puts helper.asset_path('profile_01.jpg')
=> /assets/profile_01-bbd16aac5ef1d295411af44c103fcc631ab90ee94957414d4c01c3aed1055714.jpg
You do not have to run RAILS_ENV=production rake assets:precompile, heroku does this for you during deploy. Also you do not have to precompile the assets in development and push them to heroku.
Apart from ensuring you have the 'rails_12factor' gem installed the only thing you need to do is this.
# config/application.rb
config.assets.paths << Rails.root.join('vendor', 'assets')
It seems that although Rails knows exactly what it wants, Heroku needs reminding to include the assets folder as part of the assets paths.
Use Image Extensions
I had this same issue, but for a different reason.
Instead of
<%= asset_path 'facebook-link' %>
Use:
<%= asset_path 'facebook-link.png' %>
While the first one worked locally, when I pushed to Heroku my images were breaking and I had no clue why. Using the full file extension fixed the problem :)
Add this gem gem 'rails_serve_static_assets'
https://github.com/heroku/rails_serve_static_assets
If you are using controller specific assets as in:
<%= javascript_include_tag params[:controller] %> or <%= javascript_include_tag params[:controller] %>
Then in production you will need to explicitly precompile those (in development rails compiles files on the fly).
See official Rails guide here: http://guides.rubyonrails.org/asset_pipeline.html#controller-specific-assets
To precompile as explained in the guides (here: http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets) you will need to add the following to the config/application.rb
# config/application.rb
config.assets.precompile << Proc.new do |path|
if path =~ /\.(css|js)\z/
full_path = Rails.application.assets.resolve(path).to_path
app_assets_path = Rails.root.join('app', 'assets').to_path
if full_path.starts_with? app_assets_path
puts "including asset: " + full_path
true
else
puts "excluding asset: " + full_path
false
end
else
false
end
end
I figure I'll add this as an answer since this question is linked from the Heroku Support page if you search for "assets".
This is mostly for people who are updating their app to Rails 4, but after going through this - and many other SO posts - what finally got me was changing the following in production.rb:
config.action_dispatch.x_sendfile_header = "X-Sendfile"
To:
config.action_dispatch.x_sendfile_header = nil
I hadn't caught this when I upgraded and as usual this took me forever to figure out. Hopefully it helps someone else! Shout out to PatrickEm who asked/answered the same in his question.
This may not answer the original question's root cause, But I was having a similar symptom with a different root cause.
Pre-compilation of a JPEG files changes the file extension to JPG, meaning that asset_path("my_image.jpeg") and asset_path("my_image") didn't work. Remove the "e" from JPEG and voila, it works.
Others have described the same problem here https://blazarblogs.wordpress.com/2016/04/06/rails-force-to-precompile-jpeg-to-jpg/
Is this a bug? Or desired behaviour? And also weird that it only doesn't work in my Heroku-hosted production environment. Maybe they have some sort of configuration.

Resources