ea7b5b846c
CI / verify (push) Has been cancelled
The rsync excludes node_modules, so a new dependency in the lockfile made the remote build fail until someone installed it by hand. Run npm ci on the server first and stop with the installer's exit code if it fails.
173 lines
6.0 KiB
JavaScript
173 lines
6.0 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`,
|
|
// node_modules is excluded from the rsync, so a new dependency in the
|
|
// lockfile would otherwise fail the build on a server that has never
|
|
// installed it. Installed as the login user: the checkout belongs to them.
|
|
'npm ci --no-audit --no-fund',
|
|
'deps=$?',
|
|
'[ "$deps" -eq 0 ] || { echo "deploy: remote npm ci failed ($deps)"; exit "$deps"; }',
|
|
`${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();
|