Deploy through the predeploy gate instead of by hand.
`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>
This commit is contained in:
@@ -281,6 +281,22 @@ npm run build # type-checks and bundles dist/
|
||||
npm start # serves dist/ and /api from one Node process
|
||||
```
|
||||
|
||||
To push a running install over ssh, `npm run deploy` runs the whole predeploy gate
|
||||
first — a type error, a failing test or a clipping-overlay regression stops it before
|
||||
anything is copied — then rsyncs the source, rebuilds, restarts the service and checks
|
||||
that the server is serving the same bundle hashes. npm runs the `predeploy` script
|
||||
before `deploy` on its own, so the gate runs exactly once; calling `node
|
||||
scripts/deploy.mjs` directly runs the gate itself:
|
||||
|
||||
```bash
|
||||
LUMINARY_DEPLOY_HOST=bhursey@photos.example.org \
|
||||
LUMINARY_DEPLOY_SUDO_PASS=… npm run deploy
|
||||
```
|
||||
|
||||
`LUMINARY_DEPLOY_PATH` (default `/srv/luminary`), `LUMINARY_DEPLOY_SERVICE` (default
|
||||
`luminary`) and `LUMINARY_DEPLOY_SSH_PASS` cover a different layout; without the
|
||||
password variables it uses your ssh key and passwordless sudo.
|
||||
|
||||
The server serves the built UI with an SPA fallback (unknown non-API paths return
|
||||
`index.html`), caches `/assets/*` for a year as immutable, and returns `503` with a
|
||||
build hint if `dist/index.html` is missing. JSON responses are `no-store`; image bytes
|
||||
@@ -958,6 +974,7 @@ positions. Camera-lens defaults require both a normalised camera and lens name t
|
||||
| `npm test` | Vitest, one pass then exit |
|
||||
| `npm run check:render` | Headless Chrome check that the 16-bit sidecar and JPEG proxy upload in the same row order |
|
||||
| `npm run predeploy` | Typecheck, unit suite, render check, then build — run this before pushing a deploy; the build is skipped when only test files changed since the last build |
|
||||
| `npm run deploy` | Predeploy gate, then rsync, remote build, restart and bundle-hash check (see [Production deployment](#production-deployment)) |
|
||||
|
||||
Conventions worth knowing before changing code:
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
"test": "vitest run",
|
||||
"check:render": "node scripts/render-check.mjs",
|
||||
"predeploy": "node scripts/predeploy.mjs",
|
||||
"deploy": "node scripts/deploy.mjs",
|
||||
"build:lens-db": "node scripts/build-lens-db.mjs"
|
||||
},
|
||||
"dependencies": {
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
#!/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();
|
||||
+38
-21
@@ -7,6 +7,9 @@
|
||||
* The signature is stored under node_modules/.cache: that directory is
|
||||
* gitignored and excluded from the deploy rsync, so it is per-checkout state
|
||||
* and never travels to the server.
|
||||
*
|
||||
* Exits via process.exitCode rather than process.exit(): piped stdout is async
|
||||
* and exiting early truncates the failure message.
|
||||
*/
|
||||
import { createHash } from 'node:crypto';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
@@ -37,6 +40,8 @@ const INPUTS = [
|
||||
/** Test files never reach the bundle, so their edits must not force a rebuild. */
|
||||
const NOT_BUNDLED = /(^|\/)(__tests__|__mocks__|__fixtures__)(\/|$)|\.(test|spec)\.[cm]?[jt]sx?$/;
|
||||
|
||||
class StepFailed extends Error {}
|
||||
|
||||
function walk(target) {
|
||||
if (!existsSync(target)) return [];
|
||||
if (statSync(target).isFile()) return [target];
|
||||
@@ -65,31 +70,43 @@ function run(name, args) {
|
||||
try {
|
||||
execFileSync('npm', args, { cwd: ROOT, stdio: 'inherit' });
|
||||
} catch {
|
||||
console.error(`\npredeploy FAILED at ${name}.`);
|
||||
process.exit(1);
|
||||
throw new StepFailed(name);
|
||||
}
|
||||
}
|
||||
|
||||
const signature = inputSignature();
|
||||
const previous = existsSync(CACHE) ? JSON.parse(readFileSync(CACHE, 'utf8')) : null;
|
||||
const upToDate =
|
||||
existsSync(path.join(ROOT, 'dist', 'index.html')) && previous?.digest === signature.digest;
|
||||
function main() {
|
||||
const signature = inputSignature();
|
||||
const previous = existsSync(CACHE) ? JSON.parse(readFileSync(CACHE, 'utf8')) : null;
|
||||
const upToDate =
|
||||
existsSync(path.join(ROOT, 'dist', 'index.html')) && previous?.digest === signature.digest;
|
||||
|
||||
run('typecheck', ['run', 'typecheck']);
|
||||
run('tests', ['run', 'test']);
|
||||
run('render check', ['run', 'check:render']);
|
||||
try {
|
||||
run('typecheck', ['run', 'typecheck']);
|
||||
run('tests', ['run', 'test']);
|
||||
run('render check', ['run', 'check:render']);
|
||||
if (upToDate) {
|
||||
console.log(
|
||||
`\npredeploy: build skipped - the ${signature.count} bundle inputs are unchanged since the last build.`,
|
||||
);
|
||||
} else {
|
||||
run('build', ['run', 'build']);
|
||||
}
|
||||
} catch (err) {
|
||||
if (!(err instanceof StepFailed)) throw err;
|
||||
console.error(`\npredeploy FAILED at ${err.message}.`);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (upToDate) {
|
||||
console.log(
|
||||
`\npredeploy: build skipped - the ${signature.count} bundle inputs are unchanged since the last build.`,
|
||||
);
|
||||
} else {
|
||||
run('build', ['run', 'build']);
|
||||
mkdirSync(path.dirname(CACHE), { recursive: true });
|
||||
writeFileSync(
|
||||
CACHE,
|
||||
`${JSON.stringify({ digest: signature.digest, count: signature.count, at: new Date().toISOString() }, null, 2)}\n`,
|
||||
);
|
||||
if (!upToDate) {
|
||||
mkdirSync(path.dirname(CACHE), { recursive: true });
|
||||
writeFileSync(
|
||||
CACHE,
|
||||
`${JSON.stringify({ digest: signature.digest, count: signature.count, at: new Date().toISOString() }, null, 2)}\n`,
|
||||
);
|
||||
}
|
||||
|
||||
console.log('\npredeploy PASSED.');
|
||||
return 0;
|
||||
}
|
||||
|
||||
console.log('\npredeploy PASSED.');
|
||||
process.exitCode = main();
|
||||
|
||||
Reference in New Issue
Block a user