first pass
This commit is contained in:
68
src/deploy.ts
Normal file
68
src/deploy.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { $ } from "bun";
|
||||
import path from "path";
|
||||
import { config } from "./config";
|
||||
|
||||
type DeployInstance = {
|
||||
host: string;
|
||||
user: string;
|
||||
repo: string;
|
||||
branch: string;
|
||||
commitHash: string;
|
||||
port: number;
|
||||
};
|
||||
|
||||
export const deploy = async ({
|
||||
host,
|
||||
user,
|
||||
repo,
|
||||
branch,
|
||||
port,
|
||||
commitHash,
|
||||
}: DeployInstance) => {
|
||||
const deploymentId = new Date().toISOString();
|
||||
|
||||
const serviceDir = path.join(config.directory, host);
|
||||
|
||||
// Fetch
|
||||
await $`
|
||||
cd ${serviceDir}
|
||||
mkdir -p src
|
||||
mkdir -p logs
|
||||
git clone \
|
||||
-b ${branch} \
|
||||
http://deployer:${config.token}@${config.giteaUrl}/${user}/${repo} \
|
||||
${serviceDir}/src
|
||||
cd src
|
||||
git fetch origin ${branch}
|
||||
git reset --hard origin/${branch}
|
||||
git checkout ${commitHash}
|
||||
`;
|
||||
|
||||
// Build
|
||||
await $`
|
||||
cd ${serviceDir}/src
|
||||
make build
|
||||
`;
|
||||
|
||||
// Register service
|
||||
const systemdServiceName = `deployer-${host}.service`;
|
||||
await $`
|
||||
cat template.service | envsubst > ${serviceDir}/${systemdServiceName}
|
||||
ln -sf ${serviceDir}/${systemdServiceName} ${config.systemServicesDir}/${systemdServiceName}
|
||||
`.env({
|
||||
host,
|
||||
port: port.toString(),
|
||||
repo,
|
||||
branch,
|
||||
commitHash,
|
||||
deploymentId,
|
||||
logsDir: path.join(serviceDir, "logs"),
|
||||
serviceDir,
|
||||
});
|
||||
|
||||
// Start!
|
||||
await $`
|
||||
systemctl daemon-reload
|
||||
systemctl restart ${systemdServiceName}
|
||||
`;
|
||||
};
|
||||
Reference in New Issue
Block a user