RAILS_MASTER_KEY error on Heroku deployment - ruby-on-rails

I am getting the following error while deploying my rails app on Heroku
-----> Installing node-v8.10.0-linux-x64
-----> Detecting rake tasks
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
Rails::Secrets::MissingKeyError: Missing encryption key to decrypt secrets with. Ask your team for your master key and put it in ENV["RAILS_MASTER_KEY"]
Now if I set environment variable RAILS_MASTER_KEY with the value generated from rake secret . I get this error
-----> Installing node-v8.10.0-linux-x64
-----> Detecting rake tasks
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
ActiveSupport::MessageEncryptor::InvalidMessage: ActiveSupport::MessageEncryptor::InvalidMessage
There is a solution on git regarding this.
https://gist.github.com/kaspth/bc37989c2f39a5642112f28b1d93f343
If I implement the method mentioned in the above github issue. I get the the following error that is also regarding the RAILS_MASTER_KEY.
Generating a new higher entropy encryption key in config/secrets.yml.key.
Rotate the encryption key now.
C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/railties-5.1.6/lib/rails/secrets.rb:77:in `handle_missing_key': Missing encryption key to decrypt secrets with. Ask your team for your master key and put it in ENV["RAILS_MASTER_KEY"]
(Rails::Secrets::MissingKeyError)

RAILS_MASTER_KEY should contain the same value that is in the file config/secrets.yml.key in your project on your computer. You only need to copy it from the file and paste it in Heroku settings. You do not need to generate new secret.

Related

Ruby on Rails 6 - Credentials preventing rake tasks from loading on Heroku

I'm trying to upgrade an app from Rails 6 to Rails 6.1.6.1 and I'm running into the following error using credentials. My production credentials file contains the following:
aws:
bucket_prefix: "some-prefix"
But when I try to deploy to Heroku, I'm getting the following:
-----> Detecting rake tasks
!
! Could not detect rake tasks
! ensure you can run `$ bundle exec rake -P` against your app
! and using the production group of your Gemfile.
! rake aborted!
! NoMethodError: undefined method `[]' for nil:NilClass
!
! config.dynamic_assets.bucket_prefix = Rails.application.credentials.aws[:bucket_prefix]
! ^^^^^^^^^^^^^^^^
The app starts up just fine with RAILS_ENV=production rails s locally.
Would someone please point me as to what I might be doing wrong?
This error is telling you that Rails.application.credentials.aws is returning nil.
What this usually means is that Heroku doesn't have your RAILS_MASTER_KEY, so it can't access your credentials.yml.enc file.
Using the Heroku dashboard or the CLI, you need to set an environment variable called RAILS_MASTER_KEY to match the key in your master.key file.
For a great walkthrough, check out this SO answer.
If you have set this environment variable, check to make sure you haven't added credentials.yml.enc to your .gitignore list.
If Heroku doesn't have your credentials.yml.enc file, then it can't access the credentials inside it, even with the master key.

Heroku Error `secret_key_base` for production environment must be a type of String`

When I put $git push heroku master, I got an argument error:
remote: -----> Preparing app for Rails asset pipeline
remote: Running: rake assets:precompile
remote: ArgumentError: secret_key_base for production environment must be a type of String
remote: /tmp/build_19076210a01025325310b08d032af424/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2/lib/rails/application.rb:583:invalidate_secret_key_base'
remote: /tmp/build_19076210a01025325310b08d032af424/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2/lib/rails/application.rb:432:in secret_key_base'
remote: /tmp/build_19076210a01025325310b08d032af424/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2/lib/rails/application.rb:176:inkey_generator'
remote: /tmp/build_19076210a01025325310b08d032af424/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2/lib/rails/application.rb:205:in `message_verifier'
.......................
My Rails version is 5.2 and which has new encrypt system filed in config/credentials.yml.enc. I guess this configuration makes this heroku error.
No one has this question and describe how to solve this error.
Could you please help me how to solve this error? Thanks.
https://waiyanyoon.com/deploying-rails-5-2-applications-with-encrypted-credentials-using-capistrano/
also before pushing to heroku try to compile assets in PRODUCTION mode locally to see if everything works.

