145 lines
4.7 KiB
JavaScript
Executable File
145 lines
4.7 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
const [, , command, ...args] = process.argv;
|
|
|
|
const apiUrl = (process.env.SELECTED_COOLIFY_API_URL || process.env.COOLIFY_STAGING_API_URL || process.env.COOLIFY_API_URL || "").replace(/\/+$/, "");
|
|
const token = process.env.SELECTED_COOLIFY_TOKEN || process.env.COOLIFY_STAGING_TOKEN || process.env.COOLIFY_TOKEN || "";
|
|
const delayMs = Number(process.env.COOLIFY_DEPLOYMENT_POLL_MS || "5000");
|
|
const attempts = Number(process.env.COOLIFY_DEPLOYMENT_WAIT_ATTEMPTS || "90");
|
|
|
|
function fail(message) {
|
|
process.stderr.write(`${message}\n`);
|
|
process.exit(1);
|
|
}
|
|
|
|
async function coolify(path, options = {}) {
|
|
if (!apiUrl || !token) {
|
|
fail("Coolify API URL and token are required");
|
|
}
|
|
const response = await fetch(`${apiUrl}${path}`, {
|
|
...options,
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
Accept: "application/json",
|
|
...(options.body ? { "Content-Type": "application/json" } : {}),
|
|
...options.headers,
|
|
},
|
|
});
|
|
|
|
const text = await response.text();
|
|
let payload = {};
|
|
if (text.trim()) {
|
|
try {
|
|
payload = JSON.parse(text);
|
|
} catch {
|
|
payload = { message: text.trim() };
|
|
}
|
|
}
|
|
|
|
if (!response.ok) {
|
|
fail(`Coolify API request failed with HTTP ${response.status}`);
|
|
}
|
|
|
|
return payload;
|
|
}
|
|
|
|
function deploymentStatus(payload) {
|
|
return String(payload.status || payload.deployment?.status || payload.data?.status || "unknown").toLowerCase();
|
|
}
|
|
|
|
function deploymentUuid(payload) {
|
|
const value = payload.deployment_uuid || payload.uuid || payload.deployment?.uuid || payload.data?.deployment_uuid || payload.data?.uuid || "";
|
|
return typeof value === "string" ? value : "";
|
|
}
|
|
|
|
async function trigger(resourceUuid) {
|
|
if (!resourceUuid) {
|
|
fail("Coolify resource UUID is required");
|
|
}
|
|
const payload = await coolify(`/applications/${encodeURIComponent(resourceUuid)}/start?force=true&instant_deploy=true`, {
|
|
method: "POST",
|
|
});
|
|
const uuid = deploymentUuid(payload);
|
|
if (!uuid) {
|
|
fail("Coolify deployment UUID was not returned by the start request");
|
|
}
|
|
process.stdout.write(uuid);
|
|
}
|
|
|
|
function envKey(entry) {
|
|
return typeof entry?.key === "string" ? entry.key : "";
|
|
}
|
|
|
|
function envValue(entry) {
|
|
const value = entry?.value ?? entry?.real_value;
|
|
return typeof value === "string" ? value : "";
|
|
}
|
|
|
|
async function setEnvs(resourceUuid, pairs) {
|
|
if (!resourceUuid) {
|
|
fail("Coolify resource UUID is required");
|
|
}
|
|
if (pairs.length === 0) {
|
|
fail("At least one KEY=VALUE pair is required");
|
|
}
|
|
const parsed = pairs.map((pair) => {
|
|
const separator = pair.indexOf("=");
|
|
if (separator <= 0) {
|
|
fail(`Invalid KEY=VALUE pair: ${pair}`);
|
|
}
|
|
return [pair.slice(0, separator), pair.slice(separator + 1)];
|
|
});
|
|
|
|
const existingPayload = await coolify(`/applications/${encodeURIComponent(resourceUuid)}/envs`);
|
|
const existing = Array.isArray(existingPayload) ? existingPayload : Array.isArray(existingPayload?.data) ? existingPayload.data : [];
|
|
const byKey = new Map(existing.map((entry) => [envKey(entry), entry]).filter(([key]) => key));
|
|
|
|
for (const [key, value] of parsed) {
|
|
const current = byKey.get(key);
|
|
if (current && envValue(current) === value) {
|
|
process.stdout.write(`env ${key} unchanged\n`);
|
|
continue;
|
|
}
|
|
await coolify(`/applications/${encodeURIComponent(resourceUuid)}/envs`, {
|
|
method: current ? "PATCH" : "POST",
|
|
body: JSON.stringify({
|
|
key,
|
|
value,
|
|
is_literal: true,
|
|
is_preview: false,
|
|
is_multiline: false,
|
|
is_shown_once: false,
|
|
}),
|
|
});
|
|
process.stdout.write(`env ${key} ${current ? "updated" : "created"}\n`);
|
|
}
|
|
}
|
|
|
|
async function waitForDeployment(uuid, label) {
|
|
if (!uuid) {
|
|
fail("Coolify deployment UUID is required");
|
|
}
|
|
for (let attempt = 1; attempt <= attempts; attempt += 1) {
|
|
const payload = await coolify(`/deployments/${encodeURIComponent(uuid)}`);
|
|
const status = deploymentStatus(payload);
|
|
process.stdout.write(`${label} deployment status: ${status}\n`);
|
|
if (["finished", "success", "succeeded", "completed", "done"].includes(status)) {
|
|
return;
|
|
}
|
|
if (["failed", "error", "cancelled", "canceled"].includes(status)) {
|
|
fail(`${label} deployment failed`);
|
|
}
|
|
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
}
|
|
fail(`${label} deployment did not finish in time`);
|
|
}
|
|
|
|
if (command === "trigger") {
|
|
await trigger(args[0] || "");
|
|
} else if (command === "wait") {
|
|
await waitForDeployment(args[0] || "", args[1] || "Coolify");
|
|
} else if (command === "set-envs") {
|
|
await setEnvs(args[0] || "", args.slice(1));
|
|
} else {
|
|
fail("Usage: coolify-deploy.mjs trigger <resource-uuid> | wait <deployment-uuid> <label> | set-envs <resource-uuid> KEY=VALUE...");
|
|
}
|