24deb8e69b
`npm run deploy` now runs the gate (typecheck, unit suite, render check) before anything is copied, then rsyncs the source, rebuilds and restarts the service on the target from LUMINARY_DEPLOY_* variables, and fails if the server is not serving the same bundle hashes as the reviewed local build. npm runs a script named predeploy before deploy on its own, so the gate runs once; invoked as `node scripts/deploy.mjs` the script gates itself. Both scripts exit through process.exitCode, because process.exit() truncated the failure messages when they were piped. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
167 lines
5.7 KiB
JavaScript
167 lines
5.7 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Deploy Luminary: run the predeploy gate, then copy the source to the server,
|
|
* rebuild it there and restart the service.
|
|
*
|
|
* The gate runs first and a failure exits before anything touches the server,
|
|
* so a type error, a failing test or a shader regression caught by the render
|
|
* check cannot reach production. The local bundle is compared with the one the
|
|
* server just built, so a deploy that silently ships stale assets is visible.
|
|
*
|
|
* Exits via process.exitCode rather than process.exit(): stdout/stderr are
|
|
* async when piped, and exiting early truncates the very lines that explain
|
|
* why a deploy stopped.
|
|
*
|
|
* Environment:
|
|
* LUMINARY_DEPLOY_HOST ssh target, e.g. bhursey@photos.example.org (required)
|
|
* LUMINARY_DEPLOY_PATH remote checkout of this repo (default /srv/luminary)
|
|
* LUMINARY_DEPLOY_SERVICE systemd unit to restart (default luminary)
|
|
* LUMINARY_DEPLOY_SSH_PASS optional ssh password, passed to sshpass via env
|
|
* LUMINARY_DEPLOY_SUDO_PASS optional sudo password for the remote build/restart
|
|
*/
|
|
import { execFileSync } from 'node:child_process';
|
|
import { existsSync, readdirSync } from 'node:fs';
|
|
import path from 'node:path';
|
|
|
|
const env = process.env;
|
|
const HOST = env.LUMINARY_DEPLOY_HOST;
|
|
const REMOTE_PATH = env.LUMINARY_DEPLOY_PATH || '/srv/luminary';
|
|
const SERVICE = env.LUMINARY_DEPLOY_SERVICE || 'luminary';
|
|
const SSH_PASS = env.LUMINARY_DEPLOY_SSH_PASS || '';
|
|
const SUDO_PASS = env.LUMINARY_DEPLOY_SUDO_PASS || '';
|
|
|
|
/** Local-only state that must never be copied over the server's copy. */
|
|
const EXCLUDES = [
|
|
'node_modules',
|
|
'dist',
|
|
'data',
|
|
'.git',
|
|
'.freebuff',
|
|
'.check',
|
|
'.smoke',
|
|
'logo.png',
|
|
'*.log',
|
|
];
|
|
|
|
function run(command, args, extraEnv) {
|
|
execFileSync(command, args, {
|
|
stdio: 'inherit',
|
|
env: extraEnv ? { ...process.env, ...extraEnv } : process.env,
|
|
});
|
|
}
|
|
|
|
function sshArgs(script) {
|
|
return ['-o', 'StrictHostKeyChecking=accept-new', HOST, script];
|
|
}
|
|
|
|
/** Runs a remote script with stdout echoed, returning what it printed. */
|
|
function sshCapture(script, input) {
|
|
const piped = input !== undefined;
|
|
const output = execFileSync(
|
|
SSH_PASS ? 'sshpass' : 'ssh',
|
|
SSH_PASS ? ['-e', 'ssh', ...sshArgs(script)] : sshArgs(script),
|
|
{
|
|
stdio: [piped ? 'pipe' : 'inherit', 'pipe', 'inherit'],
|
|
input,
|
|
encoding: 'utf8',
|
|
env: SSH_PASS ? { ...process.env, SSHPASS: SSH_PASS } : process.env,
|
|
},
|
|
);
|
|
process.stdout.write(output);
|
|
return output;
|
|
}
|
|
|
|
function localBundles() {
|
|
const dir = path.join(process.cwd(), 'dist', 'assets');
|
|
if (!existsSync(dir)) return [];
|
|
return readdirSync(dir)
|
|
.filter((file) => /^index-.*\.(js|css)$/.test(file))
|
|
.sort();
|
|
}
|
|
|
|
function main() {
|
|
if (!HOST) {
|
|
console.error('deploy: set LUMINARY_DEPLOY_HOST to the ssh target, e.g. user@server.');
|
|
return 2;
|
|
}
|
|
|
|
// 1. Gate. Nothing below this line may run if the gate fails.
|
|
// npm runs a script called "predeploy" before "deploy", so under `npm run
|
|
// deploy` the gate has already passed before this process starts; running it
|
|
// again would typecheck and render-check the same source twice. Invoked
|
|
// directly, this script has to run the gate itself.
|
|
if (env.npm_lifecycle_event === 'deploy') {
|
|
console.log('deploy: predeploy gate passed (npm predeploy hook)');
|
|
} else {
|
|
console.log('deploy: predeploy gate');
|
|
try {
|
|
run(process.execPath, [path.join('scripts', 'predeploy.mjs')]);
|
|
} catch {
|
|
console.error('deploy: aborted - predeploy failed, nothing was sent to the server.');
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
// 2. Copy the source, keeping server-side data and build output.
|
|
console.log(`\ndeploy: rsync to ${HOST}:${REMOTE_PATH}`);
|
|
const rsyncArgs = ['-az'];
|
|
for (const item of EXCLUDES) rsyncArgs.push(`--exclude=${item}`);
|
|
const remoteShell = SSH_PASS
|
|
? `sshpass -e ssh -o StrictHostKeyChecking=accept-new`
|
|
: `ssh -o StrictHostKeyChecking=accept-new`;
|
|
rsyncArgs.push('-e', remoteShell, './', `${HOST}:${REMOTE_PATH}/`);
|
|
try {
|
|
run('rsync', rsyncArgs, SSH_PASS ? { SSHPASS: SSH_PASS } : undefined);
|
|
} catch {
|
|
console.error('deploy: aborted - rsync failed.');
|
|
return 1;
|
|
}
|
|
|
|
// 3. Rebuild and restart there, then report what is actually being served.
|
|
const sudo = SUDO_PASS ? `sudo -S -p '' ` : '';
|
|
const sudoLines = SUDO_PASS ? `${SUDO_PASS}\n${SUDO_PASS}\n` : undefined;
|
|
const script = [
|
|
`cd ${REMOTE_PATH} || exit 3`,
|
|
`${sudo}npm run build`,
|
|
'build=$?',
|
|
'[ "$build" -eq 0 ] || { echo "deploy: remote build failed ($build)"; exit "$build"; }',
|
|
`${sudo}systemctl restart ${SERVICE}`,
|
|
'sleep 2',
|
|
`systemctl is-active ${SERVICE}`,
|
|
'sha256sum dist/assets/*.js dist/assets/*.css',
|
|
].join('\n');
|
|
|
|
console.log(`\ndeploy: build and restart ${SERVICE} on ${HOST}`);
|
|
let output;
|
|
try {
|
|
output = sshCapture(script, sudoLines);
|
|
} catch {
|
|
console.error('deploy: aborted - the remote build or restart failed.');
|
|
return 1;
|
|
}
|
|
|
|
// 4. The server must be serving the bundle the gate just reviewed.
|
|
const remoteHashes = new Map();
|
|
for (const line of output.split('\n')) {
|
|
const match = /^([0-9a-f]{64})\s+dist\/assets\/(index-[^/\s]+)$/.exec(line.trim());
|
|
if (match) remoteHashes.set(match[2], match[1]);
|
|
}
|
|
const missing = localBundles().filter((file) => !remoteHashes.has(file));
|
|
if (missing.length) {
|
|
console.error(
|
|
`\ndeploy: FAILED - the server is not serving ${missing.join(', ')}; check the remote build.`,
|
|
);
|
|
return 1;
|
|
}
|
|
|
|
const served = [...remoteHashes.entries()];
|
|
console.log(
|
|
`\ndeploy PASSED: ${SERVICE} is active on ${HOST} serving ${served
|
|
.map(([file, hash]) => `${file} (${hash.slice(0, 12)})`)
|
|
.join(', ')}.`,
|
|
);
|
|
return 0;
|
|
}
|
|
|
|
process.exitCode = main();
|