LoginSignup
1
0

More than 1 year has passed since last update.

herokuへのコンテナデプロイでのエラー ActiveSupport::MessageEncryptor::InvalidMessage: を解決

Last updated at Posted at 2021-12-05

我らが山浦清透大先生のこの動画の通りに,herokuへのコンテナデプロイをしようとしてます。

heroku run bundle exec rake db:migrate RAILS_ENV=production -a ideaframeworks

を実行したら

ActiveSupport::MessageEncryptor::InvalidMessage: ActiveSupport::MessageEncryptor::InvalidMessage

このようなエラーがでてきた。

secret_key_base
config/master.key
config/credentials.yml.enc
暗号化/複合化。
なんか聞いたことある。

端的にcredentials.yml.encの文字列とmaster.keyの文字列の組み合わせが不正解であるということです。

heroku config:set RAILS_MASTER_KEY=`cat config/master.key`

を実行

これを実行したらエラー解決できるとキータの記事にあった()

heroku run bundle exec rake db:migrate RAILS_ENV=production -a ideaframeworks

エラー変わらず。

heroku config -a ideaframeworksで

RAILS_MASTER_KEY:がちゃんと設定されていた。
ローカルのmaster.keyと同じなのに。。。。。

戯れにgit push heroku masterを実行

soichirohara@SoichironoMacBook-Pro ideaFrameworks % git push heroku master
Enumerating objects: 321, done.
Counting objects: 100% (321/321), done.
Delta compression using up to 8 threads
Compressing objects: 100% (285/285), done.
Writing objects: 100% (321/321), 219.98 KiB | 5.95 MiB/s, done.
Total 321 (delta 98), reused 0 (delta 0), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote: === Fetching app code
remote: 
remote: =!= Your app does not include a heroku.yml build manifest. To deploy your app, either create a heroku.yml: https://devcenter.heroku.com/articles/build-docker-images-heroku-yml
remote: Or change your stack by running: 'heroku stack:set heroku-20'
remote: Verifying deploy...
remote: 
remote: !   Push rejected to ideaframeworks.
remote: 
To https://git.heroku.com/ideaframeworks.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/ideaframeworks.git'

heroku.yml

build:
  docker:
    web: Dockerfile.production


heroku.ymlでイメージをビルドしてdeployできるとかなんとか

Dockerfile.production


FROM ruby:2.6.5

ENV RAILS_ENV=production


## nodejsとyarnはwebpackをインストールする際に必要
# yarnパッケージ管理ツールをインストール

RUN curl http://deb.debian.org/debian/dists/buster/main/binary-amd64/by-hash/SHA256/935deda18d5bdc25fb1813d0ec99b6e0e32a084b203e518af0cf7dc79ee8ebda | head

RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && apt-get install -y yarn  && apt-get install -y graphviz


RUN /bin/sh -c /bin/sh -c bundle update --bundler
RUN gem install bundler:2.1.4

RUN mkdir /ideaFrameworks
WORKDIR /ideaFrameworks
COPY . /ideaFrameworks
COPY Gemfile /ideaFrameworks/Gemfile
COPY Gemfile.lock /ideaFrameworks/Gemfile.lock
RUN bundle install


COPY start.sh /start.sh
RUN chmod 744 /start.sh
CMD ["sh","/start.sh"]

heroku stack:set container

git push heroku master

を実行。

 Post-install message from rubyzip:
remote: RubyZip 3.0 is coming!
remote: **********************
remote: 
remote: The public API of some Rubyzip classes has been modernized to use named
remote: parameters for optional arguments. Please check your usage of the
remote: following classes:
remote:   * `Zip::File`
remote:   * `Zip::Entry`
remote:   * `Zip::InputStream`
remote:   * `Zip::OutputStream`
remote: 
remote: Please ensure that your Gemfiles and .gemspecs are suitably restrictive
remote: to avoid an unexpected breakage when 3.0 is released (e.g. ~> 2.3.0).
remote: See https://github.com/rubyzip/rubyzip for details. The Changelog also
remote: lists other enhancements and bugfixes that have been implemented since
remote: version 2.3.0.
remote: Removing intermediate container abde761777fb
remote:  ---> 591c8c89978c
remote: Step 13/15 : COPY start.sh /start.sh
remote: COPY failed: stat /var/lib/docker/tmp/docker-builder709159940/start.sh: no such file or directory
remote: 
remote: Verifying deploy...
remote: 
remote: !   Push rejected to ideaframeworks.
remote: 
To https://git.heroku.com/ideaframeworks.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/ideaframeworks.git

