Skip to content

Commit 6f8c50c

Browse files
feat(infra): add Railway staging deployment automation (FRO-61)
Infrastructure complète pour le déploiement automatique sur Railway : Database: - Migration tracking table (000_migration_history.sql) - Système de versioning avec checksum Scripts: - migrate-database.sh : Application automatique des migrations SQL - setup-railway-staging.sh : Guide interactif de setup Railway - test-migrations-local.sh : Tests locaux des migrations - test-migrations-windows.ps1 : Version Windows PowerShell - clean-test-db.sh : Nettoyage de la base de test - dump-schema.sh : Export du schéma pour comparaison Railway: - nixpacks.toml : Configuration build .NET - railway.staging.json : Configuration service staging GitHub Actions: - deploy-staging.yml : Workflow de déploiement automatique * CI checks * Deploy backend to Railway * Run SQL migrations * Deployment summary Documentation: - railway-staging-setup.md : Guide complet de setup - quick-start.md : Guide rapide 15 min - testing-migrations.md : Guide de test des migrations Architecture: Push develop → CI → Railway Deploy → SQL Migrations → Staging prêt via Happy (https://happy.engineering)
1 parent 894b5ba commit 6f8c50c

File tree

13 files changed

+2104
-0
lines changed

13 files changed

+2104
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
name: Deploy to Staging
2+
3+
on:
4+
push:
5+
branches: [develop]
6+
workflow_dispatch:
7+
8+
concurrency:
9+
group: deploy-staging
10+
cancel-in-progress: false
11+
12+
jobs:
13+
# Run CI first to ensure code quality
14+
ci:
15+
name: Run CI checks
16+
uses: ./.github/workflows/ci.yml
17+
18+
# Deploy backend to Railway
19+
deploy-backend:
20+
name: Deploy Backend to Railway
21+
needs: ci
22+
runs-on: ubuntu-latest
23+
environment:
24+
name: staging
25+
url: ${{ steps.deploy.outputs.url }}
26+
27+
steps:
28+
- name: Checkout
29+
uses: actions/checkout@v4
30+
31+
- name: Install Railway CLI
32+
run: npm install -g @railway/cli
33+
34+
- name: Deploy to Railway
35+
id: deploy
36+
env:
37+
RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN_STAGING }}
38+
run: |
39+
railway up --service ${{ secrets.RAILWAY_SERVICE_ID_STAGING }} --detach
40+
echo "url=https://$(railway domain)" >> $GITHUB_OUTPUT
41+
42+
- name: Deployment summary
43+
run: |
44+
echo "### 🚀 Backend Deployment" >> $GITHUB_STEP_SUMMARY
45+
echo "" >> $GITHUB_STEP_SUMMARY
46+
echo "**Environment:** Staging" >> $GITHUB_STEP_SUMMARY
47+
echo "**Service:** Backend API (.NET)" >> $GITHUB_STEP_SUMMARY
48+
echo "**URL:** ${{ steps.deploy.outputs.url }}" >> $GITHUB_STEP_SUMMARY
49+
echo "**Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY
50+
51+
# Run database migrations
52+
migrate-database:
53+
name: Run Database Migrations
54+
needs: deploy-backend
55+
runs-on: ubuntu-latest
56+
environment: staging
57+
58+
steps:
59+
- name: Checkout
60+
uses: actions/checkout@v4
61+
62+
- name: Install PostgreSQL client
63+
run: |
64+
sudo apt-get update
65+
sudo apt-get install -y postgresql-client
66+
67+
- name: Install Railway CLI
68+
run: npm install -g @railway/cli
69+
70+
- name: Get Database URL
71+
id: db-url
72+
env:
73+
RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN_STAGING }}
74+
run: |
75+
DB_URL=$(railway variables get DATABASE_URL --service ${{ secrets.RAILWAY_POSTGRES_SERVICE_ID_STAGING }})
76+
echo "::add-mask::$DB_URL"
77+
echo "database_url=$DB_URL" >> $GITHUB_OUTPUT
78+
79+
- name: Run migrations
80+
env:
81+
DATABASE_URL: ${{ steps.db-url.outputs.database_url }}
82+
run: |
83+
chmod +x infra/scripts/migrate-database.sh
84+
bash infra/scripts/migrate-database.sh "$DATABASE_URL"
85+
86+
- name: Migration summary
87+
run: |
88+
echo "### 🗄️ Database Migrations" >> $GITHUB_STEP_SUMMARY
89+
echo "" >> $GITHUB_STEP_SUMMARY
90+
echo "**Environment:** Staging" >> $GITHUB_STEP_SUMMARY
91+
echo "**Status:** ✅ Completed" >> $GITHUB_STEP_SUMMARY
92+
93+
# Deployment notification
94+
notify:
95+
name: Deployment Notification
96+
needs: [deploy-backend, migrate-database]
97+
if: always()
98+
runs-on: ubuntu-latest
99+
100+
steps:
101+
- name: Check deployment status
102+
run: |
103+
if [[ "${{ needs.deploy-backend.result }}" == "success" ]] && [[ "${{ needs.migrate-database.result }}" == "success" ]]; then
104+
echo "### ✅ Staging Deployment Successful" >> $GITHUB_STEP_SUMMARY
105+
echo "" >> $GITHUB_STEP_SUMMARY
106+
echo "**Branch:** develop" >> $GITHUB_STEP_SUMMARY
107+
echo "**Backend:** Deployed to Railway" >> $GITHUB_STEP_SUMMARY
108+
echo "**Database:** Migrations applied" >> $GITHUB_STEP_SUMMARY
109+
echo "**Frontend:** Auto-deployed to Vercel (preview)" >> $GITHUB_STEP_SUMMARY
110+
else
111+
echo "### ❌ Staging Deployment Failed" >> $GITHUB_STEP_SUMMARY
112+
echo "" >> $GITHUB_STEP_SUMMARY
113+
echo "Please check the logs for details." >> $GITHUB_STEP_SUMMARY
114+
exit 1
115+
fi
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-- ============================================================================
2+
-- FantasyRealm Online Character Manager - Migration Tracking
3+
-- PostgreSQL 18+
4+
-- ============================================================================
5+
6+
-- ============================================================================
7+
-- TABLE: migration_history
8+
-- Tracks applied database migrations to prevent re-execution
9+
-- ============================================================================
10+
CREATE TABLE IF NOT EXISTS migration_history (
11+
id SERIAL PRIMARY KEY,
12+
filename VARCHAR(255) NOT NULL UNIQUE,
13+
applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
14+
checksum VARCHAR(64) NOT NULL,
15+
execution_time_ms INTEGER,
16+
applied_by VARCHAR(100) DEFAULT 'system'
17+
);
18+
19+
CREATE INDEX idx_migration_history_filename ON migration_history(filename);
20+
CREATE INDEX idx_migration_history_applied_at ON migration_history(applied_at DESC);
21+
22+
-- Insert initial entry for migration tracking system itself
23+
INSERT INTO migration_history (filename, checksum, execution_time_ms, applied_by)
24+
VALUES ('000_migration_history.sql', 'initial', 0, 'system')
25+
ON CONFLICT (filename) DO NOTHING;

