$30 off During Our Annual Pro Sale. View Details »

RAILS_ENVを統合する取り組み: 開発用デプロイ環境をよりシンプルに

Akari
September 09, 2023

RAILS_ENVを統合する取り組み: 開発用デプロイ環境をよりシンプルに

Osaka RubyKaigi 03のLT会で発表したスライドです。

Akari

September 09, 2023
Tweet

More Decks by Akari

Other Decks in Programming

Transcript

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

    View Slide

  2. 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

    View Slide

  3. 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アプリケーション

    View Slide

  4. 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環境

    View Slide

  5. 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

    View Slide

  6. 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
    やることはシンプル!!!
    だが、地味で面倒😇
    まあ、やるしかない💪

    View Slide

  7. 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

    View Slide

  8. 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

    View Slide

  9. 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

    View Slide

  10. 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

    View Slide