logging commands
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,5 +1,6 @@
|
|||||||
/config.ts
|
/config.ts
|
||||||
/Caddyfile
|
/Caddyfile
|
||||||
|
/live
|
||||||
|
|
||||||
# ---> Node
|
# ---> Node
|
||||||
# Logs
|
# Logs
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import path from "path";
|
|||||||
import { config, indexedConfig, isSelf } from "./config";
|
import { config, indexedConfig, isSelf } from "./config";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import { rebuildCaddyfile } from "./caddy";
|
import { rebuildCaddyfile } from "./caddy";
|
||||||
|
import { l$ } from "./utils";
|
||||||
|
|
||||||
type DeployInstance = {
|
type DeployInstance = {
|
||||||
host: string;
|
host: string;
|
||||||
@@ -11,33 +12,35 @@ type DeployInstance = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const deploy = async ({ host, commitHash }: DeployInstance) => {
|
export const deploy = async ({ host, commitHash }: DeployInstance) => {
|
||||||
|
const deploymentId = new Date().toISOString();
|
||||||
|
console.log(`=== ${deploymentId} ===`);
|
||||||
|
console.log({ host, commitHash });
|
||||||
|
|
||||||
const service = config.services[host];
|
const service = config.services[host];
|
||||||
const { user, repo, branch, port } = indexedConfig[hash(service)];
|
const { user, repo, branch, port } = indexedConfig[hash(service)];
|
||||||
|
|
||||||
const deploymentId = new Date().toISOString();
|
|
||||||
|
|
||||||
const serviceDir = path.join(config.directory, host);
|
const serviceDir = path.join(config.directory, host);
|
||||||
await $`mkdir -p ${serviceDir}`;
|
await l$`mkdir -p ${serviceDir}`;
|
||||||
|
|
||||||
// Fetch
|
// Fetch
|
||||||
$.cwd(serviceDir);
|
$.cwd(serviceDir);
|
||||||
const url = `http://deployer:${config.token}@${config.giteaUrl}/${user}/${repo}`;
|
const url = `http://deployer:${config.token}@${config.giteaUrl}/${user}/${repo}`;
|
||||||
await $`mkdir -p src`;
|
await l$`mkdir -p src`;
|
||||||
await $`mkdir -p logs`;
|
await l$`mkdir -p logs`;
|
||||||
await $`git clone -b ${branch} ${url} ./src`.nothrow();
|
await $`git clone -b ${branch} ${url} ./src`.nothrow(); // explicitly don't log token
|
||||||
|
|
||||||
$.cwd(path.join(serviceDir, "src"));
|
$.cwd(path.join(serviceDir, "src"));
|
||||||
await $`git fetch origin ${branch}`;
|
await l$`git fetch origin ${branch}`;
|
||||||
await $`git reset --hard origin/${branch}`;
|
commitHash && (await l$`git reset --hard origin/${branch}`);
|
||||||
commitHash && (await $`git checkout ${commitHash}`);
|
commitHash && (await l$`git checkout ${commitHash}`);
|
||||||
|
|
||||||
// Build
|
// Build
|
||||||
await $`make build`;
|
await l$`make build`;
|
||||||
rebuildCaddyfile();
|
rebuildCaddyfile();
|
||||||
|
|
||||||
// Register service
|
// Register service
|
||||||
const systemdServiceName = `deployer2-${host}.service`;
|
const systemdServiceName = `deployer2-${host}.service`;
|
||||||
await $`cat service.template | envsubst > ${serviceDir}/${systemdServiceName}`.env(
|
await l$`cat service.template | envsubst > ${serviceDir}/${systemdServiceName}`.env(
|
||||||
{
|
{
|
||||||
host,
|
host,
|
||||||
port: port.toString(),
|
port: port.toString(),
|
||||||
@@ -50,9 +53,9 @@ export const deploy = async ({ host, commitHash }: DeployInstance) => {
|
|||||||
user: isSelf(service) ? "root" : "drm",
|
user: isSelf(service) ? "root" : "drm",
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
await $`ln -sf ${serviceDir}/${systemdServiceName} ${config.systemServicesDir}/${systemdServiceName}`;
|
await l$`ln -sf ${serviceDir}/${systemdServiceName} ${config.systemServicesDir}/${systemdServiceName}`;
|
||||||
|
|
||||||
// Start!
|
// Start!
|
||||||
await $`systemctl daemon-reload`;
|
await l$`systemctl daemon-reload`;
|
||||||
await $`systemctl restart ${systemdServiceName}`;
|
await l$`systemctl restart ${systemdServiceName}`;
|
||||||
};
|
};
|
||||||
|
|||||||
13
src/utils.ts
Normal file
13
src/utils.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { $ } from "bun";
|
||||||
|
|
||||||
|
export const l$ = (strings: TemplateStringsArray, ...values: any[]) => {
|
||||||
|
// Interleave the static strings with the evaluated values
|
||||||
|
const cmd = strings.reduce((acc, str, i) => {
|
||||||
|
const val = values[i - 1];
|
||||||
|
return acc + (i > 0 ? $.escape(val) : "") + str;
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`> ${cmd.trim()}`);
|
||||||
|
|
||||||
|
return $(strings, ...values);
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user