LoginSignup
1
1

More than 3 years have passed since last update.

ファイル保存先をS3に設定後、RailsアプリをHerokuにpushすると失敗するエラーについて

Posted at

エラー発生の経緯

本番環境でファイルを投稿後、1日程経つとファイルが表示されなくなる問題が発生していたため、HerokuへデプロイしていたRailsアプリを更新し、ファイルアップロード先をAWS S3に変更しました。

ローカル環境では適用に成功しましたが、git push heroku masterコマンドで本番環境に適用しようとするとエラーが発生しました。

エラー内容

ターミナル
Caused by:
ArgumentError: key must be 16 bytes

結論

以下のコマンドで環境変数を設定するとエラーが解消されました。
heroku config:set RAILS_MASTER_KEY=`cat config/master.key`

原因

元々下記のようなエラーが出ており、解消のために
heroku config:set RAILS_MASTER_KEY=`rake secret`
と言うコマンドで環境変数を設定していました。

ターミナル

-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       Missing encryption key to decrypt file with. Ask your team for your master key and write it to /tmp/build_6edee55a_/config/master.key or put it in the ENV['RAILS_MASTER_KEY'].
 !
 !     Precompiling assets failed.
 !
 !     Push rejected, failed to compile Ruby app.
 !     Push failed

`rake secret`は secret.yml を使っている場合のコマンドであり、credentials.yml.enc (Rails5.2以降) を使っている場合はRAILS_MASTER_KEY には`cat config/master.key`と設定する必要がありました。

参考:Ask your team for your master key and put it in ENV[“RAILS_MASTER_KEY”] on heroku deploy

解決までに行ったこと

エラー解消方法に気づく直前まで、最後の方で出力されていた以下のようなエラーに注目していました。
出力されている通りPrecompiling assets failedPush rejectedfailed to compile RubyPush rejectedといったワードで検索してエラー解消に奮闘していましたが、解決しませんでした。

ターミナル
remote:        /tmp/build_0a1646cf/vendor/bundle/ruby/2.5.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:62:in `block (2 levels) in define'
remote:        Tasks: TOP => environment
remote:        (See full trace by running task with --trace)
remote: 
remote:  !
remote:  !     Precompiling assets failed.
remote:  !
remote:  !     Push rejected, failed to compile Ruby app.
remote: 
remote:  !     Push failed
remote: Verifying deploy...
remote: 
remote: !       Push rejected to (アプリ名).
remote: 
To https://git.heroku.com/(アプリ名).git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/(アプリ名).git'

エラー解消に繋がらなかった作業

1. heroku git push先を再設定
herokuのリモートリポジトリをセット
 git remote set-url heroku https://git.heroku.com/(アプリ名).git
heroku git push先確認
 git remote -v

2. config/application.rbの設定変更
config/application.rb の config.assets.initialize_on_precompile = trueconfig.assets.initialize_on_precompile = false に変更
参考 RailsでアプリをHerokuにあげる時のエラー各種
   Herokuにpushしたらprecompile assets faild.が。pushできたと思ったらApplication Errorが。解決するまで。

3. assets:precompileの設定変更
RAILS_ENV=production bundle exec rake assets:precompile
参考 Herokuにデプロイしたときの「Precompiling assets failed.」エラーについて

4. buildpackを手動設定してデプロイ
heroku buildpacks:set heroku/ruby
参考 Heroku へのデプロイに参った!

5. Herokuを消して、作り直し
Herokuのアプリ管理画面の Settings → Delete app からアプリを削除
git remote rm heroku でremoteにあるherokuを削除
heroku create (アプリ名) でアプリを作り直す ※アプリ名は元と同じ
git push heroku masterでデプロイ
参考 herokuにデプロイできない

6. 通常にデプロイできていた部分までrevertして再デプロイ
GitHub Desktop の History でコミットを1つずつ右クリックして Revert this Commit

7. 強制プッシュでデプロイ
git push heroku --force master

学んだこと

・ログは上の方もしっかり読む(最後の方のログに惑わされない)
・エラー発生直前に行った作業から原因を推測する(関係の薄いファイルはいじり過ぎない)
credentials.yml.encconfig/master.key、 環境変数の仕組みを理解することが重要
・herokuはアプリを消すと再度同じURLでアプリ作成、デプロイできる
・Git で管理されないファイルを変更したりして環境変数が関わるエラーが発生した際は revert や 再デプロイしても本番環境のエラーは直らない

特にPrecompiling assets failedといったエラーに関する情報は沢山ありますが、原因は様々なため、エラー内容をしっかり見ることが非常に大切だと実感しました。

※最後の方のエラーばかり見て解決に何時間もかかっていましたが
「ArgumentError: key must be 16 bytes」がエラーの本体だと気づいた後、30分もかからずに解決しました!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1