1

I'm trying to evaluate Fly.io and deploy an existing Rails 7 app. Following their guide for existing rails apps, fly launch works fine, however fly deploy fails on the assets:precompile step.

Console Ouput:

 => [build 5/6] RUN bundle exec bootsnap precompile app/ lib/                                                                                                                                      1.1s 
 => ERROR [build 6/6] RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile                                                                                                                    2.1s
------
 > [build 6/6] RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile:
#16 2.043 Missing encryption key to decrypt file with. Ask your team for your master key and write it to /rails/config/master.key or put it in the ENV['RAILS_MASTER_KEY'].
------
Error failed to fetch an image or build from source: error building: executor failed running [/bin/sh -c SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile]: exit code: 1

Obviously, this is a common enough problem that it’s mentioned in Fly's Getting Started Guide

I have my Rails master key in the correct config file (config/master.key), and the error output even links to the file. Fly launch successfully created the RAILS_MASTER_KEY env variable and I’ve confirmed that the key is correct. My secret_key_base is in the rails credentials file.

I've tried setting the Dockerfile to both of these:

RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile

Both result in the same error

There's also this issue on github, but the solution to that seems to be the dummy secret key base.

Any suggestions for what to try in order to resolve this?

Phil-6
  • 552
  • 6
  • 16
  • 1
    The error appears to be related to the `sassc-rails` gem. Deploys work with `cssbundling-rails` but fail with `sassc-rails` and `dartsass-rails` – Phil-6 Apr 19 '23 at 06:31
  • The `SECRET_KEY_BASE_DUMMY` method will be available once rails 7.1 is out. – Silex May 01 '23 at 14:28
  • 1
    @Phil-6 I've had this issue for weeks, and I don't have sass-rails in my gemfile, and I ended up (for now) just turning off require master key. It's frustrating. – courtsimas Jul 13 '23 at 02:59

1 Answers1

3

I have now managed to get this to work although it's not an ideal solution.

I've updating the Dockerfile to manually add an ARG and ENV var for the RAILS_MASTER_KEY and then passing the key value as a build arg.

Dockerfile

ARG RAILS_MASTER_KEY
# Set production environment
ENV RAILS_ENV="production" \
    BUNDLE_WITHOUT="development:test" \
    BUNDLE_DEPLOYMENT="1" \
    RAILS_MASTER_KEY=${RAILS_MASTER_KEY}

Testing locally if you have docker installed:

sudo docker compose build --build-arg RAILS_MASTER_KEY=$(cat config/master.key)         

Deploying to Fly.io:

fly deploy --build-arg RAILS_MASTER_KEY=$(cat config/master.key) 
Phil-6
  • 552
  • 6
  • 16
  • 1
    you should select this as an correct asnwer because fly io solution also passing hte master key as build arg in fly deploy – buncis May 07 '23 at 13:24