mirror of
https://git.meshkee.com/Meshkee/backend.git
synced 2026-08-11 22:30:59 +04:30
Track real website deploy success/failure and support master branch.
Deploy agent waits for build completion, writes status, and checks out origin/HEAD (main or master) so empty-main repos like mashinify can deploy. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
co-authored by
Cursor
parent
acc8f35682
commit
f7cee26975
@@ -103,11 +103,34 @@ function runScript(script, args) {
|
||||
});
|
||||
}
|
||||
|
||||
function readDeployStatus(slug) {
|
||||
const file = `/var/log/websites/deploy-${slug}.status.json`;
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const server = http.createServer(async (req, res) => {
|
||||
if (req.method === 'GET' && req.url === '/health') {
|
||||
return send(res, 200, { ok: true, allowed: [...allowedSlugs()] });
|
||||
}
|
||||
|
||||
if (req.method === 'GET' && req.url?.startsWith('/deploy-status')) {
|
||||
if (!assertAuth(req, res)) return;
|
||||
const url = new URL(req.url, 'http://127.0.0.1');
|
||||
const slug = String(url.searchParams.get('slug') || '').trim();
|
||||
if (!slug || !allowedSlugs().has(slug)) {
|
||||
return send(res, 400, { error: 'unknown or disallowed slug' });
|
||||
}
|
||||
const status = readDeployStatus(slug);
|
||||
if (!status) {
|
||||
return send(res, 404, { error: 'no deploy status yet', slug });
|
||||
}
|
||||
return send(res, 200, status);
|
||||
}
|
||||
|
||||
if (req.method === 'POST' && req.url === '/deploy') {
|
||||
if (!assertAuth(req, res)) return;
|
||||
|
||||
@@ -119,17 +142,47 @@ const server = http.createServer(async (req, res) => {
|
||||
}
|
||||
|
||||
const slug = String(body.slug || '').trim();
|
||||
const wait = body.wait === true || body.wait === 'true';
|
||||
if (!slug || !allowedSlugs().has(slug)) {
|
||||
return send(res, 400, { error: 'unknown or disallowed slug' });
|
||||
}
|
||||
|
||||
const child = spawn('/opt/websites-agent/deploy.sh', [slug], {
|
||||
detached: true,
|
||||
stdio: 'ignore',
|
||||
});
|
||||
child.unref();
|
||||
if (!wait) {
|
||||
const child = spawn('/opt/websites-agent/deploy.sh', [slug], {
|
||||
detached: true,
|
||||
stdio: 'ignore',
|
||||
});
|
||||
child.unref();
|
||||
return send(res, 202, { status: 'accepted', slug });
|
||||
}
|
||||
|
||||
return send(res, 202, { status: 'accepted', slug });
|
||||
try {
|
||||
const result = await runScript('/opt/websites-agent/deploy.sh', [slug]);
|
||||
const status = readDeployStatus(slug) || {
|
||||
slug,
|
||||
status: 'success',
|
||||
detail: 'Deploy finished',
|
||||
at: new Date().toISOString(),
|
||||
};
|
||||
return send(res, 200, {
|
||||
status: 'success',
|
||||
slug,
|
||||
detail: status.detail || null,
|
||||
at: status.at || null,
|
||||
log: (result.stdout || '').slice(-2000),
|
||||
});
|
||||
} catch (err) {
|
||||
const status = readDeployStatus(slug);
|
||||
const detail =
|
||||
(status && status.detail) ||
|
||||
(err instanceof Error ? err.message.slice(0, 2000) : String(err));
|
||||
return send(res, 500, {
|
||||
status: 'failed',
|
||||
slug,
|
||||
error: 'deploy failed',
|
||||
detail,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (req.method === 'POST' && req.url === '/provision') {
|
||||
|
||||
Reference in New Issue
Block a user