Skip to content

Commit 338f089

Browse files
committed
handle envs better
1 parent 48bc3bf commit 338f089

File tree

4 files changed

+124
-34
lines changed

4 files changed

+124
-34
lines changed

dist/index.js

Lines changed: 66 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/coolify.ts

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
startApplicationByUuid,
2525
startServiceByUuid,
2626
updateApplicationByUuid,
27+
updateEnvByServiceUuid,
2728
updateEnvsByServiceUuid,
2829
updateServiceByUuid
2930
} from './client/sdk.gen.js'
@@ -216,15 +217,28 @@ export default class Coolify {
216217
checkStatus()
217218
})
218219
}
219-
private async createEnvsForService({
220+
private async createOrUpdateEnv({
220221
serviceUUID,
221-
envs
222+
env
222223
}: {
223224
serviceUUID: string
224-
envs: { key: string; value: string | undefined }[]
225+
env: { key: string; value: string | undefined }
225226
}) {
226-
for (const env of envs) {
227-
await createEnvByServiceUuid({
227+
if (!env.value) {
228+
throw new Error(`Env ${env.key} has no value`)
229+
}
230+
const res = await updateEnvByServiceUuid({
231+
client: this.client,
232+
path: {
233+
uuid: serviceUUID
234+
},
235+
body: {
236+
key: env.key,
237+
value: env.value
238+
}
239+
})
240+
if (res.error && res.error.message === 'Environment variable not found.') {
241+
const res2 = await createEnvByServiceUuid({
228242
client: this.client,
229243
path: {
230244
uuid: serviceUUID
@@ -234,6 +248,28 @@ export default class Coolify {
234248
value: env.value
235249
}
236250
})
251+
if (res2.error) {
252+
throw new Error(
253+
`Error creating env ${env.key} for service ${serviceUUID}: ${res2.error.message}`
254+
)
255+
}
256+
}
257+
}
258+
private async createEnvsForService({
259+
serviceUUID,
260+
envs
261+
}: {
262+
serviceUUID: string
263+
envs: { key: string; value: string | undefined }[]
264+
}) {
265+
for (const env of envs) {
266+
if (!env.value) {
267+
throw new Error(`Env ${env.key} has no value`)
268+
}
269+
await this.createOrUpdateEnv({
270+
serviceUUID,
271+
env
272+
})
237273
}
238274
}
239275

@@ -322,12 +358,9 @@ export default class Coolify {
322358
// Generate a random 64-character deployment key
323359
const deploymentKey = randomBytes(32).toString('hex')
324360
//Set the functions deployment key
325-
await createEnvByServiceUuid({
326-
client: this.client,
327-
path: {
328-
uuid: backendServiceUUID
329-
},
330-
body: {
361+
await this.createOrUpdateEnv({
362+
serviceUUID: backendServiceUUID,
363+
env: {
331364
key: 'SERVICE_SUPABASE_FUNCTIONS_DEPLOYMENT_KEY',
332365
value: deploymentKey
333366
}
@@ -412,12 +445,9 @@ export default class Coolify {
412445
)
413446

414447
console.log(`SERVICE_SUPABASE_URL: ${supabase_url}`)
415-
await createEnvByServiceUuid({
416-
client: this.client,
417-
path: {
418-
uuid: backendServiceUUID
419-
},
420-
body: {
448+
await this.createOrUpdateEnv({
449+
serviceUUID: backendServiceUUID,
450+
env: {
421451
key: 'SERVICE_SUPABASE_URL',
422452
value: supabase_url
423453
}
@@ -516,6 +546,16 @@ export default class Coolify {
516546
ephemeral
517547
})
518548
console.log(`Backend service UUID: ${backendServiceUUID}`)
549+
if (backendServiceUUID !== '123') {
550+
return {
551+
serviceUUID: backendServiceUUID,
552+
appUUID: '123',
553+
appURL: `https://${deploymentName}.dev.pawtograder.net`,
554+
supabase_url,
555+
supabase_service_role_key,
556+
supabase_anon_key
557+
}
558+
}
519559

520560
const frontendAppName = `${deploymentName}-frontend`
521561
//If there is already a frontend app with the target name, delete it

supabase-pawtograder.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,6 @@ services:
819819
- 'SUPABASE_SERVICE_ROLE_KEY=${SERVICE_SUPABASESERVICE_KEY}'
820820
- 'SUPABASE_DB_URL=postgresql://postgres:${SERVICE_PASSWORD_POSTGRES}@${POSTGRES_HOSTNAME:-supabase-db}:${POSTGRES_PORT:-5432}/${POSTGRES_DB:-postgres}'
821821
- 'VERIFY_JWT=${FUNCTIONS_VERIFY_JWT:-false}'
822-
- 'AWS_ACCESS_KEY_ID=${SERVICE_AWS_ACCESS_KEY_ID}'
823822
- 'AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}'
824823
- 'GITHUB_APP_ID=${GITHUB_APP_ID}'
825824
- 'GITHUB_OAUTH_CLIENT_ID=${GITHUB_OAUTH_CLIENT_ID}'

0 commit comments

Comments
 (0)