secret_key_base` for ‘production’ environmentが発生したので環境を確認

Railsの環境

[admin@postgresql weather]$ rbenv -v
rbenv 1.1.2-30-gc879cb0
[admin@postgresql weather]$ 

[admin@postgresql weather]$ ruby --version
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]
[admin@postgresql weather]$ rails --version
Rails 6.0.3.2
[admin@postgresql weather]$ 

ProductionへのMigrateionが失敗


[admin@postgresql weather]$ bundle exec rails db:migrate RAILS_ENV=test
== 20200612225551 CreatePrefectures: migrating ================================
-- create_table(:prefectures)
   -> 0.0827s
== 20200612225551 CreatePrefectures: migrated (0.0829s) =======================

== 20200612231434 CreateWeatherForecasts: migrating ===========================
-- create_table(:weather_forecasts)
   -> 0.0807s
== 20200612231434 CreateWeatherForecasts: migrated (0.0810s) ==================

[admin@postgresql weather]$ bundle exec rails db:migrate RAILS_ENV=production
rails aborted!
ArgumentError: Missing `secret_key_base` for 'production' environment, set this string with `rails credentials:edit`
/home/admin/app/ruby/weather/config/environment.rb:5:in `<main>'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate => db:load_config => environment
(See full trace by running task with --trace)
[admin@postgresql weather]$ cat /home/admin/app/ruby/weather/config/environment.rb
# Load the Rails application.
require_relative 'application'
# Initialize the Rails application.
Rails.application.initialize!
[admin@postgresql weather]$

マスターキーはgit対象外(≒複合鍵が本番にも必要)

必要に応じてmaster.keyを本番環境にコピーするか、RAILS_MASTER_KEY環境変数に設定。

[admin@postgresql weather]$ cat .gitignore | grep -i key
# Ignore master key for decrypting credentials and more.
/config/master.key
[admin@postgresql weather]$ 

オプション (もし鍵をコピーしても上手くいかない場合)
上記コマンドエラーで、rails credentials:editを実行しろとあるが、こちらを実行する場合は先ずは既存の鍵が環境変数等で利用されてない事を確認した方が無難。
既存のcredentials.ymlは削除して新規で認証鍵ペアを作成。

[admin@postgresql weather]$ rm config/credentials.yml.enc 
[admin@postgresql weather]$ EDITOR="vim" bin/rails credentials:edit

マスターキーの設定をproduction.rbでONに設定


[admin@postgresql environments]$ cat production.rb | grep master
  # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"]
  # or in config/master.key. This key is used to decrypt credentials (and other encrypted files).
  # config.require_master_key = true
  config.require_master_key = true
[admin@postgresql environments]$ 

Migrationの再実行


[admin@postgresql weather]$ bundle exec rails db:migrate RAILS_ENV=production 
== 20200612225551 CreatePrefectures: migrating ================================
-- create_table(:prefectures)
   -> 0.0641s
== 20200612225551 CreatePrefectures: migrated (0.0643s) =======================

== 20200612231434 CreateWeatherForecasts: migrating ===========================
-- create_table(:weather_forecasts)
   -> 0.1899s
== 20200612231434 CreateWeatherForecasts: migrated (0.1904s) ==================

[admin@postgresql weather]$ 

スキーマにオブジェクトが作成されいる事を確認

スキーマが出来たのでデータを投入して完了


[admin@postgresql weather]$ bundle exec rails db:seed_fu RAILS_ENV=production

