fluid_27’s blog

勉強した内容をアウトプットするためのブログ

Herokuとgithubを連携させてデプロイ 初デプロイまでの道のり④

以前、Herokuでデプロイさせる記事をまとめたんですが、今回はyoutubeでHerokuとgithubを連携させて簡単にデプロイできる方法の動画を見つけたので、実際に今作っているアプリをデプロイさせてみました。

 

www.youtube.com

 

 

Herokuとgithubを連携させてデプロイしてみる

 

まず、今のアプリはAWSでデプロイしようとしてたのですが、すったもんだ上手くいかず、何が悪いのか原因がわからないので、とりあえずHerokuでデプロイしてみます。

ま、AWSはそのうちに。。

 

動画では何種類かデプロイの方法を紹介してくれているのですが、今回はHerokuのダッシュボード上でgithubのブランチをデプロイする方法をとってみました。

 

Herokuには以前の記事の中でアカウントを作成しています。

fluid-27.hatenablog.com

 

早速、やってみます。

 

・・・・

You are trying to install ruby-2.6.2 on heroku-20. ! !

Ruby ruby-2.6.2 is present on the following stacks: ! !

- cedar-14 !

- heroku-16 !

- heroku-18 ! !

Heroku recommends you use the latest supported Ruby version listed here: ! https://devcenter.heroku.com/articles/ruby-support#supported-runtimes ! !

For more information on syntax for declaring a Ruby version see: ! https://devcenter.heroku.com/articles/ruby-versions ! ! Push rejected, failed to compile Ruby app. ! Push failed

 

Rubyのバージョンがサポートされてない?

 

Heroku recommends you use the latest supported Ruby version listed here: ! https://devcenter.heroku.com/articles/ruby-support#supported-runtimes

とあるので、上記のリンクに飛んでみます。

 

Herokuは、次のRubyバージョンと関連するRubygemsをサポートしています。サポートされているバージョンとは、ツールとプラットフォームが特定のバージョンで動作することを期待できることを意味します。また、テクニカルサポートを受けることができることも意味します。サポートされているRubyバージョンは次のとおりです。

2.6.7、Rubygems: 3.0.3.1

2.7.3、Rubygems: 3.1.6

3.0.1、Rubygems: 3.2.15

と書かれている。

ちなみに、この時の自分のrubyは2.6.2。

当方、ど素人なので、上段の左記の2.6.7のバージョンに合わせろってこと?それとも、その右記にある3.0.3.1に合わせろってこと?

という、疑問が。

が、左がrubyのバージョンで、右がRubygems。つまりgemそのもののバージョンらしい?ということに気づく。 

 

ということで、まずはバージョンあげよう。

 

$ rbenv install -list

 

2.5.8

2.6.6

2.7.2

3.0.0

jruby-9.2.14.0

 

・・・

ん? 

 

rbenv install -listに指定されたバージョン出てこないんですが。。

 

なぜ?と思ってそれらしいキーワードでググっていたところ下記の記事が。

https://qiita.com/ChaaaBooo/items/10c7a484ec83db368960

 

なんでもruby-buildが古い可能性があるとのこと。

なので、試しにバージョン確認。

ruby-build --version

ruby-build 20201225

 

ということでアップデートしてみる。

$ brew upgrade ruby-build

$ ruby-build --version

 

ruby-build 20210405

 

んでもって、もっかいリストを

$ rbenv install -list

 

2.5.9

2.6.7

2.7.3

3.0.1

・・・

 

はい、出ました!

改めて

$ rbenv install 2.6.7

・・・

BUILD FAILED (Mac OS X 10.15.7 using ruby-build 20210405)

Inspect or clean up the working tree at /var/folders/82/ms1x_s_97cqfp273npm88c940000gn/T/ruby-build.20210410180230.11094.O5B1v7

Results logged to /var/folders/82/ms1x_s_97cqfp273npm88c940000gn/T/ruby-build.20210410180230.11094.log

Last 10 log lines:

vm.c:2295:9: error: implicit declaration of function 'rb_native_mutex_destroy' is invalid in C99 [-Werror,-Wimplicit-function-declaration]

rb_native_mutex_destroy(&vm->waitpid_lock);

^

vm.c:2489:34: warning: expression does not compute the number of elements in this array; element type is 'const int', not 'VALUE' (aka 'unsigned long') [-Wsizeof-array-div]

sizeof(ec->machine.regs) / sizeof(VALUE));

~~~~~~~~~~~~~~~~ ^

vm.c:2489:34: note: place parentheses around the 'sizeof(VALUE)' expression to silence this warning

1 warning and 1 error generated.

make: *** [vm.o] Error 1

make: *** Waiting for unfinished jobs....

 

とエラー。。一応、

$ rbenv versions

としてみると、やっぱり2.6.7入ってない

 

色々ググってみると

https://blog.n-z.jp/blog/2021-04-08-old-ruby-with-new-xcode.html

という記事に辿り着く。

CFLAGS="-Wno-error=implicit-function-declaration"

をつけるとエラー回避できる。とあるけど、その前に「古いバージョンだと起きる可能性あり」とあったので

 

$ rbenv install 2.7.3

してみることに。

 

、、、入った!

$ rbenv versions

 

system

2.5.1

* 2.6.2 (set by /Users/tanaberyouni/.rbenv/version)

