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

I’ve been trying to deploy some changes on my rails app and get the following error:

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

I’ve integrated stripe to my app and deleted credentials I had on ENV file and added them again using EDITOR=vim rails credentials:edit.

After running fly deploy I get the following:

 > [build 9/9] RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile:
#19 0.947 /usr/local/lib/ruby/2.7.0/net/protocol.rb:66: warning: already initialized constant Net::ProtocRetryError
#19 0.947 /rails/vendor/bundle/ruby/2.7.0/gems/net-protocol-0.2.1/lib/net/protocol.rb:68: warning: previous definition of ProtocRetryError was here
#19 0.948 /usr/local/lib/ruby/2.7.0/net/protocol.rb:206: warning: already initialized constant Net::BufferedIO::BUFSIZE
#19 0.948 /rails/vendor/bundle/ruby/2.7.0/gems/net-protocol-0.2.1/lib/net/protocol.rb:214: warning: previous definition of BUFSIZE was here
#19 0.948 /usr/local/lib/ruby/2.7.0/net/protocol.rb:503: warning: already initialized constant Net::NetPrivate::Socket
#19 0.948 /rails/vendor/bundle/ruby/2.7.0/gems/net-protocol-0.2.1/lib/net/protocol.rb:541: warning: previous definition of Socket was here
#19 1.108 rails aborted!
#19 1.109 NoMethodError: undefined method `[]' for nil:NilClass
#19 1.109 /rails/config/initializers/stripe.rb:3:in `<main>'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.7.2/lib/active_support/dependencies.rb:326:in `load'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.7.2/lib/active_support/dependencies.rb:326:in `block in load'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.7.2/lib/active_support/dependencies.rb:299:in `load_dependency'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.7.2/lib/active_support/dependencies.rb:326:in `load'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/engine.rb:681:in `block in load_config_initializer'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.7.2/lib/active_support/notifications.rb:205:in `instrument'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/engine.rb:680:in `load_config_initializer'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/engine.rb:634:in `block (2 levels) in <class:Engine>'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/engine.rb:633:in `each'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/engine.rb:633:in `block in <class:Engine>'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/initializable.rb:32:in `instance_exec'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/initializable.rb:32:in `run'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/initializable.rb:61:in `block in run_initializers'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/initializable.rb:50:in `each'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/initializable.rb:50:in `tsort_each_child'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/initializable.rb:60:in `run_initializers'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/application.rb:391:in `initialize!'
#19 1.109 /rails/config/environment.rb:5:in `<main>'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/zeitwerk-2.6.7/lib/zeitwerk/kernel.rb:38:in `require'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.7.2/lib/active_support/dependencies.rb:332:in `block in require'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.7.2/lib/active_support/dependencies.rb:299:in `load_dependency'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.7.2/lib/active_support/dependencies.rb:332:in `require'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/application.rb:367:in `require_environment!'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/application.rb:533:in `block in run_tasks_blocks'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `block in execute'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `each'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `execute'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:188:in `invoke'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/sprockets-rails-3.4.2/lib/sprockets/rails/task.rb:61:in `block (2 levels) in define'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `block in execute'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `each'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `execute'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:243:in `block in invoke_prerequisites'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `each'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `invoke_prerequisites'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:218:in `block in invoke_with_call_chain'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:188:in `invoke'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:160:in `invoke_task'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block (2 levels) in top_level'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `each'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block in top_level'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:125:in `run_with_threads'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:110:in `top_level'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/commands/rake/rake_command.rb:24:in `block (2 levels) in perform'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/commands/rake/rake_command.rb:24:in `block in perform'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/rake_module.rb:59:in `with_application'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/commands/rake/rake_command.rb:18:in `perform'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/command.rb:50:in `invoke'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/commands.rb:18:in `<main>'
#19 1.109 /rails/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
#19 1.109 Tasks: TOP => environment
#19 1.109 (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

My Dockerfile looks as follows:Preformatted text

# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=2.7.4
FROM ruby:$RUBY_VERSION-slim as base

LABEL fly_launch_runtime="rails"

# Rails app lives here
WORKDIR /rails

# Set production environment
ENV RAILS_ENV="production" \
    BUNDLE_WITHOUT="development:test" \
    BUNDLE_DEPLOYMENT="1"

# Update gems and bundler
RUN gem update --system --no-document && \
    gem install -N bundler


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build gems and node modules
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y build-essential curl libpq-dev node-gyp pkg-config python-is-python3