COPY failed

start.shが消えていたので、

start.sh

#!/bin/sh

if ["${RAILS_ENV}"="production"]
then 
    bundle exec rails assets:precompile
fi

bundle exec rails s -p ${PORT:-3000} -b 0.0.0.0

を作成して

コミット,pushして再度

git push heroku master

というかビルドする方法をすこし変えただけなので、

heroku run bundle exec rake db:migrate RAILS_ENV=production -a ideaframeworks

を実行したら

ActiveSupport::MessageEncryptor::InvalidMessage:

が出るエラーは多分変わらん。

根本的な問題はそこではない。

どうしてもできない場合は、
credentials.yml.encを一度消して、$ EDITOR="vi" bin/rails credentials:editを再度実行することで、credentials.yml.encとmaster.keyが新しく作り直されます

とあるので、こうしたら秘密鍵と公開鍵が合致する可能性もある。
が、そこらへんをいじるのはこわいので最終手段。

なにはともあれ
deploy成功した。ので


heroku run bundle exec rails db:migrate RAILS_ENV=production           

soichirohara@SoichironoMacBook-Pro ideaFrameworks % heroku run bundle exec rails db:migrate RAILS_ENV=production -

Running bundle exec rails db:migrate RAILS_ENV=production on ⬢ ideaframeworks... up, run.3070 (Free)
rails aborted!
ActiveSupport::MessageEncryptor::InvalidMessage: ActiveSupport::MessageEncryptor::InvalidMessage
/ideaFrameworks/config/environment.rb:5:in `'

Caused by:
OpenSSL::Cipher::CipherError:
/ideaFrameworks/config/environment.rb:5:in `'
Tasks: TOP => db:migrate => db:load_config => environment
(See full trace by running task with --trace)

config/enviroment.rb

Rails.application.initialize!

この方も自分と同じエラーで、master.keyとcredentials.yml.encの削除

EDITOR="vi" bin/rails credentials:edit

をすればいいらしい。

もうこれしかねぇ。

master.keyが再度作られない。。。。

この記事によるとsudoをやらないといけないらしい

master.keyとcredentials.yml.encの削除して


sudo EDITOR="vi" rails credentials:edit

sudo権限を付与したら両方つくられた。

sudo vi config/master.key

これでmaster.keyの値もみれた。

新しくつくられたmaster.keyの値もセットした

heroku run bundle exec rails db:migrate RAILS_ENV=production

お願いします!!!!

エラー変わらず。。。。

credentials.yml.encを書き換えたあとにpushしわすれた。

git push heroku masterを実行
これでcredentials.yml.encとmaster.keyの値は一致するはず。

heroku run bundle exec rails db:migrate RAILS_ENV=production

お願いします!!!!

LoadError: Error loading the 'postgresql' Active Record adapter. Missing a gem it depends on? pg is not part of the bundle. Add it to your Gemfile.


Caused by:
Gem::LoadError: pg is not part of the bundle. Add it to your Gemfile.

Tasks: TOP => db:migrate => db:load_config
(See full trace by running task with --trace)

エラー内容が変わった!やったぜ!

これはheroku ポスグレのアドオンがideaframeworksに入っていたので、削除して、

再度

heroku run bundle exec rails db:migrate RAILS_ENV=production


Running bundle exec rails db:migrate RAILS_ENV=production on ⬢ ideaframeworks... up, run.4267 (Free)
rails aborted!
ActiveRecord::ConnectionNotEstablished: Unknown MySQL server host 'db' (-2)