Error: Sprockets::FileNotFound: couldn't find file 'application.scss' for Heroku

I'm having some issues with Sprockets not finding a file during rake assets:precompile. I haven't had this issue before, but when I added a generated migration (which built locally), Heroku failed to build my app, and I don't know why, because it builds locally (without me running an additional rake assets:precompile).
Error message is this:
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
Sprockets::FileNotFound: could not find file: /tmp/build_dd4a799769e6963b7b292df72db58fd8/username-appname-uniqueid/app/assets/stylesheets/application.scss
Any help with why this is? Thank you for your time!
PS. As a side note - when I run "rake assets:precompile" and then push to Heroku, the app builds. However, an item with the css class "fa-bars" gets messed up... why is this?
Thanks!
I add the same issue with Sprockets 3.3.3 and our Rails 4.2.3 app.
I fixed it by cleaning our dyno build cache with:
heroku repo:purge_cache
Please note that your next deployment will take longer as bundler cache will be cleared too.
In rails 5 if you use Bower, uncomment in bower_rails.rb
#bower_rails.install_before_precompile = true

Deploying Rails 4 with ricn/rika gem on Heroku - precompile error

I'm running a Rails application using the Rika gem (JRuby wrapper for Apache Tika). It runs fine locally. In trying to push it to Heroku, it gets a ways into the deployment until I hit the asset precompile:
...
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
No such file to load -- rika
followed by a long string of /tmp/build_8a51... errors.
Precompile works fine on the local machine.
If I try to install the gem via 'heroku run' I get:
$ heroku run gem install rika
Running gem install rika attached to terminal... up, run.8601
ERROR: While executing gem ... (ArgumentError) malformed format string - %)
Any suggestions?
Thanks
Found the problem.
I needed to add the ruby version spec line to the Gemfile. It wasn't necessary on the local machine because I set the version externally. Heroku needs it though.

Heroku Running Wrong ENV Configuration

I have tried to create a QA environment for my app on Heroku. When I deploy, the output on the console seems to suggest that the production.rb is still being run.
I created a new environment file at /config/environments/qa.rb. This is an exact copy of the production.rb file except I added a log statement at the top.
qa.rb:
MyApp::Application.configure do
puts "Loading QA Config"
production.rb:
MyApp::Application.configure do
puts "Loading Production Config"
Next, I ran the Heroku command to update BOTH env settings:
heroku config:set RACK_ENV=qa RAILS_ENV=qa --app qa-myapp
To double check, I print the config settings:
heroku config --app qa-myapp
=== qa-myapp Config Vars
...
RACK_ENV: qa
RAILS_ENV: qa
...
To triple check, I run the console! (notice the QA lines as well as the Rails.env):
heroku run console --app qa-myapp
Running `console` attached to terminal... up, run.8199
Loading QA Config
Loading qa environment (Rails 4.0.0)
irb(main):001:0> Rails.env
=> "qa"
It all seems to be done correctly. When I push to Heroku though, it doesn't load the correct config (I added the arrow from the right to point to the log statement):
git push qa master
...
-----> Ruby app detected
-----> Compiling Ruby/Rails
-----> Using Ruby version: ruby-1.9.3
...
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
Asset precompilation completed (8.34s)
Cleaning assets
Loading Production Config <-----------------------------------------
-----> Discovering process types
Procfile declares types -> (none)
Default types for Ruby -> console, rake, web, worker
-----> Compiled slug size: 30.6MB
-----> Launching... done, v27
http://qa-myapp.herokuapp.com deployed to Heroku
What am I missing here? I didn't think there was anything else to do in order to get this working. Do I need to do something different here?
Check out the "Flow" section of the Heroku Buildpack readme. It sets RAILS_ENV=production on deploy for any Rails 2 / Rails 3 app.
Their documentation shows examples of how you can hack your own buildpack to change this, but if everything is working the way it should in console and through your tests, that may not be necessary (but still an option).

Resources