Deploys: exact sha, replay protection, health check and automatic rollback
CI / Test (jest) (push) Successful in 47s
CI / Lint (eslint) (push) Successful in 52s
CI / Security (npm audit) (push) Successful in 17s
CI / Deploy (push) Skipped
CI / Notify (push) Skipped
CI / Test (jest) (pull_request) Failing after 12s
CI / Lint (eslint) (pull_request) Successful in 14s
CI / Security (npm audit) (pull_request) Successful in 11s
CI / Deploy (pull_request) Skipped
CI / Notify (pull_request) Skipped
CI / Test (jest) (push) Successful in 47s
CI / Lint (eslint) (push) Successful in 52s
CI / Security (npm audit) (push) Successful in 17s
CI / Deploy (push) Skipped
CI / Notify (push) Skipped
CI / Test (jest) (pull_request) Failing after 12s
CI / Lint (eslint) (pull_request) Successful in 14s
CI / Security (npm audit) (pull_request) Successful in 11s
CI / Deploy (pull_request) Skipped
CI / Notify (pull_request) Skipped
#45 phase 2. - GET /healthz (unauthenticated): running git sha, version, database reachability, connected workers, uptime. 503 when the database is down. - deploy/pulse_deploy.sh, versioned for the first time (it only existed as a hand-edited file on CT 122). It takes <sha> <ts> from the signed webhook payload and: - refuses requests older than 5 minutes, or not newer than the last accepted one (replays); - refuses commits that aren't on main; - checks out exactly that sha (before, it deployed whatever main was); - runs npm ci --omit=dev (before, npm install --production) and restarts; - waits for /healthz to report the sha with a working database, and otherwise rolls back to the previous commit. - tests/deploy-script.test.js runs the script in a sandbox (temporary git origin, fake systemctl/npm/curl): exact-sha deploy with .env kept, replay, stale request, off-main commit, malformed input, and rollback of an unhealthy build. - CI's deploy job now succeeds only once production reports the tested sha as healthy, before tagging. Before, it only checked that the server answered. - README: CI/CD and deploy documentation, including installing the script and the hooks.json arguments. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b10ea154dd
commit
cb12fec28c
5 files changed
+311
-17
No files matched your search
@@ -0,0 +1,127 @@
|
||||
'use strict';
|
||||
|
||||
// Runs deploy/pulse_deploy.sh in a sandbox: a throwaway git origin and checkout,
|
||||
// with fake systemctl/npm/curl on PATH. "Production" health is simulated by the
|
||||
// fake curl, which reports whatever commit the fake systemctl last started; a
|
||||
// commit containing a BROKEN file reports db:false (an unhealthy build).
|
||||
|
||||
const { execFileSync, spawnSync } = require('child_process');
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
|
||||
const SCRIPT = path.join(__dirname, '..', 'deploy', 'pulse_deploy.sh');
|
||||
let root, app, bin, env, commits;
|
||||
|
||||
function sh(cwd, ...args) {
|
||||
return execFileSync('git', args, { cwd, stdio: ['ignore', 'pipe', 'pipe'] }).toString().trim();
|
||||
}
|
||||
|
||||
function commit(repo, name, files) {
|
||||
for (const [f, c] of Object.entries(files)) fs.writeFileSync(path.join(repo, f), c);
|
||||
sh(repo, 'add', '-A');
|
||||
sh(repo, '-c', 'user.name=t', '-c', 'user.email=t@example.invalid', 'commit', '-qm', name);
|
||||
return sh(repo, 'rev-parse', 'HEAD');
|
||||
}
|
||||
|
||||
function deploy(sha, ts = Math.floor(Date.now() / 1000)) {
|
||||
const r = spawnSync('bash', [SCRIPT, sha, String(ts)], { env, encoding: 'utf8' });
|
||||
return { code: r.status, out: r.stdout + r.stderr };
|
||||
}
|
||||
const head = () => sh(app, 'rev-parse', 'HEAD');
|
||||
const running = () => fs.readFileSync(path.join(root, 'running'), 'utf8').trim();
|
||||
|
||||
beforeAll(() => {
|
||||
root = fs.mkdtempSync(path.join(os.tmpdir(), 'pulse-deploy-'));
|
||||
const work = path.join(root, 'work');
|
||||
const origin = path.join(root, 'origin.git');
|
||||
app = path.join(root, 'app');
|
||||
bin = path.join(root, 'bin');
|
||||
fs.mkdirSync(work); fs.mkdirSync(bin);
|
||||
|
||||
sh(root, 'init', '-q', '--bare', '-b', 'main', origin);
|
||||
sh(root, 'init', '-q', '-b', 'main', work);
|
||||
commits = {};
|
||||
commits.a = commit(work, 'a', { 'server.js': 'a' });
|
||||
sh(work, 'remote', 'add', 'origin', origin);
|
||||
sh(work, 'push', '-q', 'origin', 'main');
|
||||
sh(root, 'clone', '-q', origin, app);
|
||||
fs.writeFileSync(path.join(app, '.env'), 'SECRET=keep-me\n'); // untracked, must survive
|
||||
|
||||
commits.b = commit(work, 'b', { 'server.js': 'b' });
|
||||
commits.broken = commit(work, 'broken', { 'server.js': 'c', BROKEN: '1' });
|
||||
commits.fixed = commit(work, 'fixed', { 'server.js': 'd' });
|
||||
fs.unlinkSync(path.join(work, 'BROKEN'));
|
||||
commits.fixed = commit(work, 'fixed2', { 'server.js': 'e' });
|
||||
sh(work, 'push', '-q', 'origin', 'main');
|
||||
sh(work, 'checkout', '-q', '-b', 'side', commits.a);
|
||||
commits.side = commit(work, 'side', { 'server.js': 'side' });
|
||||
sh(work, 'push', '-q', 'origin', 'side');
|
||||
|
||||
const fake = (name, body) => fs.writeFileSync(path.join(bin, name), `#!/bin/bash\n${body}\n`, { mode: 0o755 });
|
||||
fake('npm', 'exit 0');
|
||||
fake('systemctl', `git -C "${app}" rev-parse HEAD > "${root}/running"`);
|
||||
fake('curl', `s=$(cat "${root}/running" 2>/dev/null) || exit 7
|
||||
if [ -e "${app}/BROKEN" ]; then db=false; else db=true; fi
|
||||
echo "{\\"status\\":\\"ok\\",\\"sha\\":\\"$s\\",\\"db\\":$db}"`);
|
||||
fs.writeFileSync(path.join(root, 'running'), commits.a);
|
||||
|
||||
env = {
|
||||
...process.env,
|
||||
PATH: `${bin}:${process.env.PATH}`,
|
||||
PULSE_DEPLOY_APP_ROOT: app,
|
||||
PULSE_DEPLOY_STATE_DIR: path.join(root, 'state'),
|
||||
PULSE_DEPLOY_LOG: path.join(root, 'deploy.log'),
|
||||
PULSE_DEPLOY_LOCK: path.join(root, 'lock'),
|
||||
PULSE_DEPLOY_HEALTH_TRIES: '3',
|
||||
PULSE_DEPLOY_HEALTH_SLEEP: '0'
|
||||
};
|
||||
});
|
||||
|
||||
afterAll(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
|
||||
// Order matters: each test builds on the state the previous one left.
|
||||
test('deploys exactly the requested commit, not just the tip of main', () => {
|
||||
const r = deploy(commits.b);
|
||||
expect(r.code).toBe(0);
|
||||
expect(head()).toBe(commits.b);
|
||||
expect(running()).toBe(commits.b);
|
||||
expect(fs.readFileSync(path.join(app, '.env'), 'utf8')).toBe('SECRET=keep-me\n');
|
||||
});
|
||||
|
||||
test('a replayed request (same or older timestamp) is refused', () => {
|
||||
const ts = Math.floor(Date.now() / 1000) + 5;
|
||||
expect(deploy(commits.fixed, ts).code).toBe(0);
|
||||
const r = deploy(commits.b, ts);
|
||||
expect(r.code).toBe(1);
|
||||
expect(r.out).toMatch(/replay or out of order/);
|
||||
expect(head()).toBe(commits.fixed);
|
||||
});
|
||||
|
||||
test('a stale request is refused', () => {
|
||||
const r = deploy(commits.b, Math.floor(Date.now() / 1000) - 3600);
|
||||
expect(r.code).toBe(1);
|
||||
expect(r.out).toMatch(/old \(limit 300s\)/);
|
||||
});
|
||||
|
||||
test('a commit that is not on main is refused', () => {
|
||||
const r = deploy(commits.side, Math.floor(Date.now() / 1000) + 10);
|
||||
expect(r.code).toBe(1);
|
||||
// Only main is fetched, so a commit from another branch is unknown or not an ancestor.
|
||||
expect(r.out).toMatch(/is not on main|not found after fetch/);
|
||||
expect(head()).toBe(commits.fixed);
|
||||
});
|
||||
|
||||
test('malformed input is refused', () => {
|
||||
expect(deploy('abc', Math.floor(Date.now() / 1000) + 11).out).toMatch(/invalid sha/);
|
||||
expect(deploy(commits.b, 'x').out).toMatch(/invalid timestamp/);
|
||||
});
|
||||
|
||||
test('an unhealthy build is rolled back to the previous commit', () => {
|
||||
const r = deploy(commits.broken, Math.floor(Date.now() / 1000) + 20);
|
||||
expect(r.code).toBe(1);
|
||||
expect(r.out).toMatch(/rolling back/);
|
||||
expect(r.out).toMatch(/rolled back to .*healthy/);
|
||||
expect(head()).toBe(commits.fixed);
|
||||
expect(running()).toBe(commits.fixed);
|
||||
});
|
||||
Reference in new issue
Block a user