fix(coolify): upsertServiceEnv falls back to PATCH on already-exists

Coolify auto-creates env entries (with empty values) for every ${VAR}
reference it finds in the compose YAML at service-creation time. So
POST /services/{uuid}/envs returns 'already exists' for any env we
try to set after creation. The fix is to fall back to PATCH on that
specific error, making the helper a true upsert.

Made-with: Cursor
This commit is contained in:
2026-04-23 17:27:31 -07:00
parent 5d4936346e
commit 7944db8ba4

View File

@@ -576,10 +576,26 @@ export async function upsertServiceEnv(
uuid: string, uuid: string,
env: ServiceEnvVar, env: ServiceEnvVar,
): Promise<void> { ): Promise<void> {
await coolifyFetch(`/services/${uuid}/envs`, { // Coolify auto-creates env entries (with empty values) for every
method: 'POST', // ${VAR} reference in the compose file at service-creation time.
body: JSON.stringify(env), // POST to /envs returns "already exists" for those — we must use
}); // PATCH to update them. Try POST first, fall back to PATCH.
try {
await coolifyFetch(`/services/${uuid}/envs`, {
method: 'POST',
body: JSON.stringify(env),
});
} catch (err: unknown) {
const msg = err instanceof Error ? err.message : String(err);
if (msg.includes('already exists')) {
await coolifyFetch(`/services/${uuid}/envs`, {
method: 'PATCH',
body: JSON.stringify(env),
});
return;
}
throw err;
}
} }
export async function listAllServices(): Promise<Array<Record<string, unknown>>> { export async function listAllServices(): Promise<Array<Record<string, unknown>>> {