ERROR [build 9/9] RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile

I am a student in Japan and just started learning programming.

ruby "3.2.2"
gem "rails", "~> 7.0.4", ">= 7.0.4.3"
gem "omniauth-twitter2"
gem "omniauth-rails_csrf_protection"

I got the following error when I entered “fly deploy”.

Notable Sentences

=> ERROR [build 9/9] RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precom
> [build 9/9] RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile:

rails aborted!
NoMethodError: undefined method `[]' for nil:NilClass
rails/config/initializers/omniauth.rb:3:in `block in <main>'

Error: failed to fetch an image or build from source: error building: executor failed running [/bin/sh -c SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile]: exit code: 1

All sentences

=> ERROR [build 9/9] RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precom  2.0s
------
 > [build 9/9] RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile:
#19 1.985 rails aborted!
#19 1.985 NoMethodError: undefined method `[]' for nil:NilClass
#19 1.985 /rails/config/initializers/omniauth.rb:3:in `block in <main>'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rack-2.2.7/lib/rack/builder.rb:125:in `instance_eval'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rack-2.2.7/lib/rack/builder.rb:125:in `initialize'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.4.3/lib/action_dispatch/middleware/stack.rb:37:in `new'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.4.3/lib/action_dispatch/middleware/stack.rb:37:in `build'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.4.3/lib/action_dispatch/middleware/stack.rb:166:in `block in build'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.4.3/lib/action_dispatch/middleware/stack.rb:162:in `each'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.4.3/lib/action_dispatch/middleware/stack.rb:162:in `inject'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.4.3/lib/action_dispatch/middleware/stack.rb:162:in `build'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.4.3/lib/rails/engine.rb:516:in `block in app'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.4.3/lib/rails/engine.rb:512:in `synchronize'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.4.3/lib/rails/engine.rb:512:in `app'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.4.3/lib/rails/application/finisher.rb:52:in `block in <module:Finisher>'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.4.3/lib/rails/initializable.rb:32:in `instance_exec'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.4.3/lib/rails/initializable.rb:32:in `run'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.4.3/lib/rails/initializable.rb:61:in `block in run_initializers'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.4.3/lib/rails/initializable.rb:60:in `run_initializers'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.4.3/lib/rails/application.rb:372:in `initialize!'
#19 1.985 /rails/config/environment.rb:5:in `<main>'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/zeitwerk-2.6.8/lib/zeitwerk/kernel.rb:38:in `require'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.4.3/lib/rails/application.rb:348:in `require_environment!'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.4.3/lib/rails/application.rb:511:in `block in run_tasks_blocks'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `block in execute'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `each'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `execute'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:188:in `invoke'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/sprockets-rails-3.4.2/lib/sprockets/rails/task.rb:61:in `block (2 levels) in define'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `block in execute'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `each'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `execute'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:243:in `block in invoke_prerequisites'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `each'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `invoke_prerequisites'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:218:in `block in invoke_with_call_chain'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:188:in `invoke'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:160:in `invoke_task'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block (2 levels) in top_level'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `each'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block in top_level'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:125:in `run_with_threads'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:110:in `top_level'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.4.3/lib/rails/commands/rake/rake_command.rb:24:in `block (2 levels) in perform'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.4.3/lib/rails/commands/rake/rake_command.rb:24:in `block in perform'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/rake_module.rb:59:in `with_application'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.4.3/lib/rails/commands/rake/rake_command.rb:18:in `perform'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.4.3/lib/rails/command.rb:51:in `invoke'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/railties-7.0.4.3/lib/rails/commands.rb:18:in `<main>'
#19 1.985 /rails/vendor/bundle/ruby/3.2.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
#19 1.985 Tasks: TOP => environment
#19 1.985 (See full trace by running task with --trace)
------
Error: failed to fetch an image or build from source: error building: executor failed running [/bin/sh -c SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile]: exit code: 1

I read this article but could not solve the problem.

I think there is a problem with this file, how should I fix the code?

config/initializers/omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter2,
          Rails.application.credentials.twitter[:client_id],
          Rails.application.credentials.twitter[:client_secret],
          callback_path: "/auth/twitter2/callback",
          scope: "tweet.read users.read"

  OmniAuth.config.on_failure =
    Proc.new { |env| OmniAuth::FailureEndpoint.new(env).redirect_to_failure }
end

I entered “EDITOR=vi rails credentials:edit” and wrote the secret key as follows

twitter:
  client_id: ********
  client_secret: ****************

Or another file?
https://github.com/furukawaeiichi/CREDO_QUEST/compare/main...feature/login

Could someone please advise me on this?

This problem is described here.

The fix is to surround the above code with:

if Rails.application.credentials.twitter
...
end

What’s happening here is that both assets:precompile and your deployed application load your configuration, but only your deployed application has access to, and needs, your twitter credentials.

1 Like

Dear rubys
Thank you for the article and advice!
It was very easy to understand.

I have successfully deployed it.
Thank you very much for your kindness!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.