2.7.1

2.7.3

・・・

 

入ってる!

$rbenv local 2.7.3

実行!!

はい、rubyのバージョンは上げました。ただ、、

  

MySQLはサポートされてないみたいなのでPostgreSQLに置き換えます

 

いま作っているアプリはAWSでデプロイしようと思ってたので、MySQLで作っていたのですが、HerokuではPostgreSQLしか対応していないみたいです。。

なんか、そこを上手くPostgreSQLのままデプロイさせる方法もあるみたいなんですが、正式にはやっぱりサポートされてないみたいです。

なんで、MysqlPostgresqlに置き換えてみようと思います。

 

ググってみると pgloaderというもので簡単に置き換えられる。とあるので、それで試してみようと思います。

pgloader.io

 

まず、GemfileのMySQLを開発&テスト環境のみに

group :development, :test do

  gem mysql

end

 

 

そしてPostgreSQLを本番環境に

 

group :production, :staging do

 gem pg

end

 

で、

$ bundle

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

An error occurred while installing mysql2 (0.5.3), and

Bundler cannot continue.

Make sure that `gem install mysql2 -v '0.5.3' --source

'https://rubygems.org/'` succeeds before bundling.

 

エラーが。。

上記にあるように

 

$ gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'

してみる。

Building native extensions. This could take a while...

ERROR: Error installing mysql2:

ERROR: Failed to build gem native extension.

・・・

2 warnings generated.

compiling infile.c

compiling mysql2_ext.c

compiling result.c

compiling statement.c

linking shared-object mysql2/mysql2.bundle

ld: library not found for -lssl

clang: error: linker command failed with exit code 1 (use -v to see invocation)

make: *** [mysql2.bundle] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/tanaberyouni/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/mysql2-0.5.3 for inspection.

Results logged to /Users/tanaberyouni/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/extensions/x86_64-darwin-19/2.7.0/mysql2-0.5.3/gem_make.out

 

 

やっぱり、エラー。

ググってみると

https://valuable.hatenablog.com/entry/20200515/1589506999

という記事発見。記事にある通り

$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib"

で解決!

 

 

pgloaderをインストールしてみる

 

続いて、pgloaderのインストールしてPostgreSQLに移行させてみる。

$ brew install pgloader

pgloaderのgithubに載っている通り、まずデータベースの作成してみる。

GitHub - dimitri/pgloader: Migrate to PostgreSQL in a single command!

 

$ createdb pagila

 

createdb: error: could not connect to database template1: could not connect to server: No such file or directory

Is the server running locally and accepting

connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

 

というエラー。

最後の文をコピペしてググってみると、brew info postgresql でpostgresqlの情報を出力してくれると。

 

$ brew info postgresql

 

postgresql: stable 13.2 (bottled), HEAD Object-relational database system

https://www.postgresql.org/ /usr/local/Cellar/postgresql/13.2_1 (3,218 files, 42.6MB) * Poured from bottle on 2021-03-11 at 10:12:00 From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/postgresql.rb License: PostgreSQL

==> Dependencies

Build: pkg-config ✔ Required: icu4c ✔, krb5 ✔, openssl@1.1 ✔, readline ✔

==> Options

--HEAD Install HEAD version

==> Caveats

To migrate existing data from a previous major version of PostgreSQL

run: brew postgresql-upgrade-database

This formula has created a default database cluster

with: initdb --locale=C -E UTF-8 /usr/local/var/postgres For more details,

read: https://www.postgresql.org/docs/13/app-initdb.html

To have launchd start postgresql now and restart at login: brew services start postgresql

Or, if you don't want/need a background service you can just

run: pg_ctl -D /usr/local/var/postgres start

==> Analytics install: 168,476 (30 days), 521,344 (90 days), 2,254,430 (365 days)

 

上記の情報をもとに

$ pg_ctl -D /usr/local/var/postgres start

 

waiting for server to start....2021-04-11 19:20:55.698 JST [76625] FATAL: database files are incompatible with server

2021-04-11 19:20:55.698 JST [76625] DETAIL: The data directory was initialized by PostgreSQL version 12, which is not compatible with this version 13.2.

stopped waiting

pg_ctl: could not start server

Examine the log output.

 

というエラーが。PostgreSQLのバージョンの問題?

ググった情報を頼りに

$ brew postgresql-upgrade-database

でアップデートしてから、再びチャレンジ。

 

$ createdb pagila

$ pgloader mysql://user@localhost/sakila postgresql:///pagila

 

で、上手く行きました!

 

いったん、githubにプッシュしてからHerokuのダッシュボードでデプロイすると、、、

無事できました!!

 

まとめ

  • Herokuのダッシュボードでデプロイできるのは楽。

  • $ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib"あたりのコマンドは理解できてない。。場当たり的にググっていくだけでなく、構造的に勉強することが必要。

 

以上です。

 

もし、同じような境遇の方がいれば、参考にしてもらえたらと思います。

補足や間違いがあればご指摘いただけると幸いです。

 

また、一緒にプログラミングを学習していける仲間を探しています。「なんかモチベーションが上がらないなぁ、、」そんな時にも、お互いに触発しあって学習していけたらと思います。ぜひ、下記まで気軽にご連絡ください。

 

twitter.com