-
Notifications
You must be signed in to change notification settings - Fork 0
238 lines (217 loc) · 8.73 KB
/
Copy pathdeploy-prod.yml
File metadata and controls
238 lines (217 loc) · 8.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
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
name: Deploy to Production
on:
push:
branches:
- "release/**"
workflow_dispatch:
inputs:
sha:
description: Commit SHA to deploy (defaults to HEAD of current branch)
required: false
type: string
concurrency:
group: deploy-production
cancel-in-progress: false
jobs:
# ── Detect what changed ───────────────────────────────────────────────────────
filter:
name: Detect changes
runs-on: ubuntu-latest
outputs:
api: ${{ steps.changes.outputs.api }}
dashboard: ${{ steps.changes.outputs.dashboard }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.inputs.sha || github.sha }}
- uses: dorny/paths-filter@v3
id: changes
with:
filters: |
api:
- 'platform/apps/api/**'
- 'platform/packages/db/**'
- 'platform/packages/shared/**'
- 'platform/packages/azure/**'
- 'platform/packages/config/**'
dashboard:
- 'platform/apps/dashboard/**'
- 'platform/packages/shared/**'
- 'platform/packages/config/**'
# ── Build & push platform API ─────────────────────────────────────────────────
# Images are built BEFORE the manual approval gate so reviewers see what they approve.
build-api:
name: Build API image
needs: filter
if: needs.filter.outputs.api == 'true'
uses: ./.github/workflows/_docker-build-push.yml
with:
image-name: platform-api
dockerfile: apps/api/Dockerfile
context: platform
acr-login-server: deploycloudproductionacr.azurecr.io
extra-tags: stable
secrets:
AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
# ── Build & push platform Dashboard ──────────────────────────────────────────
build-dashboard:
name: Build Dashboard image
needs: filter
if: needs.filter.outputs.dashboard == 'true'
uses: ./.github/workflows/_docker-build-push.yml
with:
image-name: platform-dashboard
dockerfile: apps/dashboard/Dockerfile
context: platform
acr-login-server: deploycloudproductionacr.azurecr.io
extra-tags: stable
build-args: |
NEXT_PUBLIC_API_URL=${{ secrets.NEXT_PUBLIC_API_URL }}
NEXT_PUBLIC_APP_NAME=${{ secrets.NEXT_PUBLIC_APP_NAME }}
secrets:
AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
# ── Update Container Apps — manual approval gate enforced by GitHub Environment ─
deploy-platform:
name: Deploy to Container Apps (production)
needs: [build-api, build-dashboard]
if: always() && !failure() && !cancelled()
runs-on: ubuntu-latest
environment: production # ← reviewer must approve before this job runs
permissions:
id-token: write
contents: read
steps:
- name: Azure Login
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
use-oidc: true
- name: Update API Container App
if: needs.build-api.result == 'success'
run: |
SHA_SHORT="${GITHUB_SHA::8}"
az containerapp update \
--name deploycloud-production-api \
--resource-group deploycloud-production-rg \
--image deploycloudproductionacr.azurecr.io/platform-api:sha-${SHA_SHORT}
- name: Update Dashboard Container App
if: needs.build-dashboard.result == 'success'
run: |
SHA_SHORT="${GITHUB_SHA::8}"
az containerapp update \
--name deploycloud-production-dashboard \
--resource-group deploycloud-production-rg \
--image deploycloudproductionacr.azurecr.io/platform-dashboard:sha-${SHA_SHORT}
- name: Deployment summary
run: |
SHA_SHORT="${GITHUB_SHA::8}"
{
echo "### Production Deployment — \`sha-${SHA_SHORT}\`"
echo "| Service | Result |"
echo "|---------|--------|"
echo "| API | ${{ needs.build-api.result }} |"
echo "| Dashboard | ${{ needs.build-dashboard.result }} |"
} >> "$GITHUB_STEP_SUMMARY"
# ── Run DB migrations ─────────────────────────────────────────────────────────
migrate:
name: Run DB Migrations
needs: [build-api, deploy-platform]
if: needs.build-api.result == 'success'
runs-on: ubuntu-latest
environment: production
permissions:
id-token: write
contents: read
steps:
- name: Azure Login
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
use-oidc: true
- name: Create ephemeral migration job
run: |
SHA_SHORT="${GITHUB_SHA::8}"
JOB_NAME="deploycloud-production-migrate-${SHA_SHORT}"
echo "JOB_NAME=${JOB_NAME}" >> "$GITHUB_ENV"
IDENTITY_ID=$(az identity show \
--name deploycloud-production-platform-id \
--resource-group deploycloud-production-rg \
--query id -o tsv)
az containerapp job create \
--name "${JOB_NAME}" \
--resource-group deploycloud-production-rg \
--environment deploycloud-production-cae \
--trigger-type Manual \
--replica-timeout 600 \
--replica-retry-limit 1 \
--replica-completion-count 1 \
--parallelism 1 \
--image "deploycloudproductionacr.azurecr.io/platform-api:sha-${SHA_SHORT}" \
--registry-server deploycloudproductionacr.azurecr.io \
--registry-identity "${IDENTITY_ID}" \
--env-vars "NODE_ENV=production" \
--command "node" \
--args "apps/api/dist/migrate.js"
- name: Start migration job
run: |
EXECUTION_NAME=$(az containerapp job start \
--name "${JOB_NAME}" \
--resource-group deploycloud-production-rg \
--query name -o tsv)
echo "EXECUTION_NAME=${EXECUTION_NAME}" >> "$GITHUB_ENV"
echo "Migration execution started: ${EXECUTION_NAME}"
- name: Wait for migration to complete
timeout-minutes: 15
run: |
for i in $(seq 1 30); do
STATUS=$(az containerapp job execution show \
--name "${JOB_NAME}" \
--resource-group deploycloud-production-rg \
--job-execution-name "${EXECUTION_NAME}" \
--query "properties.status" -o tsv)
echo "Status: ${STATUS} (attempt ${i}/30)"
if [ "${STATUS}" = "Succeeded" ]; then
echo "Migration completed successfully"
exit 0
elif [ "${STATUS}" = "Failed" ]; then
echo "Migration job failed"
exit 1
fi
sleep 20
done
echo "Migration timed out"
exit 1
- name: Cleanup migration job
if: always()
run: |
az containerapp job delete \
--name "${JOB_NAME}" \
--resource-group deploycloud-production-rg \
--yes 2>/dev/null || true
# ── Smoke test ────────────────────────────────────────────────────────────────
smoke-test:
name: Smoke Test (production)
needs: [deploy-platform, migrate]
if: always() && needs.deploy-platform.result == 'success'
runs-on: ubuntu-latest
steps:
- name: Wait for Container Apps to settle
run: sleep 30
- name: Health check — API
run: |
STATUS=$(curl -s -o /dev/null -w "%{http_code}" \
--retry 5 --retry-delay 15 --retry-connrefused \
"${{ vars.PROD_API_URL }}/health")
if [ "$STATUS" != "200" ]; then
echo "Health check returned HTTP ${STATUS}"
exit 1
fi
echo "Production API is healthy (HTTP ${STATUS})"