Prune after deployment

This commit is contained in:
2026-02-14 03:06:54 +01:00
parent 8986f7c8a4
commit 3dd6b22c06
+11 -10
View File
@@ -7,7 +7,6 @@ import path from "path";
import fs from "fs"; import fs from "fs";
import pLimit from "p-limit"; import pLimit from "p-limit";
import { randomUUID } from "crypto"; import { randomUUID } from "crypto";
import simpleGit from "simple-git";
import { cloneRepo } from "./git"; import { cloneRepo } from "./git";
const docker = new Docker(); const docker = new Docker();
@@ -35,7 +34,7 @@ function formatPorts(ports: Port[]) {
} }
export async function getDeployments( export async function getDeployments(
apps: Application[] apps: Application[],
): Promise<Deployment[]> { ): Promise<Deployment[]> {
const limit = pLimit(5); const limit = pLimit(5);
@@ -91,27 +90,27 @@ export async function getDeployments(
status: containerInfo.State?.Running ? "running" : "stopped", status: containerInfo.State?.Running ? "running" : "stopped",
ports: [...ports.entries()].map( ports: [...ports.entries()].map(
([publicPort, privatePort]) => ([publicPort, privatePort]) =>
[publicPort, privatePort] as [number, number] [publicPort, privatePort] as [number, number],
), ),
}; };
} catch { } catch {
// Container may have stopped / been removed // Container may have stopped / been removed
return null; return null;
} }
}) }),
) ),
); );
// Remove failed containers // Remove failed containers
return results.filter( return results.filter(
(deployment: any): deployment is Deployment => deployment !== null (deployment: any): deployment is Deployment => deployment !== null,
); );
} }
export async function deployApp( export async function deployApp(
name: string, name: string,
app: Application, app: Application,
ports: Port[] ports: Port[],
): Promise<string | undefined> { ): Promise<string | undefined> {
const deployId = randomUUID(); const deployId = randomUUID();
const deployPath = path.join("data", "deploys", deployId); const deployPath = path.join("data", "deploys", deployId);
@@ -134,12 +133,12 @@ export async function deployApp(
context: deployPath, context: deployPath,
src: ["Dockerfile", "."], src: ["Dockerfile", "."],
}, },
{ t: imageTag } { t: imageTag },
); );
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
docker.modem.followProgress(stream, (err: Error | null) => docker.modem.followProgress(stream, (err: Error | null) =>
err ? reject(err) : resolve() err ? reject(err) : resolve(),
); );
}); });
@@ -152,12 +151,14 @@ export async function deployApp(
PortBindings: formatPorts(ports), PortBindings: formatPorts(ports),
}, },
Env: Object.entries(app.env).map( Env: Object.entries(app.env).map(
([key, { value }]) => `${key}="${value.replaceAll('"', '\\"')}"` ([key, { value }]) => `${key}="${value.replaceAll('"', '\\"')}"`,
), ),
}); });
await container.start(); await container.start();
await docker.pruneImages();
return container.id; return container.id;
} catch (err) { } catch (err) {
console.error("Failed to deploy app:", err); console.error("Failed to deploy app:", err);