#!/usr/bin/env node /** * Minimal HTTP hook for Gitea push → pull/build on the LXC. * Listen on loopback only; Gitea reaches the host via VPN/firewall or Actions runner. * * Env: * DEPLOY_HOOK_HOST default 127.0.0.1 * DEPLOY_HOOK_PORT default 9090 * DEPLOY_HOOK_TOKEN required — send as Authorization: Bearer or X-Deploy-Token * DEPLOY_SCRIPT default /opt/news-platform/deploy/scripts/webhook-deploy.sh */ import http from 'node:http'; import { spawn } from 'node:child_process'; const HOST = process.env.DEPLOY_HOOK_HOST ?? '127.0.0.1'; const PORT = parseInt(process.env.DEPLOY_HOOK_PORT ?? '9090', 10); const TOKEN = process.env.DEPLOY_HOOK_TOKEN ?? ''; const SCRIPT = process.env.DEPLOY_SCRIPT ?? '/opt/news-platform/deploy/scripts/webhook-deploy.sh'; function unauthorized(res) { res.writeHead(401, { 'Content-Type': 'text/plain; charset=utf-8' }); res.end('Unauthorized'); } function extract_token(req) { const auth = req.headers.authorization ?? ''; if (auth.startsWith('Bearer ')) { return auth.slice(7).trim(); } const h = req.headers['x-deploy-token']; return typeof h === 'string' ? h.trim() : ''; } const server = http.createServer((req, res) => { if (req.method !== 'POST' || req.url !== '/hook') { res.writeHead(404).end(); return; } if (!TOKEN) { res.writeHead(503, { 'Content-Type': 'text/plain; charset=utf-8' }); res.end('DEPLOY_HOOK_TOKEN not set'); return; } const token = extract_token(req); if (token !== TOKEN) { unauthorized(res); return; } req.resume(); const child = spawn('/bin/bash', [SCRIPT], { detached: true, stdio: 'ignore', env: { ...process.env, PATH: process.env.PATH ?? '/usr/bin:/bin' }, }); child.unref(); res.writeHead(202, { 'Content-Type': 'text/plain; charset=utf-8' }); res.end('Accepted'); }); server.listen(PORT, HOST, () => { process.stdout.write(`news-platform deploy hook on http://${HOST}:${PORT}/hook\n`); });