logging commands
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,5 +1,6 @@
|
||||
/config.ts
|
||||
/Caddyfile
|
||||
/live
|
||||
|
||||
# ---> Node
|
||||
# Logs
|
||||
|
||||
@@ -4,6 +4,7 @@ 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;
|
||||
@@ -11,33 +12,35 @@ type 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 { user, repo, branch, port } = indexedConfig[hash(service)];
|
||||
|
||||
const deploymentId = new Date().toISOString();
|
||||
|
||||
const serviceDir = path.join(config.directory, host);
|
||||
await $`mkdir -p ${serviceDir}`;
|
||||
await l$`mkdir -p ${serviceDir}`;
|
||||
|
||||
// Fetch
|
||||
$.cwd(serviceDir);
|
||||
const url = `http://deployer:${config.token}@${config.giteaUrl}/${user}/${repo}`;
|
||||
await $`mkdir -p src`;
|
||||
await $`mkdir -p logs`;
|
||||
await $`git clone -b ${branch} ${url} ./src`.nothrow();
|
||||
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 $`git fetch origin ${branch}`;
|
||||
await $`git reset --hard origin/${branch}`;
|
||||
commitHash && (await $`git checkout ${commitHash}`);
|
||||
await l$`git fetch origin ${branch}`;
|
||||
commitHash && (await l$`git reset --hard origin/${branch}`);
|
||||
commitHash && (await l$`git checkout ${commitHash}`);
|
||||
|
||||
// Build
|
||||
await $`make build`;
|
||||
await l$`make build`;
|
||||
rebuildCaddyfile();
|
||||
|
||||
// Register service
|
||||
const systemdServiceName = `deployer2-${host}.service`;
|
||||
await $`cat service.template | envsubst > ${serviceDir}/${systemdServiceName}`.env(
|
||||
await l$`cat service.template | envsubst > ${serviceDir}/${systemdServiceName}`.env(
|
||||
{
|
||||
host,
|
||||
port: port.toString(),
|
||||
@@ -50,9 +53,9 @@ export const deploy = async ({ host, commitHash }: DeployInstance) => {
|
||||
user: isSelf(service) ? "root" : "drm",
|
||||
}
|
||||
);
|
||||
await $`ln -sf ${serviceDir}/${systemdServiceName} ${config.systemServicesDir}/${systemdServiceName}`;
|
||||
await l$`ln -sf ${serviceDir}/${systemdServiceName} ${config.systemServicesDir}/${systemdServiceName}`;
|
||||
|
||||
// Start!
|
||||
await $`systemctl daemon-reload`;
|
||||
await $`systemctl restart ${systemdServiceName}`;
|
||||
await l$`systemctl daemon-reload`;
|
||||
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