| ... | ... | @@ -6,12 +6,13 @@ import { basename } from "node:path"; |
| 6 | 6 | import { setTimeout as sleep } from "node:timers/promises"; |
| 7 | 7 | import { promisify } from "node:util"; |
| 8 | 8 | |
| 9 | | const { SOURCE_URL, BRANCH, PORT } = process.env; |
| 9 | const { BRANCH, PORT } = process.env; |
| 10 | 10 | const WORK = "/work"; |
| 11 | | const REPO = `${WORK}/repo.git`; |
| 12 | | const POLL_MS = 500; |
| 11 | const REPO = "/source.git"; |
| 12 | const POLL_MS = 250; |
| 13 | 13 | const DRAIN_MS = 500; |
| 14 | 14 | const RETRY_FAILED_MS = 15000; |
| 15 | const GIT = ["-c", "safe.directory=*", "--git-dir", REPO]; |
| 15 | 16 | |
| 16 | 17 | const out = promisify(execFile); |
| 17 | 18 | const log = (...args) => console.log("[deploy]", ...args); |
| ... | ... | @@ -21,7 +22,7 @@ const run = (cmd, args, opts = {}, ok = [0]) => |
| 21 | 22 | .once("error", reject) |
| 22 | 23 | .once("exit", (code) => (ok.includes(code) ? resolve() : reject(new Error(`${basename(cmd)} ${args[0]} exited with ${code}`)))), |
| 23 | 24 | ); |
| 24 | | const git = (...args) => out("git", ["--git-dir", REPO, ...args]).then((result) => result.stdout.trim()); |
| 25 | const git = (...args) => out("git", [...GIT, ...args]).then((result) => result.stdout.trim()); |
| 25 | 26 | |
| 26 | 27 | let live = null; |
| 27 | 28 | let wanted = null; |
| ... | ... | @@ -46,11 +47,11 @@ createServer((client) => { |
| 46 | 47 | upstream.on("error", end).on("close", end); |
| 47 | 48 | }).listen(Number(PORT)); |
| 48 | 49 | |
| 49 | | async function remoteHead() { |
| 50 | | const response = await fetch(`${SOURCE_URL}/info/refs?service=git-upload-pack`, { signal: AbortSignal.timeout(5000) }); |
| 51 | | if (!response.ok) throw new Error(`${SOURCE_URL} answered ${response.status}`); |
| 52 | | return (await response.text()).match(new RegExp(`([0-9a-f]{40}) refs/heads/${BRANCH}(?=\\0|\\n)`))?.[1] ?? null; |
| 53 | | } |
| 50 | const branchTip = () => |
| 51 | git("rev-parse", "--verify", "--quiet", `refs/heads/${BRANCH}`).catch((error) => { |
| 52 | if (error.code !== 1) throw error; |
| 53 | return null; |
| 54 | }); |
| 54 | 55 | |
| 55 | 56 | async function install(dir, modules) { |
| 56 | 57 | const staging = `${modules}.partial`; |
| ... | ... | @@ -104,9 +105,9 @@ function retire(old) { |
| 104 | 105 | } |
| 105 | 106 | |
| 106 | 107 | async function deploy() { |
| 107 | | const target = wanted; |
| 108 | const rev = wanted; |
| 108 | 109 | const current = () => { |
| 109 | | if (wanted !== target) throw new Error("superseded by a newer push"); |
| 110 | if (wanted !== rev) throw new Error("superseded by a newer push"); |
| 110 | 111 | }; |
| 111 | 112 | let release; |
| 112 | 113 | let last = performance.now(); |
| ... | ... | @@ -119,18 +120,14 @@ async function deploy() { |
| 119 | 120 | }; |
| 120 | 121 | |
| 121 | 122 | try { |
| 122 | | await run("git", ["--git-dir", REPO, "fetch", "--quiet", "--depth", "1", "--no-tags", SOURCE_URL, `refs/heads/${BRANCH}`]); |
| 123 | | const rev = await git("rev-parse", "FETCH_HEAD"); |
| 124 | | if (rev === live?.rev) return; |
| 125 | 123 | const [committedAt, subject] = (await git("log", "-1", "--format=%ct%n%s", rev)).split("\n"); |
| 126 | 124 | log(`deploying ${rev.slice(0, 7)} ${subject}`); |
| 127 | | lap("fetch"); |
| 128 | 125 | |
| 129 | 126 | release = { rev, dir: `${WORK}/rel/${rev}`, modules: null, port: 0, proc: null, sockets: new Set() }; |
| 130 | 127 | releases.add(release); |
| 131 | 128 | rmSync(release.dir, { recursive: true, force: true }); |
| 132 | 129 | mkdirSync(release.dir, { recursive: true }); |
| 133 | | await run("bash", ["-c", 'set -o pipefail; git --git-dir "$0" archive "$1" | tar -x -C "$2"', REPO, rev, release.dir]); |
| 130 | await run("bash", ["-c", 'set -o pipefail; git -c safe.directory="*" --git-dir "$0" archive "$1" | tar -x -C "$2"', REPO, rev, release.dir]); |
| 134 | 131 | lap("extract"); |
| 135 | 132 | |
| 136 | 133 | const lock = createHash("sha256"); |
| ... | ... | @@ -175,8 +172,8 @@ async function deploy() { |
| 175 | 172 | if (old) retire(old); |
| 176 | 173 | log(`live ${rev.slice(0, 7)} in ${((performance.now() - start) / 1000).toFixed(1)}s: ${laps.join(", ")}`); |
| 177 | 174 | } catch (error) { |
| 178 | | log(`${target.slice(0, 7)} failed: ${error.message}`); |
| 179 | | failed = { rev: target, at: Date.now() }; |
| 175 | log(`${rev.slice(0, 7)} failed: ${error.message}`); |
| 176 | failed = { rev, at: Date.now() }; |
| 180 | 177 | if (release) releases.delete(release); |
| 181 | 178 | sweep(); |
| 182 | 179 | } |
| ... | ... | @@ -184,15 +181,14 @@ async function deploy() { |
| 184 | 181 | |
| 185 | 182 | mkdirSync(`${WORK}/rel`, { recursive: true }); |
| 186 | 183 | mkdirSync(`${WORK}/modules`, { recursive: true }); |
| 187 | | await run("git", ["init", "--quiet", "--bare", REPO]); |
| 188 | 184 | process.on("SIGTERM", () => process.exit(0)); |
| 189 | 185 | |
| 190 | 186 | (async () => { |
| 191 | 187 | let problem = ""; |
| 192 | 188 | for (;; await sleep(POLL_MS)) { |
| 193 | 189 | try { |
| 194 | | const head = await remoteHead(); |
| 195 | | if (!head) throw new Error(`${SOURCE_URL} has no branch ${BRANCH}`); |
| 190 | const head = await branchTip(); |
| 191 | if (!head) throw new Error(`${REPO} has no branch ${BRANCH}`); |
| 196 | 192 | wanted = head; |
| 197 | 193 | problem = ""; |
| 198 | 194 | } catch (error) { |