-
Notifications
You must be signed in to change notification settings - Fork 0
485 lines (401 loc) · 15.7 KB
/
deploy.yml
File metadata and controls
485 lines (401 loc) · 15.7 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
name: Deploy to GCP Cloud Run
on:
workflow_dispatch:
inputs:
skip_staging:
description: 'Skip staging and deploy directly to production'
required: false
default: false
type: boolean
push:
branches:
- main
paths:
- "src/**"
- "docs/**"
- ".github/workflows/deploy.yml"
- "terraform/**"
- "alembic/**"
- "Dockerfile"
- "pyproject.toml"
- "uv.lock"
permissions:
id-token: write
contents: write
concurrency:
group: deploy-main
cancel-in-progress: false
jobs:
# ── Stage 1: Test ──────────────────────────────────────────────
test:
name: Test
runs-on: ubuntu-latest
if: ${{ github.event_name != 'push' || github.event.head_commit.message != 'Update package version' }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v5
- name: Setup Python
run: uv python install 3.13
- name: Sync dependencies
run: uv sync --extra dev
- name: Run tests
run: uv run pytest -v -m "not integration and not staging"
# ── Stage 2: Build + Migrate + Infra (parallel) ───────────────
migrate-staging:
name: Migrate staging database
runs-on: ubuntu-latest
needs: test
if: ${{ !inputs.skip_staging }}
environment: staging
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v5
- name: Setup Python
run: uv python install 3.13
- name: Sync dependencies
run: uv sync
- name: Run database migrations
env:
SUPABASE_DB_URL: ${{ secrets.SUPABASE_DB_URL }}
run: uv run alembic upgrade head
migrate-production:
name: Migrate production database
runs-on: ubuntu-latest
needs: test
environment: production
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v5
- name: Setup Python
run: uv python install 3.13
- name: Sync dependencies
run: uv sync
- name: Run database migrations
env:
SUPABASE_DB_URL: ${{ secrets.SUPABASE_DB_URL }}
run: uv run alembic upgrade head
build:
name: Build Docker image
runs-on: ubuntu-latest
needs: test
environment: production
env:
IMAGE_URL: ${{ vars.GCP_REGION }}-docker.pkg.dev/${{ vars.GCP_PROJECT_ID }}/${{ vars.PROJECT_NAME }}/${{ vars.PROJECT_NAME }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }}
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v2
- name: Configure Docker for Artifact Registry
run: gcloud auth configure-docker ${{ vars.GCP_REGION }}-docker.pkg.dev
- name: Build and push Docker image
run: |
docker build -t $IMAGE_URL:${{ github.sha }} .
docker tag $IMAGE_URL:${{ github.sha }} $IMAGE_URL:latest
docker push $IMAGE_URL:${{ github.sha }}
docker push $IMAGE_URL:latest
infra:
name: Apply infrastructure
runs-on: ubuntu-latest
needs: test
environment: production
env:
TF_IN_AUTOMATION: true
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }}
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.6.0
- name: Terraform init
working-directory: ./terraform
run: terraform init -input=false
- name: Terraform plan
working-directory: ./terraform
env:
TF_VAR_supabase_url: ${{ secrets.SUPABASE_URL }}
TF_VAR_supabase_key: ${{ secrets.SUPABASE_KEY }}
TF_VAR_supabase_secret_key: ${{ secrets.SUPABASE_SECRET_KEY }}
TF_VAR_supabase_db_url: ${{ secrets.SUPABASE_DB_URL }}
TF_VAR_logfire_token: ${{ secrets.LOGFIRE_TOKEN }}
TF_VAR_logfire_environment: prod
TF_VAR_modal_token_id: ${{ secrets.MODAL_TOKEN_ID }}
TF_VAR_modal_token_secret: ${{ secrets.MODAL_TOKEN_SECRET }}
TF_VAR_modal_environment: main
run: terraform plan -out=tfplan -input=false
- name: Terraform apply
working-directory: ./terraform
run: terraform apply -input=false tfplan
# ── Stage 3: Deploy to staging ─────────────────────────────────
setup-modal-environments:
name: Setup Modal environments
runs-on: ubuntu-latest
needs: test
if: ${{ !inputs.skip_staging }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v5
- name: Setup Python
run: uv python install 3.13
- name: Sync dependencies
run: uv sync
- name: Create staging environment
env:
MODAL_TOKEN_ID: ${{ secrets.MODAL_TOKEN_ID }}
MODAL_TOKEN_SECRET: ${{ secrets.MODAL_TOKEN_SECRET }}
run: |
chmod +x .github/scripts/*.sh
.github/scripts/modal-setup-environments.sh
deploy-staging-modal:
name: Deploy Modal to staging
runs-on: ubuntu-latest
needs: [migrate-staging, setup-modal-environments]
if: ${{ !inputs.skip_staging }}
environment: staging
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v5
- name: Setup Python
run: uv python install 3.13
- name: Sync dependencies
run: uv sync
- name: Sync secrets to staging
env:
MODAL_TOKEN_ID: ${{ secrets.MODAL_TOKEN_ID }}
MODAL_TOKEN_SECRET: ${{ secrets.MODAL_TOKEN_SECRET }}
SUPABASE_DB_URL: ${{ secrets.SUPABASE_DB_URL }}
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
SUPABASE_KEY: ${{ secrets.SUPABASE_KEY }}
SUPABASE_SECRET_KEY: ${{ secrets.SUPABASE_SECRET_KEY }}
LOGFIRE_TOKEN: ${{ secrets.LOGFIRE_TOKEN }}
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
chmod +x .github/scripts/*.sh
.github/scripts/modal-sync-secrets.sh staging staging
- name: Extract package versions
id: versions
run: |
chmod +x .github/scripts/modal-extract-versions.sh
.github/scripts/modal-extract-versions.sh .
- name: Deploy versioned Modal simulation app to staging
env:
MODAL_TOKEN_ID: ${{ secrets.MODAL_TOKEN_ID }}
MODAL_TOKEN_SECRET: ${{ secrets.MODAL_TOKEN_SECRET }}
POLICYENGINE_US_VERSION: ${{ steps.versions.outputs.us_version }}
POLICYENGINE_UK_VERSION: ${{ steps.versions.outputs.uk_version }}
run: |
chmod +x .github/scripts/modal-deploy-versioned.sh
.github/scripts/modal-deploy-versioned.sh staging
- name: Deploy agent sandbox to staging
env:
MODAL_TOKEN_ID: ${{ secrets.MODAL_TOKEN_ID }}
MODAL_TOKEN_SECRET: ${{ secrets.MODAL_TOKEN_SECRET }}
run: |
uv run modal deploy --env=staging src/policyengine_api/agent_sandbox.py
deploy-staging-cloudrun:
name: Deploy Cloud Run staging
runs-on: ubuntu-latest
needs: [build, infra, deploy-staging-modal]
if: ${{ !inputs.skip_staging }}
environment: staging
outputs:
staging_url: ${{ steps.get-staging-url.outputs.url }}
env:
IMAGE_URL: ${{ vars.GCP_REGION }}-docker.pkg.dev/${{ vars.GCP_PROJECT_ID }}/${{ vars.PROJECT_NAME }}/${{ vars.PROJECT_NAME }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }}
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v2
- name: Deploy staging revision (no traffic)
run: |
gcloud run deploy ${{ vars.API_SERVICE_NAME }} \
--region=${{ vars.GCP_REGION }} \
--image=$IMAGE_URL:${{ github.sha }} \
--tag=staging \
--no-traffic \
--update-env-vars=MODAL_ENVIRONMENT=staging,LOGFIRE_ENVIRONMENT=staging,SUPABASE_URL=${{ secrets.SUPABASE_URL }},SUPABASE_KEY=${{ secrets.SUPABASE_KEY }},SUPABASE_SECRET_KEY=${{ secrets.SUPABASE_SECRET_KEY }},SUPABASE_DB_URL=${{ secrets.SUPABASE_DB_URL }}
- name: Get staging URL
id: get-staging-url
run: |
STAGING_URL=$(gcloud run services describe ${{ vars.API_SERVICE_NAME }} \
--region=${{ vars.GCP_REGION }} \
--format=json | jq -r '.status.traffic[] | select(.tag=="staging") | .url')
echo "url=$STAGING_URL" >> "$GITHUB_OUTPUT"
echo "Staging URL: $STAGING_URL"
- name: Health check staging
run: |
chmod +x .github/scripts/*.sh
.github/scripts/health-check.sh "${{ steps.get-staging-url.outputs.url }}/health"
# ── Stage 4: Integration tests against staging ─────────────────
integration-tests:
name: Integration tests (staging)
runs-on: ubuntu-latest
needs: deploy-staging-cloudrun
if: ${{ !inputs.skip_staging }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v5
- name: Setup Python
run: uv python install 3.13
- name: Sync dependencies
run: uv sync --extra dev
- name: Run staging integration tests
env:
API_BASE_URL: ${{ needs.deploy-staging-cloudrun.outputs.staging_url }}
run: uv run pytest tests/test_staging_api.py -v
# ── Stage 5: Deploy to production ──────────────────────────────
deploy-prod-modal:
name: Deploy Modal to production
runs-on: ubuntu-latest
needs: [migrate-production, integration-tests]
if: |
always() &&
needs.migrate-production.result == 'success' &&
(needs.integration-tests.result == 'success' || needs.integration-tests.result == 'skipped')
environment: production
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v5
- name: Setup Python
run: uv python install 3.13
- name: Sync dependencies
run: uv sync
- name: Sync secrets to production
env:
MODAL_TOKEN_ID: ${{ secrets.MODAL_TOKEN_ID }}
MODAL_TOKEN_SECRET: ${{ secrets.MODAL_TOKEN_SECRET }}
SUPABASE_DB_URL: ${{ secrets.SUPABASE_DB_URL }}
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
SUPABASE_KEY: ${{ secrets.SUPABASE_KEY }}
SUPABASE_SECRET_KEY: ${{ secrets.SUPABASE_SECRET_KEY }}
LOGFIRE_TOKEN: ${{ secrets.LOGFIRE_TOKEN }}
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
chmod +x .github/scripts/*.sh
.github/scripts/modal-sync-secrets.sh main prod
- name: Extract package versions
id: prod-versions
run: |
chmod +x .github/scripts/modal-extract-versions.sh
.github/scripts/modal-extract-versions.sh .
- name: Deploy versioned Modal simulation app to production
env:
MODAL_TOKEN_ID: ${{ secrets.MODAL_TOKEN_ID }}
MODAL_TOKEN_SECRET: ${{ secrets.MODAL_TOKEN_SECRET }}
POLICYENGINE_US_VERSION: ${{ steps.prod-versions.outputs.us_version }}
POLICYENGINE_UK_VERSION: ${{ steps.prod-versions.outputs.uk_version }}
run: |
chmod +x .github/scripts/modal-deploy-versioned.sh
.github/scripts/modal-deploy-versioned.sh main
- name: Deploy agent sandbox to production
env:
MODAL_TOKEN_ID: ${{ secrets.MODAL_TOKEN_ID }}
MODAL_TOKEN_SECRET: ${{ secrets.MODAL_TOKEN_SECRET }}
run: |
uv run modal deploy --env=main src/policyengine_api/agent_sandbox.py
- name: Validate Modal secrets
env:
MODAL_TOKEN_ID: ${{ secrets.MODAL_TOKEN_ID }}
MODAL_TOKEN_SECRET: ${{ secrets.MODAL_TOKEN_SECRET }}
POLICYENGINE_US_VERSION: ${{ steps.prod-versions.outputs.us_version }}
POLICYENGINE_UK_VERSION: ${{ steps.prod-versions.outputs.uk_version }}
run: |
US_SAFE="${POLICYENGINE_US_VERSION//./-}"
UK_SAFE="${POLICYENGINE_UK_VERSION//./-}"
APP_NAME="policyengine-v2-us${US_SAFE}-uk${UK_SAFE}"
echo "Validating Modal secrets on ${APP_NAME}..."
result=$(uv run python -c "
import modal
f = modal.Function.from_name('${APP_NAME}', 'validate_secrets')
result = f.remote()
import json
print(json.dumps(result))
")
echo "$result"
if echo "$result" | grep -q '"status": "error"'; then
echo "::error::Modal secrets validation failed"
exit 1
fi
echo "Modal secrets validated"
deploy-prod-cloudrun:
name: Deploy to Cloud Run production
runs-on: ubuntu-latest
needs: [deploy-prod-modal, build, infra]
if: |
always() &&
needs.deploy-prod-modal.result == 'success' &&
needs.build.result == 'success' &&
needs.infra.result == 'success'
environment: production
env:
IMAGE_URL: ${{ vars.GCP_REGION }}-docker.pkg.dev/${{ vars.GCP_PROJECT_ID }}/${{ vars.PROJECT_NAME }}/${{ vars.PROJECT_NAME }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }}
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v2
- name: Deploy canary (no traffic)
run: |
gcloud run deploy ${{ vars.API_SERVICE_NAME }} \
--region=${{ vars.GCP_REGION }} \
--image=$IMAGE_URL:${{ github.sha }} \
--tag=canary \
--no-traffic \
--update-env-vars=MODAL_ENVIRONMENT=main,LOGFIRE_ENVIRONMENT=prod
- name: Smoke test canary
run: |
CANARY_URL=$(gcloud run services describe ${{ vars.API_SERVICE_NAME }} \
--region=${{ vars.GCP_REGION }} \
--format=json | jq -r '.status.traffic[] | select(.tag=="canary") | .url')
echo "Canary URL: $CANARY_URL"
chmod +x .github/scripts/*.sh
.github/scripts/health-check.sh "$CANARY_URL/health"
- name: Shift traffic to new revision
run: |
gcloud run services update-traffic ${{ vars.API_SERVICE_NAME }} \
--region=${{ vars.GCP_REGION }} \
--to-latest
- name: Get production URL
run: |
API_URL=$(gcloud run services describe ${{ vars.API_SERVICE_NAME }} \
--region=${{ vars.GCP_REGION }} \
--format='value(status.url)')
echo "API: $API_URL"
echo "Health: $API_URL/health"
echo "Docs: $API_URL/docs"