0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Rails】本番環境しかないプロジェクトにstaging環境を構築した話(全7回)

0
Posted at

はじめに

「PRマージしたら本番に直デプロイ」

...このフローで運用していたRailsプロジェクトに、ついにstaging環境を構築しました。

本記事はシリーズの導入編として、なぜstaging環境が必要になったのか、そして構築の全体像を紹介します。

プロジェクト概要

  • プロジェクト: AI ナビ(AIツールのQ&Aプラットフォーム)
  • 技術スタック: Ruby 4.0.0 / Rails 8.1 / MySQL 8.0 / Redis 7.2 / Puma / Sidekiq
  • インフラ: AWS EC2(Amazon Linux 2023)
  • デプロイ方法: EC2にSSHして deploy.sh を手動実行

これまでのデプロイフロー

ローカルで開発 → PR作成 → レビュー → mainにマージ → EC2にSSH → ./deploy.sh

本番環境に直接デプロイするフローです。deploy.sh の中身はこんな感じ:

#!/bin/bash
set -e

cd /var/www/ainavi
git fetch origin
git merge origin/main

bundle install
RAILS_ENV=production bundle exec rails assets:precompile
RAILS_ENV=production bundle exec rails db:migrate

# Pumaをkill -9で強制終了...
PUMA_PIDS=$(pgrep -f "puma.*ainavi" || true)
if [ -n "$PUMA_PIDS" ]; then
  echo "$PUMA_PIDS" | xargs kill -9
fi

RAILS_ENV=production nohup bundle exec puma -C config/puma.rb &

課題

  1. 本番で直接確認するしかない: UIの変更やDB変更を事前にテストできない
  2. ブランチの動作確認ができない: マージ前にブランチ単位で動作を見たい
  3. チームメンバーが増えた: 複数人で開発するにはstaging環境が不可欠
  4. kill -9 で強制終了: 処理中のリクエストが切断されるリスク

構築するstaging環境のゴール

  • 本番と同じ構成のEC2インスタンス
  • HTTPS対応(https://staging.ai-navi.biz
  • ブランチ指定でデプロイ可能(PRレビュー時に実機確認)
  • systemdでサービスを永続化(再起動しても自動復旧)

構成図

┌─────────────────────────────────────────────────┐
│                  AWS EC2 (t3.small)              │
│                  Amazon Linux 2023               │
│                                                  │
│  ┌──────────┐    ┌──────────┐    ┌──────────┐   │
│  │  Nginx   │───→│   Puma   │    │ Sidekiq  │   │
│  │ (SSL/    │    │ (Unix    │    │ (BG Jobs)│   │
│  │  Proxy)  │    │  Socket) │    │          │   │
│  └──────────┘    └──────────┘    └──────────┘   │
│       │               │              │           │
│       │          ┌──────────┐   ┌──────────┐    │
│       │          │  MySQL   │   │  Redis   │    │
│       │          │  8.0     │   │  7.2     │    │
│       │          └──────────┘   └──────────┘    │
│       │                                          │
│  Elastic IP: 57.181.44.170                       │
│  Domain: staging.ai-navi.biz                     │
│  SSL: Let's Encrypt                              │
└─────────────────────────────────────────────────┘

シリーズ構成

タイトル 内容
1 本記事 導入・全体像
2 EC2インスタンス作成編 AMI複製、サブネットの罠、SSH接続の試行錯誤
3 Ruby 4.0.0 & ミドルウェア構築編 Ruby ビルド(メモリ枯渇)、Redis 7、MySQL
4 Rails staging環境設定編 staging.rb、Puma設定、credentials
5 systemdサービス化編 Puma・Sidekiq・Redisの永続化
6 Nginx・DNS・SSL編 リバースプロキシ、Route 53、Let's Encrypt
7 デプロイスクリプト & 運用編 ブランチ指定デプロイ、seedデータ、ドキュメント整備

次回は、EC2インスタンスの作成で盛大にハマった話をお送りします。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?