== Seed from /home/admin/app/ruby/weather/db/fixtures/prefecture.rb
 - Prefecture {:id=>1, :name=>"北海道", :ename=>"hokkaido"}
 - Prefecture {:id=>2, :name=>"青森県", :ename=>"aomori"}
 - Prefecture {:id=>3, :name=>"岩手県", :ename=>"iwate"}
 - Prefecture {:id=>4, :name=>"宮城県", :ename=>"miyagi"}
 - Prefecture {:id=>5, :name=>"秋田県", :ename=>"akita"}
 - Prefecture {:id=>6, :name=>"山形県", :ename=>"yamagata"}
 - Prefecture {:id=>7, :name=>"福島県", :ename=>"fukushima"}
 - Prefecture {:id=>8, :name=>"茨城県", :ename=>"ibaraki"}
 - Prefecture {:id=>9, :name=>"栃木県", :ename=>"tochigi"}
 - Prefecture {:id=>10, :name=>"群馬県", :ename=>"gunma"}
 - Prefecture {:id=>11, :name=>"埼玉県", :ename=>"saitama"}
 - Prefecture {:id=>12, :name=>"千葉県", :ename=>"chiba"}
 - Prefecture {:id=>13, :name=>"東京都", :ename=>"tokyo"}
 - Prefecture {:id=>14, :name=>"神奈川県", :ename=>"kanagawa"}
 - Prefecture {:id=>15, :name=>"新潟県", :ename=>"niigata"}
 - Prefecture {:id=>16, :name=>"富山県", :ename=>"toyama"}
 - Prefecture {:id=>17, :name=>"石川県", :ename=>"ishikawa"}
 - Prefecture {:id=>18, :name=>"福井県", :ename=>"fukui"}
 - Prefecture {:id=>19, :name=>"山梨県", :ename=>"yamanashi"}
 - Prefecture {:id=>20, :name=>"長野県", :ename=>"nagano"}
 - Prefecture {:id=>21, :name=>"岐阜県", :ename=>"gifu"}
 - Prefecture {:id=>22, :name=>"静岡県", :ename=>"shizuoka"}
 - Prefecture {:id=>23, :name=>"愛知県", :ename=>"aichi"}
 - Prefecture {:id=>24, :name=>"三重県", :ename=>"mie"}
 - Prefecture {:id=>25, :name=>"滋賀県", :ename=>"shiga"}
 - Prefecture {:id=>26, :name=>"京都府", :ename=>"kyoto"}
 - Prefecture {:id=>27, :name=>"大阪府", :ename=>"osaka"}
 - Prefecture {:id=>28, :name=>"兵庫県", :ename=>"hyogo"}
 - Prefecture {:id=>29, :name=>"奈良県", :ename=>"nara"}
 - Prefecture {:id=>30, :name=>"和歌山県", :ename=>"wakayama"}
 - Prefecture {:id=>31, :name=>"鳥取県", :ename=>"tottori"}
 - Prefecture {:id=>32, :name=>"島根県", :ename=>"shimane"}
 - Prefecture {:id=>33, :name=>"岡山県", :ename=>"okayama"}
 - Prefecture {:id=>34, :name=>"広島県", :ename=>"hiroshima"}
 - Prefecture {:id=>35, :name=>"山口県", :ename=>"yamaguchi"}
 - Prefecture {:id=>36, :name=>"徳島県", :ename=>"tokushima"}
 - Prefecture {:id=>37, :name=>"香川県", :ename=>"kagawa"}
 - Prefecture {:id=>38, :name=>"愛媛県", :ename=>"ehime"}
 - Prefecture {:id=>39, :name=>"高知県", :ename=>"kochi"}
 - Prefecture {:id=>40, :name=>"福岡県", :ename=>"fukuoka"}
 - Prefecture {:id=>41, :name=>"佐賀県", :ename=>"saga"}
 - Prefecture {:id=>42, :name=>"長崎県", :ename=>"nagasaki"}
 - Prefecture {:id=>43, :name=>"熊本県", :ename=>"kumamoto"}
 - Prefecture {:id=>44, :name=>"大分県", :ename=>"oita"}
 - Prefecture {:id=>45, :name=>"宮崎県", :ename=>"miyazaki"}
 - Prefecture {:id=>46, :name=>"鹿児島県", :ename=>"kagoshima"}
 - Prefecture {:id=>47, :name=>"沖縄県", :ename=>"okinawa"}
[admin@postgresql weather]$ 

Comments are closed.

Post Navigation