import hash from "object-hash"; import { $ } from "bun"; import path from "path"; import { config, indexedConfig, isSelf } from "./config"; import fs from "fs"; import { rebuildCaddyfile } from "./caddy"; import { l$ } from "./utils"; type DeployInstance = { host: string; commitHash?: string; }; export const deploy = async ({ host, commitHash }: DeployInstance) => { const deploymentId = new Date().toISOString(); console.log(`=== ${deploymentId} ===`); console.log({ host, commitHash }); const deployerDir = await l$`pwd`.text().then((t) => t.trim()); console.log(deployerDir); const service = config.services[host]; const { user, repo, branch, port } = indexedConfig[hash(service)]; const serviceDir = path.join(config.directory, host); await l$`mkdir -p ${serviceDir}`; // Fetch $.cwd(serviceDir); const url = `http://deployer:${config.token}@${config.giteaUrl}/${user}/${repo}`; await l$`mkdir -p src`; await l$`mkdir -p logs`; await $`git clone -b ${branch} ${url} ./src`.nothrow(); // explicitly don't log token $.cwd(path.join(serviceDir, "src")); await l$`git fetch origin ${branch}`; commitHash && (await l$`git reset --hard origin/${branch}`); commitHash && (await l$`git checkout ${commitHash}`); // Build await l$`make build`; rebuildCaddyfile(); // Register service const systemdServiceName = `deployer2-${host}.service`; await l$`cat ${path.join( deployerDir, "service.template" )} | envsubst > ${path.join(serviceDir, systemdServiceName)}`.env({ host, port: port.toString(), repo, branch, commitHash, deploymentId, logsDir: path.join(serviceDir, "logs"), serviceDir, user: isSelf(service) ? "root" : "drm", }); await l$`ln -sf ${serviceDir}/${systemdServiceName} ${config.systemServicesDir}/${systemdServiceName}`; // Start! await l$`systemctl daemon-reload`; await l$`systemctl restart ${systemdServiceName}`; };