-
Notifications
You must be signed in to change notification settings - Fork 4.2k
498 lines (452 loc) · 19.1 KB
/
deploy.yml
File metadata and controls
498 lines (452 loc) · 19.1 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
486
487
488
489
490
491
492
493
494
495
496
497
498
name: Deploy to Novu Cloud
run-name: >
Deploying to
${{
github.event.inputs.deploy_api == 'true' && 'api, ' || ''
}}${{
github.event.inputs.deploy_worker == 'true' && 'worker, ' || ''
}}${{
github.event.inputs.deploy_ws == 'true' && 'ws, ' || ''
}}${{
github.event.inputs.deploy_webhook == 'true' && 'webhook ' || ''
}}on ${{ github.event.inputs.environment }}
description: |
This workflow deploys the Novu Cloud application to different environments and services based on the selected options.
It builds Docker images, pushes them to Amazon ECR, and deploys them to Amazon ECS.
Additionally, it creates Sentry releases and New Relic deployment markers.
env:
NX_CLOUD_ACCESS_TOKEN: ${{ secrets.NX_CLOUD_ACCESS_TOKEN }}
on:
workflow_dispatch:
inputs:
environment:
description: "Environment to deploy to"
required: true
type: choice
default: staging
options:
- staging
- staging-sg
- production-us
- production-eu
- production-sg
- production-au
- production-uk
- production-jp
- production-kr
- production-us-and-eu
- production-sg-au-uk-jp-kr
deploy_api:
description: "Deploy API"
required: true
type: boolean
default: true
deploy_worker:
description: "Deploy Worker"
required: true
type: boolean
default: false
deploy_ws:
description: "Deploy WS"
required: true
type: boolean
default: false
deploy_webhook:
description: "Deploy Webhook"
required: true
type: boolean
default: false
jobs:
prepare-matrix:
runs-on: ubuntu-latest
outputs:
env_matrix: ${{ steps.set-matrix.outputs.env_matrix }}
service_matrix: ${{ steps.set-matrix.outputs.service_matrix }}
deploy_matrix: ${{ steps.set-matrix.outputs.deploy_matrix }}
nr_matrix: ${{ steps.set-matrix.outputs.nr_matrix }}
steps:
- name: Validate Selected Services
run: |
if [ "${{ github.event.inputs.deploy_api }}" != "true" ] && \
[ "${{ github.event.inputs.deploy_worker }}" != "true" ] && \
[ "${{ github.event.inputs.deploy_ws }}" != "true" ] && \
[ "${{ github.event.inputs.deploy_webhook }}" != "true" ]; then
echo "Error: At least one service must be selected for deployment."
exit 1
fi
- name: Generate Environment, Service, and Deploy Matrices
id: set-matrix
env:
WORKER_SERVICE: ${{ vars.WORKER_SERVICE }}
API_SERVICE: ${{ vars.API_SERVICE }}
run: |
envs=()
services=()
deploy_matrix=()
nr=()
# Collect selected environments
if [ "${{ github.event.inputs.environment }}" == "staging" ]; then
envs+=("\"staging-eu\"")
fi
if [ "${{ github.event.inputs.environment }}" == "staging-sg" ]; then
envs+=("\"staging-apse1\"")
fi
if [ "${{ github.event.inputs.environment }}" == "production-us" ]; then
envs+=("\"prod-us\"")
fi
if [ "${{ github.event.inputs.environment }}" == "production-eu" ]; then
envs+=("\"prod-eu\"")
fi
if [ "${{ github.event.inputs.environment }}" == "production-sg" ]; then
envs+=("\"prod-apse1\"")
fi
if [ "${{ github.event.inputs.environment }}" == "production-au" ]; then
envs+=("\"prod-apse2\"")
fi
if [ "${{ github.event.inputs.environment }}" == "production-uk" ]; then
envs+=("\"prod-ew2\"")
fi
if [ "${{ github.event.inputs.environment }}" == "production-jp" ]; then
envs+=("\"prod-apne1\"")
fi
if [ "${{ github.event.inputs.environment }}" == "production-kr" ]; then
envs+=("\"prod-apne2\"")
fi
if [ "${{ github.event.inputs.environment }}" == "production-us-and-eu" ]; then
envs+=("\"prod-us\"")
envs+=("\"prod-eu\"")
fi
if [ "${{ github.event.inputs.environment }}" == "production-sg-au-uk-jp-kr" ]; then
envs+=("\"prod-apse1\"")
envs+=("\"prod-apse2\"")
envs+=("\"prod-ew2\"")
envs+=("\"prod-apne1\"")
envs+=("\"prod-apne2\"")
fi
# Collect selected services
if [ "${{ github.event.inputs.deploy_api }}" == "true" ]; then
services+=("\"api\"")
nr+=("\"api\"")
fi
if [ "${{ github.event.inputs.deploy_worker }}" == "true" ]; then
services+=("\"worker\"")
nr+=("\"worker\"")
fi
if [ "${{ github.event.inputs.deploy_ws }}" == "true" ]; then
services+=("\"ws\"")
fi
if [ "${{ github.event.inputs.deploy_webhook }}" == "true" ]; then
services+=("\"webhook\"")
fi
# Parse service secrets and generate deploy_matrix
for service in "${services[@]}"; do
if [ "$service" == "\"worker\"" ]; then
IFS=',' read -r -a worker_services <<< "$WORKER_SERVICE"
for worker_service in $(echo "$WORKER_SERVICE" | jq -c '.[]'); do
cluster_name=$(echo "$worker_service" | jq -r '.cluster_name')
container_name=$(echo "$worker_service" | jq -r '.container_name')
service_name=$(echo "$worker_service" | jq -r '.service')
task_name=$(echo "$worker_service" | jq -r '.task_name')
image=$(echo "$worker_service" | jq -r '.image')
# Check if service has environments filter, otherwise deploy to all
allowed_envs=$(echo "$worker_service" | jq -r '.environments // empty')
should_deploy=false
if [ -z "$allowed_envs" ]; then
# No environment filter, deploy to all environments
should_deploy=true
else
# Check if any of the selected environments match the allowed environments
for env in "${envs[@]}"; do
env_clean=$(echo "$env" | tr -d '"')
if echo "$allowed_envs" | jq -e --arg env "$env_clean" 'index($env) != null' > /dev/null; then
should_deploy=true
break
fi
done
fi
if [ "$should_deploy" == "true" ]; then
deploy_matrix+=("{\"cluster_name\": \"$cluster_name\", \"container_name\": \"$container_name\", \"service_name\": \"$service_name\", \"task_name\": \"$task_name\", \"image\": \"$image\"}")
fi
done
elif [ "$service" == "\"api\"" ]; then
for api_service in $(echo "$API_SERVICE" | jq -c '.[]'); do
cluster_name=$(echo "$api_service" | jq -r '.cluster_name')
container_name=$(echo "$api_service" | jq -r '.container_name')
service_name=$(echo "$api_service" | jq -r '.service')
task_name=$(echo "$api_service" | jq -r '.task_name')
image=$(echo "$api_service" | jq -r '.image')
# Check if service has environments filter, otherwise deploy to all
allowed_envs=$(echo "$api_service" | jq -r '.environments // empty')
should_deploy=false
if [ -z "$allowed_envs" ]; then
# No environment filter, deploy to all environments
should_deploy=true
else
# Check if any of the selected environments match the allowed environments
for env in "${envs[@]}"; do
env_clean=$(echo "$env" | tr -d '"')
if echo "$allowed_envs" | jq -e --arg env "$env_clean" 'index($env) != null' > /dev/null; then
should_deploy=true
break
fi
done
fi
if [ "$should_deploy" == "true" ]; then
deploy_matrix+=("{\"cluster_name\": \"$cluster_name\", \"container_name\": \"$container_name\", \"service_name\": \"$service_name\", \"task_name\": \"$task_name\", \"image\": \"$image\"}")
fi
done
elif [ "$service" == "\"ws\"" ]; then
cluster_name=ws-cluster
container_name=ws-container
service_name=ws-service
task_name=ws-task
image=ws
deploy_matrix+=("{\"cluster_name\": \"$cluster_name\", \"container_name\": \"$container_name\", \"service_name\": \"$service_name\", \"task_name\": \"$task_name\", \"image\": \"$image\"}")
elif [ "$service" == "\"webhook\"" ]; then
cluster_name=webhook-cluster
container_name=webhook-container
service_name=webhook-service
task_name=webhook-task
image=webhook
deploy_matrix+=("{\"cluster_name\": \"$cluster_name\", \"container_name\": \"$container_name\", \"service_name\": \"$service_name\", \"task_name\": \"$task_name\", \"image\": \"$image\"}")
fi
done
env_matrix="{\"environment\": [$(
IFS=','; echo "${envs[*]}"
)]}"
service_matrix="{\"service\": [$(
IFS=','; echo "${services[*]}"
)]}"
deploy_matrix="[$(
IFS=','; echo "${deploy_matrix[*]}"
)]"
nr_matrix="[$(
IFS=','; echo "${nr[*]}"
)]"
echo "env_matrix=$env_matrix" >> $GITHUB_OUTPUT
echo "service_matrix=$service_matrix" >> $GITHUB_OUTPUT
echo "deploy_matrix=$deploy_matrix" >> $GITHUB_OUTPUT
echo "nr_matrix=$nr_matrix" >> $GITHUB_OUTPUT
run-clickhouse-migrations:
needs: prepare-matrix
runs-on: ubuntu-latest
strategy:
matrix:
env: ${{ fromJson(needs.prepare-matrix.outputs.env_matrix).environment }}
environment: ${{ matrix.env }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: true
fetch-depth: 0
token: ${{ secrets.SUBMODULES_TOKEN }}
- name: Whitelist Runner IP
id: whitelist
uses: novuhq/clickhouse-cloud-whitelist-ip-action@v1.0.0
with:
clickhouse-org-id: ${{ secrets.CLICKHOUSE_ORG_ID }}
clickhouse-service-id: ${{ secrets.CLICKHOUSE_SERVICE_ID }}
clickhouse-api-key-id: ${{ secrets.CLICKHOUSE_API_KEY_ID }}
clickhouse-api-key-secret: ${{ secrets.CLICKHOUSE_API_KEY_SECRET }}
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 10.16.1
run_install: false
- name: Setup Node Version
uses: actions/setup-node@v4
with:
node-version: "22.22.1"
cache: "pnpm"
- name: Install Dependencies
shell: bash
run: pnpm install --frozen-lockfile
- name: Run ClickHouse Migrations
working-directory: apps/api
env:
CH_MIGRATIONS_HOST: ${{ secrets.CLICK_HOUSE_URL }}
CH_MIGRATIONS_USER: ${{ secrets.CLICK_HOUSE_USER }}
CH_MIGRATIONS_PASSWORD: ${{ secrets.CLICK_HOUSE_PASSWORD }}
CH_MIGRATIONS_DB: ${{ secrets.CLICK_HOUSE_DATABASE }}
run: pnpm run clickhouse:migrate:prod
build:
needs: [prepare-matrix, run-clickhouse-migrations]
timeout-minutes: 60
runs-on: ubuntu-latest
environment: ${{ fromJson(needs.prepare-matrix.outputs.env_matrix).environment[0] }}
strategy:
matrix:
service: ${{ fromJson(needs.prepare-matrix.outputs.service_matrix).service }}
steps:
- name: Checkout
uses: actions/checkout@v5
with:
submodules: true
fetch-depth: 0
token: ${{ secrets.SUBMODULES_TOKEN }}
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 10.16.1
run_install: false
- name: Setup Node Version
uses: actions/setup-node@v4
with:
node-version: "22.22.1"
cache: "pnpm"
- name: Install Dependencies
shell: bash
run: pnpm install --frozen-lockfile
- name: Set Up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: "image=moby/buildkit:v0.13.1"
- name: Prepare Variables
run: echo "BULL_MQ_PRO_NPM_TOKEN=${{ secrets.BULL_MQ_PRO_NPM_TOKEN }}" >> $GITHUB_ENV
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID}}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ vars.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: ${{ vars.ECR_PREFIX }}
SERVICE: ${{ matrix.service }}
IMAGE_TAG: ${{ github.sha }}
DOCKER_BUILD_ARGUMENTS: >
--platform=linux/amd64
--output=type=image,name=$REGISTRY/$REPOSITORY/$SERVICE,push-by-digest=true,name-canonical=true
run: |
cp scripts/dotenvcreate.mjs apps/$SERVICE/src/dotenvcreate.mjs
cd apps/$SERVICE && pnpm run docker:build
docker tag novu-$SERVICE $REGISTRY/$REPOSITORY/$SERVICE:latest
docker tag novu-$SERVICE $REGISTRY/$REPOSITORY/$SERVICE:$IMAGE_TAG
docker push $REGISTRY/$REPOSITORY/$SERVICE:latest
docker push $REGISTRY/$REPOSITORY/$SERVICE:$IMAGE_TAG
deploy:
needs: [build, prepare-matrix, run-clickhouse-migrations]
runs-on: ubuntu-latest
strategy:
matrix:
env: ${{ fromJson(needs.prepare-matrix.outputs.env_matrix).environment }}
service: ${{ fromJson(needs.prepare-matrix.outputs.deploy_matrix) }}
environment: ${{ matrix.env }}
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ vars.AWS_REGION }}
- name: Download task definition
env:
ECS_PREFIX: ${{ vars.ECS_PREFIX }}
TASK_NAME: ${{ matrix.service.task_name }}
run: |
aws ecs describe-task-definition --task-definition ${ECS_PREFIX}-${TASK_NAME} \
--query taskDefinition > task-definition.json
- name: Render Amazon ECS task definition
id: render-web-container
uses: aws-actions/amazon-ecs-render-task-definition@39c13cf530718ffeb524ec8ee0c15882bcb13842
with:
task-definition: task-definition.json
container-name: ${{ vars.ECS_PREFIX }}-${{ matrix.service.container_name }}
image: ${{secrets.ECR_URI}}/${{ vars.ECR_PREFIX }}/${{ matrix.service.image }}:${{ github.sha }}
- name: Deploy to Amazon ECS service
uses: aws-actions/amazon-ecs-deploy-task-definition@3e7310352de91b71a906e60c22af629577546002
with:
task-definition: ${{ steps.render-web-container.outputs.task-definition }}
service: ${{ vars.ECS_PREFIX }}-${{ matrix.service.service_name }}
cluster: ${{ vars.ECS_PREFIX }}-${{ matrix.service.cluster_name }}
wait-for-service-stability: true
sentry_release:
needs: [deploy, prepare-matrix]
runs-on: ubuntu-latest
strategy:
matrix:
service: ${{ fromJson(needs.prepare-matrix.outputs.service_matrix).service }}
environment: ${{ fromJson(needs.prepare-matrix.outputs.env_matrix).environment[0] }}
steps:
- name: Checkout
uses: actions/checkout@v5
- name: Get NPM Version
id: package-version
uses: martinbeentjes/npm-get-version-action@main
with:
path: apps/${{ matrix.service }}
- name: Create Sentry release
uses: getsentry/action-release@v1
env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
SENTRY_ORG: ${{ vars.SENTRY_ORG }}
SENTRY_PROJECT: ${{ matrix.service }}
with:
version: "${{ github.sha }}"
version_prefix: v
environment: ${{vars.SENTRY_ENV}}
ignore_empty: true
ignore_missing: true
new_relic_release:
needs: [deploy, prepare-matrix]
if: ${{ fromJson(needs.prepare-matrix.outputs.nr_matrix) != '[]' }}
runs-on: ubuntu-latest
strategy:
matrix:
env: ${{ fromJson(needs.prepare-matrix.outputs.env_matrix).environment }}
nr: ${{ fromJson(needs.prepare-matrix.outputs.nr_matrix) }}
environment: ${{ matrix.env }}
steps:
- name: New Relic Application Deployment Marker
uses: newrelic/deployment-marker-action@v2.3.0
with:
region: EU
apiKey: ${{ secrets.NEW_RELIC_API_KEY }}
guid: ${{ matrix.nr == 'api' && secrets.NEW_RELIC_API_GUID || matrix.nr == 'worker' && secrets.NEW_RELIC_Worker_GUID }}
version: "${{ github.sha }}"
user: "${{ github.actor }}"
description: "Novu Cloud Deployment"
sync_novu_state:
needs: [deploy, prepare-matrix]
runs-on: ubuntu-latest
if: github.event.inputs.deploy_api == 'true'
environment: ${{ fromJson(needs.prepare-matrix.outputs.env_matrix).environment[0] }}
steps:
- name: Sync State to Novu
uses: novuhq/actions-novu-sync@v2
with:
secret-key: ${{ secrets.NOVU_INTERNAL_SECRET_KEY }}
bridge-url: ${{ vars.NOVU_BRIDGE_URL }}
webhook_notification:
needs: [deploy, prepare-matrix]
runs-on: ubuntu-latest
if: |
always() &&
needs.deploy.result == 'success' &&
(contains(github.event.inputs.environment, 'production'))
environment: ${{ fromJson(needs.prepare-matrix.outputs.env_matrix).environment[0] }}
steps:
- name: Send webhook notification for US production
if: |
github.event.inputs.environment == 'production-us' ||
github.event.inputs.environment == 'production-us-and-eu'
run: |
curl -X POST https://webhooks.bug0.com/integrations/test/run \
-H "Content-Type: application/json" \
-H "x-api-key: ${{ secrets.BUG0_SECRET_KEY }}" \
-d '{"url": "https://dashboard.novu.co", "source": "novuhq-novu", "prod": "true"}'
- name: Send webhook notification for EU production
if: |
github.event.inputs.environment == 'production-eu' ||
github.event.inputs.environment == 'production-us-and-eu'
run: |
curl -X POST https://webhooks.bug0.com/integrations/test/run \
-H "Content-Type: application/json" \
-H "x-api-key: ${{ secrets.BUG0_SECRET_KEY }}" \
-d '{"url": "https://eu.dashboard.novu.co", "source": "novuhq-novu", "prod": "true"}'