forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
225 lines (198 loc) · 9.2 KB
/
Copy pathcd-staging.yml
File metadata and controls
225 lines (198 loc) · 9.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
name: CD — Staging
on:
push:
branches: [main]
concurrency:
group: cd-staging
cancel-in-progress: false # never cancel an in-flight deploy
jobs:
# ──────────────────────────────────────────────────────────────────────────
# 1. Deploy backend to Railway staging
# ──────────────────────────────────────────────────────────────────────────
deploy-backend:
name: Deploy Backend → Railway Staging
runs-on: ubuntu-latest
environment:
name: staging
url: ${{ secrets.STAGING_BACKEND_URL }}
outputs:
deployment_id: ${{ steps.deploy.outputs.deployment_id }}
steps:
- uses: actions/checkout@v7
- name: Install Railway CLI
run: npm install -g @railway/cli
- name: Deploy to Railway
id: deploy
env:
RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}
run: |
cd novaRewards/backend
DEPLOY_OUTPUT=$(railway up --service nova-rewards-backend --environment staging --detach 2>&1)
echo "$DEPLOY_OUTPUT"
DEPLOYMENT_ID=$(echo "$DEPLOY_OUTPUT" | grep -oP 'deployment/\K[a-zA-Z0-9]+' | head -1 || echo "")
echo "deployment_id=$DEPLOYMENT_ID" >> "$GITHUB_OUTPUT"
- name: Wait for Railway deployment
env:
RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}
run: |
echo "Waiting for Railway deployment to become healthy..."
for i in $(seq 1 24); do
STATUS=$(railway status --service nova-rewards-backend --environment staging --json 2>/dev/null | jq -r '.status // "unknown"' || echo "unknown")
echo "Attempt $i: status=$STATUS"
if [ "$STATUS" = "SUCCESS" ]; then
echo "Deployment healthy."
exit 0
fi
if [ "$STATUS" = "FAILED" ] || [ "$STATUS" = "CRASHED" ]; then
echo "Deployment failed with status: $STATUS"
exit 1
fi
sleep 10
done
echo "Timed out waiting for deployment."
exit 1
# ──────────────────────────────────────────────────────────────────────────
# 2. Deploy frontend to Vercel staging
# ──────────────────────────────────────────────────────────────────────────
deploy-frontend:
name: Deploy Frontend → Vercel Staging
runs-on: ubuntu-latest
environment:
name: staging
url: ${{ steps.deploy.outputs.preview_url }}
outputs:
preview_url: ${{ steps.deploy.outputs.preview_url }}
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 20
- name: Install Vercel CLI
run: npm install -g vercel
- name: Deploy to Vercel (staging)
id: deploy
env:
VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
run: |
cd novaRewards/frontend
DEPLOY_URL=$(vercel deploy \
--token "$VERCEL_TOKEN" \
--env NEXT_PUBLIC_API_URL="${{ secrets.STAGING_BACKEND_URL }}" \
--env NEXT_PUBLIC_STELLAR_NETWORK=TESTNET \
--build-env NEXT_PUBLIC_API_URL="${{ secrets.STAGING_BACKEND_URL }}" \
--build-env NEXT_PUBLIC_STELLAR_NETWORK=TESTNET \
2>&1 | tail -1)
echo "preview_url=$DEPLOY_URL" >> "$GITHUB_OUTPUT"
echo "Deployed to: $DEPLOY_URL"
# ──────────────────────────────────────────────────────────────────────────
# 3. Smoke tests against staging
# ──────────────────────────────────────────────────────────────────────────
smoke-tests:
name: Smoke Tests
runs-on: ubuntu-latest
needs: [deploy-backend, deploy-frontend]
outputs:
passed: ${{ steps.smoke.outputs.passed }}
steps:
- uses: actions/checkout@v7
- name: Run smoke tests
id: smoke
env:
STAGING_BACKEND_URL: ${{ secrets.STAGING_BACKEND_URL }}
STAGING_FRONTEND_URL: ${{ needs.deploy-frontend.outputs.preview_url }}
run: |
set +e
FAILED=0
check() {
local label="$1"
local url="$2"
local expected_status="${3:-200}"
local response
response=$(curl -s -o /dev/null -w "%{http_code}" --max-time 15 "$url")
if [ "$response" = "$expected_status" ]; then
echo "✅ $label ($url) → $response"
else
echo "❌ $label ($url) → $response (expected $expected_status)"
FAILED=$((FAILED + 1))
fi
}
# Backend health
check "Backend /health" "$STAGING_BACKEND_URL/health"
check "Backend /metrics" "$STAGING_BACKEND_URL/metrics"
check "API docs" "$STAGING_BACKEND_URL/api/docs"
# Auth endpoints exist (expect 400/422 without body, not 404/500)
AUTH_STATUS=$(curl -s -o /dev/null -w "%{http_code}" --max-time 15 \
-X POST "$STAGING_BACKEND_URL/api/auth/login" \
-H "Content-Type: application/json" -d '{}')
if [ "$AUTH_STATUS" = "400" ] || [ "$AUTH_STATUS" = "422" ] || [ "$AUTH_STATUS" = "401" ]; then
echo "✅ Auth /login endpoint reachable ($AUTH_STATUS)"
else
echo "❌ Auth /login unexpected status: $AUTH_STATUS"
FAILED=$((FAILED + 1))
fi
# Frontend
if [ -n "$STAGING_FRONTEND_URL" ]; then
check "Frontend home" "$STAGING_FRONTEND_URL"
fi
if [ "$FAILED" -gt 0 ]; then
echo "passed=false" >> "$GITHUB_OUTPUT"
echo "$FAILED smoke test(s) failed."
exit 1
else
echo "passed=true" >> "$GITHUB_OUTPUT"
echo "All smoke tests passed."
fi
# ──────────────────────────────────────────────────────────────────────────
# 4. Auto-rollback if smoke tests fail
# ──────────────────────────────────────────────────────────────────────────
rollback:
name: Rollback on Failure
runs-on: ubuntu-latest
needs: smoke-tests
if: failure() && needs.smoke-tests.outputs.passed == 'false'
steps:
- uses: actions/checkout@v7
- name: Install Railway CLI
run: npm install -g @railway/cli
- name: Rollback Railway backend
env:
RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}
run: |
echo "Smoke tests failed — rolling back Railway backend..."
railway rollback --service nova-rewards-backend --environment staging || true
echo "Rollback triggered."
- name: Notify rollback
uses: actions/github-script@v9
with:
script: |
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: context.sha,
state: 'failure',
description: 'Staging deploy failed smoke tests — rolled back',
context: 'cd/staging',
});
# ──────────────────────────────────────────────────────────────────────────
# 5. Report success back to GitHub
# ──────────────────────────────────────────────────────────────────────────
report-success:
name: Report Deployment Status
runs-on: ubuntu-latest
needs: smoke-tests
if: success()
steps:
- uses: actions/github-script@v9
with:
script: |
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: context.sha,
state: 'success',
description: 'Staging deploy passed all smoke tests',
context: 'cd/staging',
target_url: '${{ needs.deploy-frontend.outputs.preview_url || secrets.STAGING_BACKEND_URL }}',
});