Skip to content

Commit 054a0fa

Browse files
refactor: Add dedicated setup-db job to eliminate cache race condition (calcom#26171)
* refactor: Add dedicated setup-db job to eliminate cache race condition - Create new setup-db.yml workflow that runs cache-db action - Update pr.yml to add setup-db job that runs before all E2E jobs - Update integration-test, e2e, e2e-api-v2, e2e-app-store, e2e-embed, and e2e-embed-react jobs to depend on setup-db instead of build-api-v1 - Add setup-db to required job needs list and result check This eliminates the race condition where multiple jobs could try to write to the same database cache simultaneously. Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * refactor: Remove DB parts from api-v1-production-build.yml The database setup is now handled by the dedicated setup-db job, so the postgres service and cache-db action are no longer needed in the API v1 build workflow. Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * Apply suggestion from @keithwillcode * Made integration tests dependency consistent * refactor: Remove unnecessary env vars from setup-db.yml Only keep the env vars needed for database setup: - CALENDSO_ENCRYPTION_KEY (for db-seed) - DATABASE_URL / DATABASE_DIRECT_URL (for database connection) - TURBO_TOKEN / TURBO_TEAM (for turbo caching) Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: Add back E2E_TEST_CALCOM_QA_* env vars needed by db-seed These env vars are used by scripts/seed.ts to create the QA user with Google Calendar credentials during database seeding. Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * fix: Restore postgres service and cache-db to api-v1-production-build.yml The API v1 build still needs a database to run properly. Restored the postgres service and cache-db action, and added dependency on setup-db so it waits for the database cache to be created first. Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * E2E for API v2 no longer waiting on API v2 build * Reordering the jobs by dependencies * add GOOGLE_API_CREDENTIALS env var to setup db * Added back all env vars to setup-db * fix: Include commit SHA in cache-db key to invalidate cache on new commits Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> * refactor: Strip back setup-db.yml env vars to minimal set needed for db-seed Co-Authored-By: keith@cal.com <keithwillcode@gmail.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent cd6c5fe commit 054a0fa

4 files changed

Lines changed: 80 additions & 11 deletions

File tree

.github/actions/cache-db/action.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ inputs:
77
path:
88
required: false
99
default: "backups/backup.sql"
10+
COMMIT_SHA:
11+
required: false
12+
default: ""
13+
description: "Commit SHA to include in cache key (passed from workflow)"
1014
runs:
1115
using: "composite"
1216
steps:
@@ -17,7 +21,7 @@ runs:
1721
cache-name: cache-db
1822
key-1: ${{ hashFiles('packages/prisma/schema.prisma', 'packages/prisma/migrations/**/**.sql', 'packages/prisma/*.ts') }}
1923
key-2: ${{ github.event.pull_request.number || github.ref }}
20-
key-3: ${{ github.event.pull_request.head.sha }}
24+
key-3: ${{ inputs.COMMIT_SHA || github.event.pull_request.head.sha || github.sha }}
2125
DATABASE_URL: ${{ inputs.DATABASE_URL }}
2226
DATABASE_DIRECT_URL: ${{ inputs.DATABASE_URL }}
2327
E2E_TEST_CALCOM_QA_EMAIL: ${{ inputs.E2E_TEST_CALCOM_QA_EMAIL }}

.github/workflows/e2e.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ permissions:
55
actions: write
66
contents: read
77
env:
8-
NODE_OPTIONS: --max-old-space-size=4096
98
ALLOWED_HOSTNAMES: ${{ vars.CI_ALLOWED_HOSTNAMES }}
109
CALENDSO_ENCRYPTION_KEY: ${{ secrets.CI_CALENDSO_ENCRYPTION_KEY }}
1110
DAILY_API_KEY: ${{ secrets.CI_DAILY_API_KEY }}
@@ -15,11 +14,11 @@ env:
1514
E2E_TEST_APPLE_CALENDAR_EMAIL: ${{ secrets.E2E_TEST_APPLE_CALENDAR_EMAIL }}
1615
E2E_TEST_APPLE_CALENDAR_PASSWORD: ${{ secrets.E2E_TEST_APPLE_CALENDAR_PASSWORD }}
1716
E2E_TEST_MAILHOG_ENABLED: ${{ vars.E2E_TEST_MAILHOG_ENABLED }}
18-
GOOGLE_API_CREDENTIALS: ${{ secrets.CI_GOOGLE_API_CREDENTIALS }}
1917
EMAIL_SERVER_HOST: ${{ secrets.CI_EMAIL_SERVER_HOST }}
2018
EMAIL_SERVER_PORT: ${{ secrets.CI_EMAIL_SERVER_PORT }}
2119
EMAIL_SERVER_USER: ${{ secrets.CI_EMAIL_SERVER_USER }}
2220
EMAIL_SERVER_PASSWORD: ${{ secrets.CI_EMAIL_SERVER_PASSWORD}}
21+
GOOGLE_API_CREDENTIALS: ${{ secrets.CI_GOOGLE_API_CREDENTIALS }}
2322
GOOGLE_LOGIN_ENABLED: ${{ vars.CI_GOOGLE_LOGIN_ENABLED }}
2423
NEXTAUTH_SECRET: ${{ secrets.CI_NEXTAUTH_SECRET }}
2524
NEXTAUTH_URL: ${{ secrets.CI_NEXTAUTH_URL }}
@@ -30,6 +29,7 @@ env:
3029
NEXT_PUBLIC_STRIPE_PUBLIC_KEY: ${{ secrets.CI_NEXT_PUBLIC_STRIPE_PUBLIC_KEY }}
3130
NEXT_PUBLIC_WEBAPP_URL: ${{ vars.CI_NEXT_PUBLIC_WEBAPP_URL }}
3231
NEXT_PUBLIC_WEBSITE_URL: ${{ vars.CI_NEXT_PUBLIC_WEBSITE_URL }}
32+
NODE_OPTIONS: --max-old-space-size=4096
3333
PAYMENT_FEE_FIXED: ${{ vars.CI_PAYMENT_FEE_FIXED }}
3434
PAYMENT_FEE_PERCENTAGE: ${{ vars.CI_PAYMENT_FEE_PERCENTAGE }}
3535
SAML_ADMINS: ${{ secrets.CI_SAML_ADMINS }}

.github/workflows/pr.yml

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,18 @@ jobs:
139139
uses: ./.github/workflows/unit-tests.yml
140140
secrets: inherit
141141

142+
setup-db:
143+
name: Setup Database
144+
needs: [prepare, deps]
145+
if: ${{ needs.prepare.outputs.run-e2e == 'true' && needs.prepare.outputs.has-files-requiring-all-checks == 'true' }}
146+
uses: ./.github/workflows/setup-db.yml
147+
with:
148+
COMMIT_SHA: ${{ needs.prepare.outputs.commit-sha }}
149+
secrets: inherit
150+
142151
build-api-v1:
143152
name: Production builds
144-
needs: [prepare, deps]
153+
needs: [prepare, deps, setup-db]
145154
if: ${{ needs.prepare.outputs.run-e2e == 'true' && needs.prepare.outputs.has-files-requiring-all-checks == 'true' }}
146155
uses: ./.github/workflows/api-v1-production-build.yml
147156
secrets: inherit
@@ -183,42 +192,42 @@ jobs:
183192

184193
integration-test:
185194
name: Tests
186-
needs: [prepare, build, build-api-v1]
187-
if: ${{ needs.prepare.outputs.has-files-requiring-all-checks == 'true' }}
195+
needs: [prepare, build, setup-db]
196+
if: ${{ needs.prepare.outputs.run-e2e == 'true' && needs.prepare.outputs.has-files-requiring-all-checks == 'true' }}
188197
uses: ./.github/workflows/integration-tests.yml
189198
secrets: inherit
190199

191200
e2e:
192201
name: Tests
193-
needs: [prepare, build, build-api-v1]
202+
needs: [prepare, build, setup-db]
194203
if: ${{ needs.prepare.outputs.run-e2e == 'true' && needs.prepare.outputs.has-files-requiring-all-checks == 'true' }}
195204
uses: ./.github/workflows/e2e.yml
196205
secrets: inherit
197206

198207
e2e-api-v2:
199208
name: Tests
200-
needs: [prepare, build-api-v2]
209+
needs: [prepare, setup-db]
201210
if: ${{ needs.prepare.outputs.run-e2e == 'true' && needs.prepare.outputs.has-files-requiring-all-checks == 'true' }}
202211
uses: ./.github/workflows/e2e-api-v2.yml
203212
secrets: inherit
204213

205214
e2e-app-store:
206215
name: Tests
207-
needs: [prepare, build]
216+
needs: [prepare, build, setup-db]
208217
if: ${{ needs.prepare.outputs.run-e2e == 'true' && needs.prepare.outputs.has-files-requiring-all-checks == 'true' }}
209218
uses: ./.github/workflows/e2e-app-store.yml
210219
secrets: inherit
211220

212221
e2e-embed:
213222
name: Tests
214-
needs: [prepare, build]
223+
needs: [prepare, build, setup-db]
215224
if: ${{ needs.prepare.outputs.run-e2e == 'true' && needs.prepare.outputs.has-files-requiring-all-checks == 'true' }}
216225
uses: ./.github/workflows/e2e-embed.yml
217226
secrets: inherit
218227

219228
e2e-embed-react:
220229
name: Tests
221-
needs: [prepare, build]
230+
needs: [prepare, build, setup-db]
222231
if: ${{ needs.prepare.outputs.run-e2e == 'true' && needs.prepare.outputs.has-files-requiring-all-checks == 'true' }}
223232
uses: ./.github/workflows/e2e-embed-react.yml
224233
secrets: inherit
@@ -243,6 +252,7 @@ jobs:
243252
build-atoms,
244253
build-docs,
245254
build-companion,
255+
setup-db,
246256
e2e,
247257
e2e-api-v2,
248258
e2e-embed,
@@ -266,6 +276,7 @@ jobs:
266276
needs.build-api-v2.result != 'success' ||
267277
needs.build-atoms.result != 'success' ||
268278
needs.build-docs.result != 'success' ||
279+
needs.setup-db.result != 'success' ||
269280
needs.integration-test.result != 'success' ||
270281
needs.e2e.result != 'success' ||
271282
needs.e2e-api-v2.result != 'success' ||

.github/workflows/setup-db.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: Setup Database
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
COMMIT_SHA:
7+
required: false
8+
type: string
9+
default: ""
10+
description: "Commit SHA to include in cache key"
11+
12+
permissions:
13+
contents: read
14+
15+
env:
16+
CALENDSO_ENCRYPTION_KEY: ${{ secrets.CI_CALENDSO_ENCRYPTION_KEY }}
17+
DATABASE_URL: ${{ secrets.CI_DATABASE_URL }}
18+
DATABASE_DIRECT_URL: ${{ secrets.CI_DATABASE_URL }}
19+
E2E_TEST_CALCOM_QA_EMAIL: ${{ secrets.E2E_TEST_CALCOM_QA_EMAIL }}
20+
E2E_TEST_CALCOM_QA_PASSWORD: ${{ secrets.E2E_TEST_CALCOM_QA_PASSWORD }}
21+
E2E_TEST_CALCOM_QA_GCAL_CREDENTIALS: ${{ secrets.E2E_TEST_CALCOM_QA_GCAL_CREDENTIALS }}
22+
GOOGLE_API_CREDENTIALS: ${{ secrets.CI_GOOGLE_API_CREDENTIALS }}
23+
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
24+
TURBO_TEAM: ${{ secrets.TURBO_TEAM }}
25+
26+
jobs:
27+
setup-db:
28+
name: Setup Database
29+
runs-on: buildjet-4vcpu-ubuntu-2204
30+
timeout-minutes: 15
31+
services:
32+
postgres:
33+
image: postgres:13
34+
credentials:
35+
username: ${{ secrets.DOCKERHUB_USERNAME }}
36+
password: ${{ secrets.DOCKERHUB_TOKEN }}
37+
env:
38+
POSTGRES_USER: postgres
39+
POSTGRES_PASSWORD: postgres
40+
POSTGRES_DB: calendso
41+
options: >-
42+
--health-cmd pg_isready
43+
--health-interval 10s
44+
--health-timeout 5s
45+
--health-retries 5
46+
ports:
47+
- 5432:5432
48+
steps:
49+
- uses: actions/checkout@v4
50+
- uses: ./.github/actions/dangerous-git-checkout
51+
- uses: ./.github/actions/yarn-install
52+
- uses: ./.github/actions/cache-db
53+
with:
54+
COMMIT_SHA: ${{ inputs.COMMIT_SHA || github.sha }}

0 commit comments

Comments
 (0)