-
Notifications
You must be signed in to change notification settings - Fork 10
472 lines (412 loc) · 15.2 KB
/
deploy_with_db.yml
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
name: Build and deploy with Database
on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch:
inputs:
environment:
description: 'Environment to deploy to'
type: environment
required: true
env:
OUTPUT_PATH_ENGINE_NODE: './build/engine/'
OUTPUT_PATH_ENGINE_ANDROID: './src/engine/native/android/app/build/outputs/apk/debug/'
OUTPUT_PATH_MS_SERVER: './src/management-system-v2/.next/'
DOCKER_PATH_MS_SERVER: './src/management-system-v2'
FILE_NAME_NODE_ENGINE: 'PROCEED-Engine-0.1.0.zip'
FILE_NAME_ANDROID: 'PROCEED-Engine-0.1.0.apk'
DB_CONTAINER: ${{ vars.DB_CONTAINER_NAME }}
DEFAULT_DB: ${{ vars.DB_DEFAULT_DB }}
jobs:
check-feature-flags:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Check feature flags are disabled
run: |
FILE="FeatureFlags.js"
# Define exempted flags
EXEMPTED_FLAGS=("enableMessaging" "enableUseDB" "enableUseFileManager")
# Find all flags set to `true`
FLAGS_WITH_TRUE=$(grep -E '^\s*enable[a-zA-Z0-9_]+\s*:\s*true' "$FILE" | awk -F: '{print $1}' | tr -d ' ')
# Check each flag
for FLAG in $FLAGS_WITH_TRUE; do
if [[ ! " ${EXEMPTED_FLAGS[@]} " =~ " $FLAG " ]]; then
echo "❌ Feature flag $FLAG must be set to false before merging!"
exit 1
fi
done
echo "✅ All feature flags (except exemptions) are correctly set to false."
install:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
check-latest: true
cache: 'yarn'
- run: yarn install --frozen-lockfile --ignore-engines
- uses: actions/cache@v4
timeout-minutes: 2
id: cache-install
with:
path: ./*
key: ${{ github.sha }}-${{ github.run_number }}
lint:
runs-on: ubuntu-latest
needs: install
steps:
- uses: actions/cache@v4
timeout-minutes: 2
id: restore-install
with:
path: ./*
key: ${{ github.sha }}-${{ github.run_number }}
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
check-latest: true
cache: 'yarn'
- run: yarn prettier --check .
testEngine:
runs-on: ubuntu-latest
needs: install
steps:
- uses: actions/cache@v4
timeout-minutes: 2
id: restore-install
with:
path: ./*
key: ${{ github.sha }}-${{ github.run_number }}
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
check-latest: true
cache: 'yarn'
- run: yarn test-engine --ci
testEngineE2E:
runs-on: ubuntu-latest
needs: install
steps:
- uses: actions/cache@v4
timeout-minutes: 2
id: restore-install
with:
path: ./*
key: ${{ github.sha }}-${{ github.run_number }}
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
check-latest: true
cache: 'yarn'
- run: yarn test-e2e --ci
testMS:
runs-on: ubuntu-latest
needs: install
steps:
- uses: actions/cache@v4
timeout-minutes: 2
id: restore-install
with:
path: ./*
key: ${{ github.sha }}-${{ github.run_number }}
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
check-latest: true
cache: 'yarn'
- run: yarn test-ms --ci
buildMS:
runs-on: ubuntu-latest
env:
IMAGE_TAG: ${{ inputs.environment == 'Production' && 'latest' || 'edge' }}
outputs:
tag: ${{ steps.set_tag.outputs.tag }}
needs: lint
steps:
- uses: actions/cache@v4
timeout-minutes: 2
id: restore-install
with:
path: ./*
key: ${{ github.sha }}-${{ github.run_number }}
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
check-latest: true
cache: 'yarn'
- run: yarn build-ms
- id: set_tag
run: |
TAG=$(git describe --tags --always --dirty --abbrev=7)
if [[ "$GITHUB_REF" == "refs/heads/main" ]]; then
echo "tag=${{ env.IMAGE_TAG }}" >> $GITHUB_OUTPUT
else
echo "tag=$TAG" >> $GITHUB_OUTPUT
fi
- name: Kaniko build
uses: aevea/action-kaniko@master
with:
image: proceed/ms-server
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
path: ${{ env.DOCKER_PATH_MS_SERVER }}
tag: ${{ steps.set_tag.outputs.tag }}
validate-migrations:
runs-on: ubuntu-latest
needs: buildMS
outputs:
main_diff: ${{ steps.migration-diff-main.outputs.diff }}
branch_diff: ${{ steps.migration-diff-branch.outputs.diff }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Fetch main branch
if: ${{github.ref != 'refs/heads/main'}}
run: git fetch origin main:main
- name: Compare migrations with main
if: ${{github.ref != 'refs/heads/main'}}
id: migration-diff-main
run: |
MIGRATIONS_PATH="src/management-system-v2/prisma/migrations"
# Use arrays for better handling of filenames with spaces
readarray -t MAIN_MIGRATIONS < <(git ls-tree -r main --name-only "$MIGRATIONS_PATH" | grep '\.sql$' || echo "")
readarray -t CURRENT_MIGRATIONS < <(git ls-tree -r HEAD --name-only "$MIGRATIONS_PATH" | grep '\.sql$' || echo "")
# Compare arrays and handle empty cases
if [ "${#MAIN_MIGRATIONS[@]}" -ne "${#CURRENT_MIGRATIONS[@]}" ] || \
[ -n "$(diff <(printf "%s\n" "${MAIN_MIGRATIONS[@]}") <(printf "%s\n" "${CURRENT_MIGRATIONS[@]}"))" ]; then
echo "Migration differences detected between main and current branch"
echo "diff=true" >> "$GITHUB_OUTPUT"
else
echo "No migration differences detected between main and current branch"
echo "diff=false" >> "$GITHUB_OUTPUT"
fi
- name: Check branch-specific migration changes
id: migration-diff-branch
run: |
if git diff --name-only HEAD~1 HEAD | grep -q 'src/management-system-v2/prisma/migrations/.*\.sql$'; then
echo "Branch-specific migration changes detected"
echo "diff=true" >> "$GITHUB_OUTPUT"
else
echo "No branch-specific migration changes"
echo "diff=false" >> "$GITHUB_OUTPUT"
fi
deploy:
runs-on: ubuntu-latest
needs: [buildMS, validate-migrations]
permissions:
contents: read
id-token: write
pull-requests: write
issues: write
environment: Research
env:
MS_TAG: ${{ needs.buildMS.outputs.tag }}
SERVICE_NAME: ${{ inputs.environment == 'Production' && 'ms-server-production' || 'ms-server-staging' }}
SUBDOMAIN: ${{ inputs.environment == 'Production' && 'app' || 'staging' }}
ENV: Research
steps:
# Database name generation step
- name: Generate sanitized database name
id: generate-name
run: |
# Sanitize branch name for database naming
BRANCH_NAME="${{ github.ref_name }}"
SANITIZED_BRANCH=$(echo "$BRANCH_NAME" | sed -E 's/[^a-zA-Z0-9]/_/g' | tr '[:upper:]' '[:lower:]')
# Determine suffix based on migration differences
if [ "${{ needs.validate-migrations.outputs.main_diff }}" = "false" ]; then
DB_NAME="proceed_db_${SANITIZED_BRANCH}_main"
else
DB_NAME="proceed_db_${SANITIZED_BRANCH}"
fi
DB_NAME="${DB_NAME:0:63}"
echo "Generated database name: $DB_NAME"
echo "DB_NAME=$DB_NAME" >> "$GITHUB_OUTPUT"
# SSH Configuration step
- name: Configure SSH
run: |
mkdir -p ~/.ssh/
echo "$SSH_KEY" > ~/.ssh/$ENV.key
chmod 600 ~/.ssh/$ENV.key
cat >>~/.ssh/config <<END
Host $ENV
HostName $SSH_HOST
User $SSH_USER
IdentityFile ~/.ssh/$ENV.key
StrictHostKeyChecking no
END
env:
SSH_USER: ${{ secrets.SSH_USER }}
SSH_KEY: ${{ secrets.SSH_KEY }}
SSH_HOST: ${{ secrets.SSH_HOST }}
# Database lifecycle management step
- name: Manage database lifecycle
run: |
DB_EXISTS=$(ssh $ENV "docker exec ${{ env.DB_CONTAINER }} \
psql -U ${{ secrets.DB_USER }} \
-d ${{ env.DEFAULT_DB }} \
-tAc \"SELECT 1 FROM pg_database WHERE datname='${{ steps.generate-name.outputs.DB_NAME }}'\"")
if [[ "${{ needs.validate-migrations.outputs.main_diff }}" = "true" && \
"${{ needs.validate-migrations.outputs.branch_diff }}" = "true" ]]; then
echo "Dropping existing database due to migration changes"
ssh $ENV \
"docker exec ${{ env.DB_CONTAINER }} \
psql -U ${{ secrets.DB_USER }} \
-d ${{ env.DEFAULT_DB }} \
-c 'DROP DATABASE IF EXISTS \"${{ steps.generate-name.outputs.DB_NAME }}\"'"
elif [ "$DB_EXISTS" = "1" ]; then
echo "Reusing existing database"
else
echo "Database will be created in subsequent step"
fi
# Database deployment steps
- name: Verify database status
id: db-status
run: |
DB_EXISTS=$(ssh $ENV \
"docker exec ${{ env.DB_CONTAINER }} \
psql -U ${{ secrets.DB_USER }} \
-d ${{ env.DEFAULT_DB }} \
-tAc \"SELECT 1 FROM pg_database WHERE datname='${{ steps.generate-name.outputs.DB_NAME }}'\"")
echo "create_db=$([[ "$DB_EXISTS" != "1" ]] && echo 'true' || echo 'false')" >> "$GITHUB_OUTPUT"
- name: Create database if needed
if: steps.db-status.outputs.create_db == 'true'
run: |
ssh $ENV \
"docker exec ${{ env.DB_CONTAINER }} \
psql -U ${{ secrets.DB_USER }} \
-d ${{ env.DEFAULT_DB }} \
-c 'CREATE DATABASE \"${{ steps.generate-name.outputs.DB_NAME }}\"'"
- name: Configure database connection
id: configure-connection
run: |
echo "DATABASE_URL=postgresql://${{ secrets.DB_USER }}:${{secrets.DB_PASSWORD}}@${{ secrets.SSH_HOST }}:5433/${{ steps.generate-name.outputs.DB_NAME }}?schema=public" >> "$GITHUB_OUTPUT"
- name: Restore Cache
uses: actions/cache@v4
timeout-minutes: 2
id: restore-install
with:
path: ./*
key: ${{ github.sha }}-${{ github.run_number }}
- name: Apply Prisma Migrations
if: steps.db-status.outputs.create_db == 'true'
env:
DATABASE_URL: ${{ steps.configure-connection.outputs.DATABASE_URL }}
run: |
yarn dev-ms-db-deploy
- uses: 'google-github-actions/auth@v2'
with:
project_id: 'proceed-bpms'
workload_identity_provider: 'projects/1062024918148/locations/global/workloadIdentityPools/github-ci/providers/github'
service_account: 'github-actions@proceed-bpms.iam.gserviceaccount.com'
- id: 'deploy'
if: ${{ github.ref == 'refs/heads/main' }}
uses: 'google-github-actions/deploy-cloudrun@v2'
with:
service: ${{ env.SERVICE_NAME }}
image: 'docker.io/proceed/ms-server:${{ env.MS_TAG }}'
env_vars: |
NEXTAUTH_URL=https://${{ env.SUBDOMAIN }}.proceed-labs.org
DATABASE_URL=postgresql://${{ secrets.DB_USER }}:${{secrets.DB_PASSWORD}}@${{ secrets.SSH_HOST }}:5433/${{ steps.generate-name.outputs.DB_NAME }}?schema=public
PROCEED_PUBLIC_DEPLOYMENT_ENV=local
region: 'europe-west1'
revision_traffic: LATEST=100
- id: 'deploy-preview'
if: ${{ github.event_name == 'pull_request' }}
name: Preview Cloud Run Deployment
uses: anishsapkota/preview-cloudrun@main
with:
service: ${{ env.SERVICE_NAME }}
image: docker.io/proceed/ms-server:${{ env.MS_TAG }}
token: ${{ secrets.GITHUB_TOKEN }}
region: 'europe-west1'
env_vars: '{"DATABASE_URL": "${{ steps.configure-connection.outputs.DATABASE_URL }}", "PROCEED_PUBLIC_DEPLOYMENT_ENV":"local", "PROCEED_PUBLIC_ENABLE_EXECUTION": "true"}'
githubEnvironment:
runs-on: ubuntu-latest
needs: deploy
if: ${{ github.ref == 'refs/heads/main' }}
environment:
name: ${{ inputs.environment || 'Staging' }}
url: ${{ inputs.environment == 'Production' && 'https://app.proceed-labs.org' || 'https://staging.proceed-labs.org' }}
steps:
- run: echo "${{ inputs.environment || 'Staging' }}"
testE2E:
needs: deploy
timeout-minutes: 60
runs-on: ubuntu-latest
if: ${{ inputs.environment != 'Production' }}
strategy:
matrix:
shard: [1, 2, 3, 4]
steps:
- uses: actions/cache@v4
timeout-minutes: 2
id: restore-install
with:
path: ./*
key: ${{ github.sha }}-${{ github.run_number }}
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
check-latest: true
cache: 'yarn'
# TODO: cache these?
- name: Install Playwright Browsers
run: yarn playwright install --with-deps
- name: Run Playwright tests
run: yarn playwright test --shard=${{ matrix.shard }}/${{ strategy.job-total }}
env:
PLAYWRIGHT_TEST_BASE_URL: ${{ github.event_name == 'pull_request' && format('https://pr-{0}---ms-server-staging-c4f6qdpj7q-ew.a.run.app', github.event.number) || 'https://staging.proceed-labs.org' }}
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: all-blob-reports-${{ matrix.shard }}
path: blob-report
retention-days: 1
create-report:
name: 📔 Create test report
if: always()
needs: [testE2E]
runs-on: ubuntu-latest
steps:
- uses: actions/cache@v4
timeout-minutes: 2
id: restore-install
with:
path: ./*
key: ${{ github.sha }}-${{ github.run_number }}
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
check-latest: true
cache: 'yarn'
- name: Download blob reports from GitHub Actions Artifacts
uses: actions/download-artifact@v4
with:
pattern: all-blob-reports-*
merge-multiple: true
path: all-blob-reports
- name: Merge into HTML Report
run: npx playwright merge-reports --reporter html ./all-blob-reports
- name: Upload HTML report
uses: actions/upload-artifact@v4
with:
name: html-report--attempt-${{ github.run_attempt }}
path: playwright-report
retention-days: 7