docs/deployment/quick-start.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Quick Start - Déploiement Staging
2+
3+
Guide rapide pour déployer l'environnement de recette en 15 minutes.
4+
5+
## ⚡ Prérequis (5 min)
6+
7+
```bash
8+
# 1. Installer Railway CLI
9+
npm install -g @railway/cli
10+
11+
# 2. Se connecter
12+
railway login
13+
14+
# 3. Créer compte MongoDB Atlas (gratuit)
15+
# https://mongodb.com/atlas
16+
```
17+
18+
## 🚀 Setup (10 min)
19+
20+
### 1. Railway (5 min)
21+
22+
```bash
23+
# Lancer le script de setup
24+
cd infra/scripts
25+
chmod +x setup-railway-staging.sh
26+
./setup-railway-staging.sh
27+
```
28+
29+
**Suivre les instructions du script :**
30+
- Créer projet "fantasyrealm-staging"
31+
- Ajouter PostgreSQL
32+
- Noter les tokens et IDs
33+
34+
### 2. MongoDB Atlas (3 min)
35+
36+
1. Créer cluster M0 (gratuit) → `fantasyrealm-staging`
37+
2. Network Access → Autoriser `0.0.0.0/0`
38+
3. Database Access → Créer user `fantasyrealm_staging`
39+
4. Copier la connection string
40+
41+
### 3. GitHub Secrets (2 min)
42+
43+
Aller dans GitHub → Settings → Secrets → Actions
44+
45+
Ajouter :
46+
- `RAILWAY_TOKEN_STAGING`
47+
- `RAILWAY_SERVICE_ID_STAGING`
48+
- `RAILWAY_POSTGRES_SERVICE_ID_STAGING`
49+
50+
## ✅ Premier déploiement
51+
52+
```bash
53+
git checkout develop
54+
git push origin develop
55+
```
56+
57+
Aller dans GitHub Actions et observer le déploiement ! 🎉
58+
59+
## 🔍 Vérification
60+
61+
```bash
62+
# Voir les logs
63+
railway logs
64+
65+
# Obtenir l'URL
66+
railway domain
67+
68+
# Tester l'API
69+
curl https://<railway-url>/health
70+
```
71+
72+
## 📚 Documentation complète
73+
74+
Voir [railway-staging-setup.md](./railway-staging-setup.md) pour tous les détails.
75+
76+
## ❓ Problèmes ?
77+
78+
### Le déploiement échoue
79+
```bash
80+
railway logs --tail
81+
```
82+
83+
### La migration échoue
84+
```bash
85+
DATABASE_URL=$(railway variables get DATABASE_URL)
86+
bash infra/scripts/migrate-database.sh "$DATABASE_URL"
87+
```
88+
89+
### Besoin d'aide ?
90+
Consulter [railway-staging-setup.md](./railway-staging-setup.md) section "Dépannage"

0 commit comments

Comments
 (0)