-
Notifications
You must be signed in to change notification settings - Fork 29
156 lines (135 loc) · 5.73 KB
/
Copy pathdeploy.yml
File metadata and controls
156 lines (135 loc) · 5.73 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
name: Deploy to Cloud Run
on:
push:
branches: [main]
workflow_dispatch:
inputs:
environment:
description: "Deployment environment"
required: true
default: "production"
type: choice
options:
- production
- staging
concurrency:
group: deploy-${{ github.ref }}
cancel-in-progress: true
env:
PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
REGION: us-central1
SERVICE_NAME: crypto-vision
VPC_CONNECTOR: crypto-vision-vpc
SERVICE_ACCOUNT: crypto-vision-run
jobs:
# ── Quality Gates ────────────────────────────────────────────
test:
name: Test & Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- name: Install dependencies
run: npm ci
- name: Typecheck
run: npm run typecheck
- name: Lint
run: npm run lint
- name: Test
run: npm run test
- name: Build
run: npm run build
# ── Deploy to Cloud Run ────────────────────────────────────
deploy:
name: Deploy
runs-on: ubuntu-latest
needs: test
if: github.ref == 'refs/heads/main'
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
# Authenticate to GCP via Workload Identity Federation (preferred)
# Fallback: use service account key JSON
- id: auth
uses: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ secrets.GCP_SA_EMAIL }}
# Alternative: credentials_json: ${{ secrets.GCP_SA_KEY }}
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v2
with:
project_id: ${{ env.PROJECT_ID }}
- name: Configure Docker for Artifact Registry
run: gcloud auth configure-docker ${{ env.REGION }}-docker.pkg.dev --quiet
- name: Build container image
run: |
docker build \
-t ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/crypto-vision/${{ env.SERVICE_NAME }}:${{ github.sha }} \
-t ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/crypto-vision/${{ env.SERVICE_NAME }}:latest \
.
- name: Push container image
run: |
docker push ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/crypto-vision/${{ env.SERVICE_NAME }}:${{ github.sha }}
docker push ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/crypto-vision/${{ env.SERVICE_NAME }}:latest
- name: Deploy to Cloud Run
run: |
gcloud run deploy ${{ env.SERVICE_NAME }} \
--image=${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/crypto-vision/${{ env.SERVICE_NAME }}:${{ github.sha }} \
--region=${{ env.REGION }} \
--platform=managed \
--no-allow-unauthenticated \
--port=8080 \
--memory=2Gi \
--cpu=4 \
--min-instances=2 \
--max-instances=500 \
--timeout=60s \
--concurrency=250 \
--cpu-boost \
--execution-environment=gen2 \
--vpc-connector=projects/${{ env.PROJECT_ID }}/locations/${{ env.REGION }}/connectors/${{ env.VPC_CONNECTOR }} \
--vpc-egress=private-ranges-only \
--service-account=${{ env.SERVICE_ACCOUNT }}@${{ env.PROJECT_ID }}.iam.gserviceaccount.com \
--set-env-vars=NODE_ENV=production,COINGECKO_PRO=true \
--set-secrets=COINGECKO_API_KEY=COINGECKO_API_KEY:latest,GROQ_API_KEY=GROQ_API_KEY:latest,GEMINI_API_KEY=GEMINI_API_KEY:latest,OPENAI_API_KEY=OPENAI_API_KEY:latest,ANTHROPIC_API_KEY=ANTHROPIC_API_KEY:latest,OPENROUTER_API_KEY=OPENROUTER_API_KEY:latest,REDIS_URL=REDIS_URL:latest \
--quiet
- name: Verify deployment
run: |
SERVICE_URL=$(gcloud run services describe ${{ env.SERVICE_NAME }} \
--region=${{ env.REGION }} \
--format='value(status.url)')
echo "Service URL: ${SERVICE_URL}"
# Authenticated health check with retries (internal service)
TOKEN=$(gcloud auth print-identity-token --audiences="${SERVICE_URL}")
for i in 1 2 3 4 5; do
STATUS=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: Bearer ${TOKEN}" "${SERVICE_URL}/health" || true)
if [ "$STATUS" = "200" ]; then
echo "✓ Health check passed"
curl -s -H "Authorization: Bearer ${TOKEN}" "${SERVICE_URL}/health" | head -c 500
exit 0
fi
echo "Attempt ${i}/5 — status ${STATUS}, retrying in 10s..."
sleep 10
done
echo "✗ Health check failed after 5 attempts"
exit 1
- name: Post deployment URL
if: success()
run: |
SERVICE_URL=$(gcloud run services describe ${{ env.SERVICE_NAME }} \
--region=${{ env.REGION }} \
--format='value(status.url)')
echo "### 🚀 Deployed to Cloud Run" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "| Field | Value |" >> $GITHUB_STEP_SUMMARY
echo "|-------|-------|" >> $GITHUB_STEP_SUMMARY
echo "| **Service** | ${{ env.SERVICE_NAME }} |" >> $GITHUB_STEP_SUMMARY
echo "| **URL** | ${SERVICE_URL} |" >> $GITHUB_STEP_SUMMARY
echo "| **Image** | \`${{ github.sha }}\` |" >> $GITHUB_STEP_SUMMARY
echo "| **Region** | ${{ env.REGION }} |" >> $GITHUB_STEP_SUMMARY