Slide 1

Slide 1 text

Copyright © 2023 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止 RAILS_ENVを統合する取り組み: 開発用デプロイ環境をよりシンプルに 明里 慶祐 株式会社 アンドパッド 2023/9/9 大阪Ruby会議03 LT

Slide 2

Slide 2 text

Copyright © 2023 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止 1. 自己紹介 株式会社アンドパッド / プラットフォーム開発チーム   明里 慶祐 @k-akari @akarin0519 2022年5月入社。 2023年3月まで、社内で最も古くから稼働する施工管理アプリ (Rails)のインフラ・CI/CD改善に従事。 2023年4月よりANDPADの通知プラットフォーム(Go言語、 gRPCサーバ)の開発に従事。 Confidential

Slide 3

Slide 3 text

Copyright © 2023 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止 2. 施工管理アプリとは Confidential 社内で最も古くから稼働している巨大でモノリシックな Railsアプリケーション

Slide 4

Slide 4 text

Copyright © 2023 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止 3. 改善前の施工管理アプリ Confidential ● デプロイ環境とRails.envが一対一 ● Rails.envが16個 データベース Rails アプリケーション (Rails.env = staging) データベース Rails アプリケーション (Rails.env = staging2) データベース Rails アプリケーション (Rails.env = staging3) staging環境 staging2環境 staging3環境

Slide 5

Slide 5 text

Copyright © 2023 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止 4. 何が問題なのか ● 本番環境と厳密にコードが同一となる環境が存在しない。 ○ デプロイ環境毎にconfig/environmentsなどが異なる。 ○ Rails.envによる分岐ロジックが実装可能になる。 ● デプロイ環境毎の差分が把握しにくい。(=認知負荷高) ○ config/environments? ○ config/settings? (config gemを利用している場合) ○ 環境変数? ○ Rails.envの分岐ロジック? Confidential

Slide 6

Slide 6 text

Copyright © 2023 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止 5. 解決方針 ● デプロイ環境ごとに異なる設定を全て環境変数化する。 ○ The Twelve-Factor Appでも推奨されている。 ● Rails.envを標準の3つ + stagingの合計4つにまで減らす。 ○ 最終的にはRails.envを標準の3つだけにしたい。 Confidential やることはシンプル!!! だが、地味で面倒😇 まあ、やるしかない💪

Slide 7

Slide 7 text

Copyright © 2023 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止 6. 取り組み手順 1. 方針をDesign Docとしてまとめ、関係者の同意を得る。 2. アプリケーションコードに記述されたRails.envによる分岐 ロジックをconfig gemを利用した分岐ロジックに置き換える。 例)Rails.env.production? => Settings.deploy_env==”production” 3. 開発環境のconfig/environmentsファイルを統一する。 デプロイ環境差分はconfig gemを利用して分岐させる。 例)redis_host: Settings.redis.host 4. デプロイ環境毎に異なる設定値を環境変数化し、config gemには 環境変数の値を参照させる。 例)host: <%= ENV.fetch('HOST') %> Confidential

Slide 8

Slide 8 text

Copyright © 2023 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止 7. 結果 ● Rails.envを5つにまで削減🎉(残り一つはデプロイ環境ごと廃止予定。) ○ これにより、デプロイ環境毎に異なる設定値は環境変数 に設定するしか方法がない。 ● 要した期間はおよそ8ヶ月(2022年8月〜2023年3月)。 Confidential

Slide 9

Slide 9 text

Copyright © 2023 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止 8. 教訓 ● デプロイ環境毎にRails.envは増やさないでおく。 ○ デプロイ環境毎に異なる設定値がRails.envによって 切り替わることを防げる。 ● デプロイ環境ごとに異なる設定は環境変数で一元管理する。 Confidential

Slide 10

Slide 10 text

Copyright © 2023 Present ANDPAD Inc. This information is confidential and was prepared by ANDPAD Inc. for the use of our client. It is not to be relied on by and 3rd party. Proprietary & Confidential 無断転載・無断複製の禁止 9. 終わりに ご清聴ありがとうございました🙏 Confidential