forked from slopus/happy-server
-
Notifications
You must be signed in to change notification settings - Fork 0
282 lines (248 loc) · 12.5 KB
/
deploy.yml
File metadata and controls
282 lines (248 loc) · 12.5 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
name: Build and Deploy
on:
workflow_dispatch:
env:
ACR_NAME: mxaesharedservicesacr
RESOURCE_GROUP: mx-ae-prod-happyserver-rg
CUSTOM_DOMAIN: happy.makerx.tech
jobs:
build:
name: Build Docker Image
runs-on: ubuntu-latest
environment: prod
permissions:
id-token: write
contents: read
outputs:
image-tag: ${{ github.sha }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Azure
uses: azure/login@v2
with:
client-id: ${{ vars.AZURE_CLIENT_ID }}
tenant-id: ${{ vars.AZURE_TENANT_ID }}
subscription-id: ${{ vars.AZURE_SUBSCRIPTION_ID }}
- name: Login to Azure Container Registry
run: |
az acr login --name ${{ env.ACR_NAME }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: |
${{ env.ACR_NAME }}.azurecr.io/happy-server:${{ github.sha }}
${{ env.ACR_NAME }}.azurecr.io/happy-server:latest
cache-from: type=gha
cache-to: type=gha,mode=max
deploy:
name: Deploy to Azure
needs: build
if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
runs-on: ubuntu-latest
environment: prod
permissions:
id-token: write
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Azure Login
uses: azure/login@v2
with:
client-id: ${{ vars.AZURE_CLIENT_ID }}
tenant-id: ${{ vars.AZURE_TENANT_ID }}
subscription-id: ${{ vars.AZURE_SUBSCRIPTION_ID }}
- name: Deploy Infrastructure
id: deploy
run: |
az deployment group create \
--name "happy-server-${{ github.sha }}" \
--resource-group "${{ env.RESOURCE_GROUP }}" \
--template-file ./infra/main.bicep \
--parameters \
environmentName=prod \
location=australiaeast \
postgresPassword="${{ secrets.POSTGRES_PASSWORD }}" \
serverSeed="${{ secrets.SERVER_SEED }}" \
minioPassword="${{ secrets.MINIO_PASSWORD }}" \
handyMasterSecret="${{ secrets.HANDY_MASTER_SECRET }}" \
imageTag="${{ github.sha }}" \
acrName="${{ env.ACR_NAME }}"
# Get postgres FQDN from deployment output
POSTGRES_FQDN=$(az deployment group show \
--name "happy-server-${{ github.sha }}" \
--resource-group "${{ env.RESOURCE_GROUP }}" \
--query "properties.outputs.postgresServerFqdn.value" -o tsv)
echo "postgres-fqdn=$POSTGRES_FQDN" >> $GITHUB_OUTPUT
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'yarn'
- name: Install Dependencies
run: yarn install --frozen-lockfile
- name: Run Database Migrations
run: |
# URL-encode the password for the connection string
ENCODED_PASSWORD=$(python3 -c "import urllib.parse, os; print(urllib.parse.quote(os.environ['RAW_PASSWORD'], safe=''))")
export DATABASE_URL="postgresql://happyadmin:${ENCODED_PASSWORD}@${{ steps.deploy.outputs.postgres-fqdn }}:5432/happy?sslmode=require"
npx prisma migrate deploy
env:
RAW_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
- name: Get App URL and Domain Verification ID
id: get-url
run: |
APP_FQDN=$(az containerapp show \
--name happy-prod-server \
--resource-group ${{ env.RESOURCE_GROUP }} \
--query "properties.configuration.ingress.fqdn" \
--output tsv)
echo "app-url=https://$APP_FQDN" >> $GITHUB_OUTPUT
echo "app-fqdn=$APP_FQDN" >> $GITHUB_OUTPUT
VERIFICATION_ID=$(az containerapp show \
--name happy-prod-server \
--resource-group ${{ env.RESOURCE_GROUP }} \
--query "properties.customDomainVerificationId" \
--output tsv)
echo "domain-verification-id=$VERIFICATION_ID" >> $GITHUB_OUTPUT
- name: Show Container App Status
if: always()
run: |
echo "=== Server Container App Details ==="
az containerapp show \
--name happy-prod-server \
--resource-group ${{ env.RESOURCE_GROUP }} \
--query "{provisioningState:properties.provisioningState, runningStatus:properties.runningStatus, latestRevision:properties.latestRevisionName}" \
-o table
echo ""
echo "=== Server Latest Revision Status ==="
REVISION=$(az containerapp show \
--name happy-prod-server \
--resource-group ${{ env.RESOURCE_GROUP }} \
--query "properties.latestRevisionName" -o tsv)
az containerapp revision show \
--name happy-prod-server \
--resource-group ${{ env.RESOURCE_GROUP }} \
--revision "$REVISION" \
--query "{name:name, active:properties.active, replicas:properties.replicas, healthState:properties.healthState, provisioningState:properties.provisioningState, runningState:properties.runningState}" \
-o table
echo ""
echo "=== MinIO Container App Details ==="
az containerapp show \
--name happy-prod-minio \
--resource-group ${{ env.RESOURCE_GROUP }} \
--query "{provisioningState:properties.provisioningState, runningStatus:properties.runningStatus, latestRevision:properties.latestRevisionName, ingressFqdn:properties.configuration.ingress.fqdn}" \
-o table || echo "Could not get MinIO status"
echo ""
echo "=== MinIO Latest Revision Status ==="
MINIO_REVISION=$(az containerapp show \
--name happy-prod-minio \
--resource-group ${{ env.RESOURCE_GROUP }} \
--query "properties.latestRevisionName" -o tsv) || echo ""
if [ -n "$MINIO_REVISION" ]; then
az containerapp revision show \
--name happy-prod-minio \
--resource-group ${{ env.RESOURCE_GROUP }} \
--revision "$MINIO_REVISION" \
--query "{name:name, active:properties.active, replicas:properties.replicas, healthState:properties.healthState, provisioningState:properties.provisioningState, runningState:properties.runningState}" \
-o table || echo "Could not get MinIO revision status"
fi
- name: Show Container Logs
if: always()
run: |
echo "=== Container Logs (last 100 lines) ==="
az containerapp logs show \
--name happy-prod-server \
--resource-group ${{ env.RESOURCE_GROUP }} \
--tail 100 \
--follow false || echo "Could not retrieve logs"
- name: Show System Logs
if: always()
run: |
echo "=== System Logs ==="
az containerapp logs show \
--name happy-prod-server \
--resource-group ${{ env.RESOURCE_GROUP }} \
--type system \
--tail 50 \
--follow false || echo "Could not retrieve system logs"
- name: Health Check
run: |
echo "Waiting for deployment to stabilize..."
sleep 10
for i in {1..5}; do
echo "Attempt $i: Checking ${{ steps.get-url.outputs.app-url }}/health"
HTTP_CODE=$(curl -s -m 5 -o /tmp/health_response.txt -w "%{http_code}" "${{ steps.get-url.outputs.app-url }}/health" || echo "000")
echo "HTTP Status: $HTTP_CODE"
if [ -f /tmp/health_response.txt ]; then
echo "Response body:"
cat /tmp/health_response.txt
echo ""
fi
if [ "$HTTP_CODE" = "200" ]; then
echo "Health check passed!"
exit 0
fi
echo "Retrying in 5s..."
sleep 5
done
echo "Health check failed after 5 attempts"
exit 1
- name: Configure Custom Domain
run: |
# Check if hostname is already bound
EXISTING=$(az containerapp hostname list \
--name happy-prod-server \
--resource-group ${{ env.RESOURCE_GROUP }} \
--query "[?name=='${{ env.CUSTOM_DOMAIN }}'].name" -o tsv)
if [ -n "$EXISTING" ]; then
echo "Custom domain ${{ env.CUSTOM_DOMAIN }} is already configured"
else
echo "Adding custom domain ${{ env.CUSTOM_DOMAIN }}..."
az containerapp hostname add \
--hostname ${{ env.CUSTOM_DOMAIN }} \
--name happy-prod-server \
--resource-group ${{ env.RESOURCE_GROUP }}
echo "Binding managed certificate..."
az containerapp hostname bind \
--hostname ${{ env.CUSTOM_DOMAIN }} \
--name happy-prod-server \
--resource-group ${{ env.RESOURCE_GROUP }} \
--environment happy-prod-env \
--validation-method CNAME
fi
- name: Debug on Failure
if: failure()
run: |
echo "=== DEBUGGING FAILED DEPLOYMENT ==="
echo ""
echo "=== Environment Variables Configured ==="
az containerapp show \
--name happy-prod-server \
--resource-group ${{ env.RESOURCE_GROUP }} \
--query "properties.template.containers[0].env[].name" -o tsv
echo ""
echo "=== Container Image ==="
az containerapp show \
--name happy-prod-server \
--resource-group ${{ env.RESOURCE_GROUP }} \
--query "properties.template.containers[0].image" -o tsv
echo ""
echo "=== Recent Container Logs (extended) ==="
az containerapp logs show \
--name happy-prod-server \
--resource-group ${{ env.RESOURCE_GROUP }} \
--tail 200 \
--follow false || echo "Could not retrieve logs"
- name: Deployment Summary
run: |
echo "## Deployment Complete :rocket:" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Server URL:** https://${{ env.CUSTOM_DOMAIN }}" >> $GITHUB_STEP_SUMMARY
echo "**Azure URL:** ${{ steps.get-url.outputs.app-url }}" >> $GITHUB_STEP_SUMMARY
echo "**Image Tag:** \`${{ github.sha }}\`" >> $GITHUB_STEP_SUMMARY