-
Notifications
You must be signed in to change notification settings - Fork 0
354 lines (302 loc) · 11.2 KB
/
Copy pathdeploy.yml
File metadata and controls
354 lines (302 loc) · 11.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
name: Deploy to AWS
on:
push:
branches: [main]
workflow_dispatch:
inputs:
environment:
description: 'Environment to deploy'
required: true
default: 'prod'
type: choice
options:
- prod
- staging
# All runs of this workflow (push or manual dispatch, any environment input)
# deploy to the same Lightsail service and S3 bucket, so every run must be
# serialized behind a single group; queue instead of cancelling.
concurrency:
group: aws-deploy
cancel-in-progress: false
env:
AWS_REGION: us-east-1
APP_NAME: metamate
LIGHTSAIL_POWER: medium
LIGHTSAIL_SCALE: 1
jobs:
# Test Backend
test-backend:
name: Test Backend
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Set up Python
uses: actions/setup-python@v6
with:
# Pinned to match backend/Dockerfile's base image patch version
python-version: '3.11.15'
- name: Install uv
uses: astral-sh/setup-uv@v7
with:
# Pinned to match backend/Dockerfile; upgrade deliberately via PR
version: "0.11.21"
- name: Install dependencies
working-directory: backend
run: |
uv sync
- name: Run linting
working-directory: backend
run: |
uv run ruff check .
uv run ruff format --check .
# Uncomment when tests are added
# - name: Run tests
# working-directory: backend
# run: uv run pytest
# Test Frontend
test-frontend:
name: Test Frontend
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: pnpm/action-setup@v6
with:
version: "10.34.3"
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: '22'
cache: 'pnpm'
cache-dependency-path: frontend/pnpm-lock.yaml
- name: Install dependencies
working-directory: frontend
run: pnpm install --frozen-lockfile
- name: Run linting
working-directory: frontend
run: |
pnpm lint
pnpm prettier:check
- name: Type check
working-directory: frontend
run: pnpm typecheck
# Uncomment when tests are added
# - name: Run tests
# working-directory: frontend
# run: pnpm test
# Deploy Backend
deploy-backend:
name: Deploy Backend
runs-on: ubuntu-latest
needs: test-backend
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v6
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v6
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Ensure Lightsail capacity tier
run: |
SERVICE_NAME="${{ env.APP_NAME }}-backend"
CURRENT_SCALE=$(aws lightsail get-container-services \
--region ${{ env.AWS_REGION }} \
--service-name ${SERVICE_NAME} \
--query 'containerServices[0].scale' \
--output text 2>/dev/null || echo "")
if [ -z "$CURRENT_SCALE" ] || [ "$CURRENT_SCALE" = "None" ]; then
CURRENT_SCALE="${{ env.LIGHTSAIL_SCALE }}"
fi
aws lightsail update-container-service \
--region ${{ env.AWS_REGION }} \
--service-name ${SERVICE_NAME} \
--power ${{ env.LIGHTSAIL_POWER }} \
--scale ${CURRENT_SCALE}
- name: Install Lightsail plugin
run: |
curl "https://s3.us-west-2.amazonaws.com/lightsailctl/latest/linux-amd64/lightsailctl" -o "/usr/local/bin/lightsailctl"
sudo chmod +x /usr/local/bin/lightsailctl
- name: Build Docker image
run: |
docker build -f backend/Dockerfile -t ${{ env.APP_NAME }}-backend:${{ github.sha }} .
docker tag ${{ env.APP_NAME }}-backend:${{ github.sha }} ${{ env.APP_NAME }}-backend:latest
- name: Push to Lightsail
run: |
# Get container service name
SERVICE_NAME="${{ env.APP_NAME }}-backend"
# Push image
aws lightsail push-container-image \
--region ${{ env.AWS_REGION }} \
--service-name ${SERVICE_NAME} \
--label backend-${{ github.sha }} \
--image ${{ env.APP_NAME }}-backend:${{ github.sha }}
# Get the pushed image name
IMAGE=$(aws lightsail get-container-images \
--region ${{ env.AWS_REGION }} \
--service-name ${SERVICE_NAME} \
--query 'containerImages[0].image' \
--output text)
echo "IMAGE=${IMAGE}" >> $GITHUB_ENV
- name: Deploy container
run: |
SERVICE_NAME="${{ env.APP_NAME }}-backend"
# Create container configuration
cat > containers.json <<EOF
{
"api": {
"image": "${{ env.IMAGE }}",
"ports": {
"8000": "HTTP"
},
"environment": {
"PORT": "8000",
"AWS_REGION": "${{ env.AWS_REGION }}",
"ENVIRONMENT": "${{ github.event.inputs.environment || 'prod' }}",
"LLM_API_KEY": "${{ secrets.LLM_API_KEY }}",
"LLM_API_URL": "${{ secrets.LLM_API_URL }}",
"LLM_MODEL": "${{ secrets.LLM_MODEL }}",
"BACKUP_LLM_API_KEY": "${{ secrets.BACKUP_LLM_API_KEY }}",
"MINERU_API_KEY": "${{ secrets.MINERU_API_KEY }}",
"AWS_S3_TEMP_BUCKET": "${{ secrets.AWS_S3_TEMP_BUCKET }}",
"CORS_ORIGINS": "https://${{ secrets.CUSTOM_DOMAIN }},https://www.${{ secrets.CUSTOM_DOMAIN }}"
}
}
}
EOF
# Create endpoint configuration
cat > endpoint.json <<EOF
{
"containerName": "api",
"containerPort": 8000,
"healthCheck": {
"healthyThreshold": 2,
"unhealthyThreshold": 2,
"timeoutSeconds": 5,
"intervalSeconds": 30,
"path": "/health",
"successCodes": "200-299"
}
}
EOF
# Deploy
aws lightsail create-container-service-deployment \
--region ${{ env.AWS_REGION }} \
--service-name ${SERVICE_NAME} \
--containers file://containers.json \
--public-endpoint file://endpoint.json
- name: Wait for deployment
run: |
SERVICE_NAME="${{ env.APP_NAME }}-backend"
for i in {1..60}; do
STATE=$(aws lightsail get-container-services \
--region ${{ env.AWS_REGION }} \
--service-name ${SERVICE_NAME} \
--query 'containerServices[0].state' \
--output text)
if [ "$STATE" == "READY" ] || [ "$STATE" == "RUNNING" ]; then
echo "Deployment successful (State: $STATE)"
exit 0
elif [ "$STATE" == "DEPLOYING" ]; then
echo "Still deploying... (attempt $i/60)"
sleep 10
else
echo "Deployment failed. State: $STATE"
exit 1
fi
done
echo "Deployment timeout"
exit 1
# Deploy Frontend
deploy-frontend:
name: Deploy Frontend
runs-on: ubuntu-latest
needs: [test-frontend, deploy-backend]
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v6
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v6
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- uses: pnpm/action-setup@v6
with:
version: "10.34.3"
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: '22'
cache: 'pnpm'
cache-dependency-path: frontend/pnpm-lock.yaml
- name: Get backend URL and build frontend
run: |
SERVICE_NAME="${{ env.APP_NAME }}-backend"
BACKEND_URL=$(aws lightsail get-container-services \
--region ${{ env.AWS_REGION }} \
--service-name ${SERVICE_NAME} \
--query 'containerServices[0].url' \
--output text | sed 's:/*$::')
# Use custom domain if available
if [ "${{ secrets.CUSTOM_DOMAIN }}" != "" ]; then
API_URL="https://api.${{ secrets.CUSTOM_DOMAIN }}"
else
API_URL="${BACKEND_URL}"
fi
echo "Backend URL: ${BACKEND_URL}"
echo "Using API URL: ${API_URL}"
# Install dependencies
cd frontend
pnpm install --frozen-lockfile
# Build with the correct API URL
export NEXT_PUBLIC_API_URL="${API_URL}"
echo "Building frontend with NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}"
pnpm build
- name: Deploy to S3
working-directory: frontend
run: |
# Get bucket name from secrets or construct it
BUCKET_NAME="${{ secrets.FRONTEND_BUCKET_NAME }}"
if [ -z "$BUCKET_NAME" ]; then
BUCKET_NAME="${{ env.APP_NAME }}-frontend-${{ secrets.BUCKET_SUFFIX }}"
fi
# Sync with cache headers for assets
aws s3 sync out/ s3://${BUCKET_NAME}/ \
--delete \
--cache-control "public, max-age=31536000, immutable" \
--exclude "*.html" \
--exclude "_next/data/*" \
--exclude "_next/static/chunks/app/*"
# Sync HTML files with no-cache
aws s3 sync out/ s3://${BUCKET_NAME}/ \
--delete \
--cache-control "public, max-age=0, must-revalidate" \
--exclude "*" \
--include "*.html" \
--include "_next/data/*" \
--include "_next/static/chunks/app/*"
- name: Invalidate CloudFront
env:
DISTRIBUTION_ID: ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }}
if: env.DISTRIBUTION_ID != ''
run: |
aws cloudfront create-invalidation \
--distribution-id ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} \
--paths "/*"
# Notify deployment status
notify:
name: Notify Deployment
runs-on: ubuntu-latest
needs: [deploy-backend, deploy-frontend]
if: always()
steps:
- name: Send notification
run: |
if [ "${{ needs.deploy-backend.result }}" == "success" ] && [ "${{ needs.deploy-frontend.result }}" == "success" ]; then
echo "✅ Deployment successful!"
# Add Slack/Discord/Email notification here
else
echo "❌ Deployment failed!"
# Add Slack/Discord/Email notification here
fi