# Install JavaScript dependencies
ARG NODE_VERSION=14.15.5
ARG YARN_VERSION=1.22.19
ENV PATH=/usr/local/node/bin:$PATH
RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
    /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
    npm install -g yarn@$YARN_VERSION && \
    rm -rf /tmp/node-build-master

# Install application gems
COPY Gemfile Gemfile.lock .
RUN bundle install && \
    bundle exec bootsnap precompile --gemfile

# Install node modules
COPY package.json yarn.lock .
RUN yarn install --frozen-lockfile

# Copy application code
COPY . .

# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/

# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile


# Final stage for app image
FROM base

# Install packages needed for deployment
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y postgresql-client && \
    rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Run and own the application files as a non-root user for security
RUN useradd rails
USER rails:rails

# Copy built artifacts: gems, application
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build --chown=rails:rails /rails /rails

# Deployment options
ENV RAILS_LOG_TO_STDOUT="1" \
    RAILS_SERVE_STATIC_FILES="true"

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]

At build time, your credentials will be nil. Your credentials will be available at deploy time.

Assuming that you don’t need stripe at build time to build your assets (HTML, CSS, JS), all you need to do is add an if statement to your initializer to check if your credential is nil before proceeding.

If you need further help, post the source to this initializer.

config/initializers/stripe.rb

require 'stripe'

Stripe.api_key = Rails.application.credentials[:stripe][:secret]

Try:

require 'stripe'

if Rails.application.credentials[:stripe]
  Stripe.api_key = Rails.application.credentials[:stripe][:secret]
end

It solved the problem regarding stripe, but now I’m getting this error:

Failure #1

Instance
ID              PROCESS VERSION REGION  DESIRED STATUS  HEALTH CHECKS   RESTARTS        CREATED
ca904090        app     1       lax     run     pending 1 total         0               38s ago

Recent Events
TIMESTAMP               TYPE            MESSAGE
2023-03-22T18:48:57Z    Received        Task received by client
2023-03-22T18:48:57Z    Task Setup      Building Task Directory

2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/initializable.rb:60:in `run_initializers'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/application.rb:391:in `initialize!'
2023-03-22T18:49:46Z   [info]   from /rails/config/environment.rb:5:in `<main>'
2023-03-22T18:49:46Z   [info]   from config.ru:3:in `block in <main>'
2023-03-22T18:49:46Z   [info]   from config.ru:3:in `block in <main>'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/rack-2.2.6.2/lib/rack/builder.rb:116:in `new_f2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/rack-2.2.6.2/lib/rack/builder.rb:116:in `new_fom_string'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/rack-2.2.6.2/lib/rack/builder.rb:105:in `load_file'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/rack-2.2.6.2/lib/rack/builder.rb:66:in `parse_file'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/rack-2.2.6.2/lib/rack/server.rb:349:in `build_app_and_options_from_config'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/rack-2.2.6.2/lib/rack/server.rb:249:in `app'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/rack-2.2.6.2/lib/rack/server.rb:422:in `wrapped_app'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/rack-2.2.6.2/lib/rack/server.rb:312:in `block in start'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/rack-2.2.6.2/lib/rack/server.rb:379:in `handle_profiling'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/rack-2.2.6.2/lib/rack/server.rb:311:in `start'2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/commands/server/server_command.rb:39:in `start'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/commands/server/server_command.rb:144:in `block in perform'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/commands/server/server_command.rb:135:in `tap'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/commands/server/server_command.rb:135:in `perform'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/command/base.rb:69:in `perform'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/command.rb:48:in `invoke'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/railties-6.1.7.2/lib/rails/commands.rb:18:in `<main>'
2023-03-22T18:49:46Z   [info]   from /usr/local/lib/ruby/site_ruby/2.7.0/rubygems/core_ext/kernel_require.rb:37:in `require'
2023-03-22T18:49:46Z   [info]   from /usr/local/lib/ruby/site_ruby/2.7.0/rubygems/core_ext/kernel_require.rb:37:in `require'
2023-03-22T18:49:46Z   [info]   from /rails/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
2023-03-22T18:49:46Z   [info]   from /rails/bin/rails:5:in `<main>'
2023-03-22T18:49:47Z   [info]Starting clean up.
--> v1 failed - Failed due to unhealthy allocations - rolling back to job version 0 and deploying as v2

--> Troubleshooting guide at https://fly.io/docs/getting-started/troubleshooting/
Error abort

You are getting an error in an initializer, but you would need to see more of the stack trace to debug it. Try looking at your dashboard to see if more information is there, otherwise run fly logs. See Existing Rails Apps · Fly Docs for more information.

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