Skip to content

Commit a3e392f

Browse files
authored
Allow assets:precompile to be run in a production build step without passing in RAILS_MASTER_KEY (rails#46760)
* Add ENV["SECRET_KEY_BASE_DUMMY"] This is useful when precompiling assets for production as part of a build step that otherwise does not need access to the production secrets. * Test SECRET_KEY_BASE_DUMMY
1 parent 0100102 commit a3e392f

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

railties/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
* Add ENV["SECRET_KEY_BASE_DUMMY"] for starting production environment with a generated secret base key,
2+
which can be used to run tasks like `assets:precompile` without making the RAILS_MASTER_KEY available
3+
to the build process.
4+
5+
Dockerfile layer example:
6+
7+
```
8+
RUN SECRET_KEY_BASE_DUMMY=1 bundle exec rails assets:precompile
9+
```
10+
11+
*DHH*
12+
113
* Show descriptions for all commands in Rails help
214
315
When calling `rails help` most commands missed their description. We now

railties/lib/rails/application.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,17 @@ def secrets
461461
# In development and test, this is randomly generated and stored in a
462462
# temporary file in <tt>tmp/development_secret.txt</tt>.
463463
#
464+
# You can also set <tt>ENV["SECRET_KEY_BASE_DUMMY"]</tt> to trigger the use of a randomly generated
465+
# secret_key_base that's stored in a temporary file. This is useful when precompiling assets for
466+
# production as part of a build step that otherwise does not need access to the production secrets.
467+
#
468+
# Dockerfile example: <tt>RUN SECRET_KEY_BASE_DUMMY=1 bundle exec rails assets:precompile</tt>.
469+
#
464470
# In all other environments, we look for it first in <tt>ENV["SECRET_KEY_BASE"]</tt>,
465471
# then +credentials.secret_key_base+, and finally +secrets.secret_key_base+. For most applications,
466472
# the correct place to store it is in the encrypted credentials file.
467473
def secret_key_base
468-
if Rails.env.development? || Rails.env.test?
474+
if Rails.env.development? || Rails.env.test? || ENV["SECRET_KEY_BASE_DUMMY"]
469475
secrets.secret_key_base ||= generate_development_secret
470476
else
471477
validate_secret_key_base(

railties/test/application/configuration_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,20 @@ def index
762762
assert_match(/Missing `secret_key_base`./, error.message)
763763
end
764764

765+
test "dont raise in production when dummy secret_key_base is used" do
766+
ENV["SECRET_KEY_BASE_DUMMY"] = "1"
767+
768+
app_file "config/initializers/secret_token.rb", <<-RUBY
769+
Rails.application.credentials.secret_key_base = nil
770+
RUBY
771+
772+
assert_nothing_raised do
773+
app "production"
774+
end
775+
ensure
776+
ENV["SECRET_KEY_BASE_DUMMY"] = nil
777+
end
778+
765779
test "raise when secret_key_base is not a type of string" do
766780
add_to_config <<-RUBY
767781
Rails.application.credentials.secret_key_base = 123

0 commit comments

Comments
 (0)