Caused by:
Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (-2)

Tasks: TOP => db:migrate
(See full trace by running task with --trace)

エラーが変わりましたね。

database.yml


production:
  <<: *default
  database: ideaFrameworks_production
  username: ideaFrameworks
  password: <%= ENV['MYSQL_ROOT_PASSWORD'] %>

のプロダクションがこうなってました。
以前、設定した値が何故か吹きとんでたので

この動画のまんまの設定にして

database.yml

production:
  <<: *default
  database: <%= ENV['APP_DATABASE'] %>
  username: <%= ENV['APP_DATABASE_USERNAME'] %>
  password: <%= ENV['APP_DATABASE_PASSWORD'] %>
  host: <%= ENV['APP_DATABASE_HOST'] %>


に設定しなおして

git pushとgit push heroku master

して

heroku run bundle exec rails db:migrate RAILS_ENV=production

これでいけるだろ!!!

soichirohara@SoichironoMacBook-Pro ideaFrameworks % heroku run bundle exec rails db:migrate RAILS_ENV=production
Running bundle exec rails db:migrate RAILS_ENV=production on ⬢ ideaframeworks... up, run.8668 (Free)
I, [2021-12-05T06:50:22.412316 #4] INFO -- : Migrating to CreateMandalarts (20211202075545)
== 20211202075545 CreateMandalarts: migrating =================================
-- create_table(:mandalarts)
-> 0.0229s
== 20211202075545 CreateMandalarts: migrated (0.0230s) ========================

きた=======!!!!!!!

まとめると

ここまでうまくいってる前提で話します。

heroku run bundle exec rake db:migrate RAILS_ENV=production -a ideaframeworks

を実行したら

ActiveSupport::MessageEncryptor::InvalidMessage: ActiveSupport::MessageEncryptor::InvalidMessage

というエラーがでてきた。

どうやら、credentials.yml.encの文字列とmaster.keyの文字列の組み合わせが合ってないらしい。

この記事を参考に、

master.keyとcredentials.yml.encの削除して


sudo EDITOR="vi" rails credentials:edit

sudo権限を付与したら両方つくられた。

sudo vi config/master.key

これでmaster.keyの値もみれた。

新しくつくられたmaster.keyの値もheroku config:set とかでセットして、

git push  origin master

git push heroku master

か、

heroku container:push とか

個人的には、heroku.ymlのほうが
ビルドするDockerfileも指定しやすくて好き。

んで、どっちかのコマンドを打ったら、

master.keyとcredentials.yml.encが合致するので、うまくいくはず。

おまけ

Dockefile.production


FROM ruby:2.6.5

ENV RAILS_ENV=production


## nodejsとyarnはwebpackをインストールする際に必要
# yarnパッケージ管理ツールをインストール

RUN curl http://deb.debian.org/debian/dists/buster/main/binary-amd64/by-hash/SHA256/935deda18d5bdc25fb1813d0ec99b6e0e32a084b203e518af0cf7dc79ee8ebda | head

RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && apt-get install -y yarn  && apt-get install -y graphviz


RUN /bin/sh -c /bin/sh -c bundle update --bundler
RUN gem install bundler:2.1.4

RUN mkdir /ideaFrameworks
WORKDIR /ideaFrameworks
COPY . /ideaFrameworks
COPY Gemfile /ideaFrameworks/Gemfile
COPY Gemfile.lock /ideaFrameworks/Gemfile.lock
RUN bundle install


COPY start.sh /start.sh
RUN chmod 744 /start.sh
CMD ["sh","/start.sh"]

start.sh


#!/bin/sh

if ["${RAILS_ENV}"="production"]
then 
    bundle exec rails assets:precompile
fi

bundle exec rails s -p ${PORT:-3000} -b 0.0.0.0

heroku.yml

build:
  docker:
    web: Dockerfile.production

これで,git push heroku masterでデプロイができるので良き

1
0
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
0