ERROR [build 6/6] RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile

Hey guys, I am relatively new to building full stack apps, and this is really my first time deploying a proper full stack app. While trying to do so, I got the following error (stated in title of post) when I entered “fly deploy”, with the following message:

 => ERROR [build 6/6] RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile                                                                                                                                2.4s
------                                                                                                                                                                                                              
 > [build 6/6] RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile:
2.323 Error retrieving instance profile credentials: Failed to open TCP connection to 169.254.169.254:80 (Network is unreachable - connect(2) for "169.254.169.254" port 80)
2.328 bin/rails aborted!
2.329 Aws::Errors::MissingRegionError: No region was provided. Configure the `:region` option or export the region name to ENV['AWS_REGION'] (Aws::Errors::MissingRegionError)
2.329 
2.329         raise Errors::MissingRegionError if region.nil? || region == ''
2.329               ^^^^^^^^^^^^^^^^^^^^^^^^^^
2.329 /rails/app/models/user.rb:8:in `<class:User>'
2.329 /rails/app/models/user.rb:3:in `<main>'
2.329 /rails/config/routes.rb:2:in `block in <main>'
2.329 /rails/config/routes.rb:1:in `<main>'
2.329 /rails/config/environment.rb:5:in `<main>'
2.329 Tasks: TOP => environment
2.329 (See full trace by running task with --trace)
------
Error: failed to fetch an image or build from source: error building: failed to solve: process "/bin/sh -c SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile" did not complete successfully: exit code: 1
hunyao@macbookpro odinbook % 

I am running two different things within my program which could be potential problems, though I’m not sure how to identify which might be causing them, and how to solve it:

  1. Using Omniauth for google. I have already implemented as per one of the article by doing this:
if Rails.application.credentials.google
   config.omniauth :google_oauth2, Rails.application.credentials.dig(:google, :google_client_id), Rails.application.credentials.dig(:google, :google_client_secret), {}
  end

Within “EDITOR=vi rails credentials:edit” is as follows:

google:
  google_client_id: *********
  google_client_secret: ************
  1. I am using ActiveStorage to store user profile pictures (stores from either google profile picture via API, or through uploading their own profile picture)
    → The potential problem here is that I am using local disk for storage currently, although fly.io has automatically generated tigris for me, which is also causing this error message regarding aws
2.329 Aws::Errors::MissingRegionError: No region was provided. Configure the `:region` option or export the region name to ENV['AWS_REGION'] (Aws::Errors::MissingRegionError)
2.329 
2.329         raise Errors::MissingRegionError if region.nil? || region == ''
2.329               ^^^^^^^^^^^^^^^^^^^^^^^^^^

Here’s the code within my storage.yml:

test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

local:
  service: Disk
  root: <%= Rails.root.join("storage") %>

# Use bin/rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
# amazon:
#   service: S3
#   access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
#   secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
#   region: us-east-1
#   bucket: your_own_bucket-<%= Rails.env %>

# Remember not to checkin your GCS keyfile to a repository
# google:
#   service: GCS
#   project: your_project
#   credentials: <%= Rails.root.join("path/to/gcs.keyfile") %>
#   bucket: your_own_bucket-<%= Rails.env %>

# Use bin/rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key)
# microsoft:
#   service: AzureStorage
#   storage_account_name: your_account_name
#   storage_access_key: <%= Rails.application.credentials.dig(:azure_storage, :storage_access_key) %>
#   container: your_container_name-<%= Rails.env %>

# mirror:
#   service: Mirror
#   primary: local
#   mirrors: [ amazon, google, microsoft ]

tigris:
  service: S3
  access_key_id: <%= ENV["AWS_ACCESS_KEY_ID"] %>
  secret_access_key: <%= ENV["AWS_SECRET_ACCESS_KEY"] %>
  endpoint: <%= ENV["AWS_ENDPOINT_URL_S3"] %>
  bucket: <%= ENV["BUCKET_NAME"] %>

I have also tried running bin/rails generate dockerfile --tigris but to no avail:

    conflict  Dockerfile
Overwrite /Users/hunyao/repos/odinbook/Dockerfile? (enter "h" for help) [Ynaqdhm] y
       force  Dockerfile
    conflict  .dockerignore
Overwrite /Users/hunyao/repos/odinbook/.dockerignore? (enter "h" for help) [Ynaqdhm] y
       force  .dockerignore
    conflict  bin/docker-entrypoint
Overwrite /Users/hunyao/repos/odinbook/bin/docker-entrypoint? (enter "h" for help) [Ynaqdhm] y
       force  bin/docker-entrypoint
   identical  fly.toml
   identical  config/dockerfile.yml
   unchanged  config/storage.yml
   unchanged  config/environments/production.yml

Could someone please advise me on this? Thank you.

Is this in your code (app/models/user.rb:8)? This doesn’t seem to be something you would do if you were intending to use local storage, nor is it something we would have added. The problem here is that secrets are not available at build time, so region will be nil.

Tigris is recommended for ActiveStorage, but ultimately the choice is yours. If you want to use local storage in production, that storage would need to be on a volume, can only be accessed by one machine, and it would be your responsibility to back this up.

Thanks for your reply, rubys. A few follow-ups:

Tigris was appended into my storage.yml file by fly.io automatically, Which I believe is also returning the error MissingRegionError. How do I go about to resolve this? At the moment, my storage.yml contains:

test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

local:
  service: Disk
  root: <%= Rails.root.join("storage") %>

tigris:
  service: S3
  access_key_id: <%= ENV["AWS_ACCESS_KEY_ID"] %>
  secret_access_key: <%= ENV["AWS_SECRET_ACCESS_KEY"] %>
  endpoint: <%= ENV["AWS_ENDPOINT_URL_S3"] %>
  bucket: <%= ENV["BUCKET_NAME"] %>

In app/models/user.rb:8 contains this line has_one_attached :profile_picture which essentially points to an ActiveStorage record.

I haven’t seen that error before, so I’m just guessing here.

Try running fly secrets list. If AWS_REGION is present there, add this to the tigris section of config/secrets.yml:

  region: <%= ENV["AWS_REGION"] %>

If not, try:

  region: auto

Hi rubys, did you mean add to storage.yml as I don’t have secrets.yml file in my directory.

Anyway, I have run fly secrets list and there is AWS_REGION there, like that: AWS_REGION 274e16452b90854d Nov 4 2024 07:20

I then proceeded to add in both options you have stated (auto and ENV) into the storage.yml file and still doesnt work.

The ENV option gave me the same error as originally stated above - e.g. MissingRegionError;
whereas the region: auto gave me a new error:

=> ERROR [build 6/6] RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:prec  2.2s
------                                                                          
 > [build 6/6] RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile:
2.110 Error retrieving instance profile credentials: Failed to open TCP connection to 169.254.169.254:80 (Network is unreachable - connect(2) for "169.254.169.254" port 80)
2.176 bin/rails aborted!
2.181 ArgumentError: missing required option :name (ArgumentError)
2.181 
2.181       when nil then raise ArgumentError, "missing required option :name"
2.181                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2.181 /rails/app/models/user.rb:8:in `<class:User>'
2.181 /rails/app/models/user.rb:3:in `<main>'
2.181 /rails/config/routes.rb:2:in `block in <main>'
2.181 /rails/config/routes.rb:1:in `<main>'
2.181 /rails/config/environment.rb:5:in `<main>'
2.181 Tasks: TOP => environment
2.181 (See full trace by running task with --trace)

How should I proceed?

What are in those two lines of code?

What appears to be happening is that you are trying to create a new User object? That wouldn’t work during the build. If you can track down why that is occurring, perhaps adding if Rails.application.credentials.google around that line would enable the build to succeed.

Attaching the screenshots here for your reference on the snippets of code related to those lines, also I have already included if Rails.application.credentials.google earlier but still had that error.

Looks like a problem with devise that they know about but won’t fix:

The only workaround is to find something that is set when run in production but isn’t set at build time and skip the call to devise in your config/routes during build time. I’m suggesting that Rails.application.credentials.google might be something you could check. So update your config/routes.rb to contain:

if Rails.application.credentials.google
  devise ...
end

Thanks, using the below solution I found from the link solved my problem.

unless defined?(::Rake::SprocketsTask)
  #...
end

Although, when I got to the website, the website breaks when I try to sign up, I suspect its a google authentication problem…

Here’s the link: https://odinbook-sparkling-water-3341.fly.dev/users/sign_in

The broken image in that page is supposedly the google sign up button, but it’s not showing here.

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