Deploy app

This commit is contained in:
2026-01-03 21:34:35 +01:00
parent 07a37f92df
commit 9d41823e87
9 changed files with 70 additions and 22 deletions
+4 -3
View File
@@ -2,10 +2,11 @@ import { NextRequest, NextResponse } from "next/server";
import testAuth from "../auth";
import { StatusCodes } from "http-status-codes";
import { deployApp, getDeployments } from "@/core/deployment";
import { Application } from "@/lib/types";
export async function POST(req: NextRequest): Promise<NextResponse> {
const auth = await testAuth(req);
const { name, appId }: { name: string; appId: string } = await req.json();
const { name, app }: { name: string; app: Application } = await req.json();
if (auth) {
return NextResponse.json(
@@ -14,14 +15,14 @@ export async function POST(req: NextRequest): Promise<NextResponse> {
);
}
if (!name || !appId) {
if (!name || !app) {
return NextResponse.json(
{ message: "Body missing name, appId, nodeId" },
{ status: StatusCodes.BAD_REQUEST }
);
}
const deployId = await deployApp(name, appId);
const deployId = await deployApp(name, app);
return NextResponse.json({
message: "ok",
+1 -1
View File
@@ -1,7 +1,7 @@
import { NextRequest, NextResponse } from "next/server";
import testAuth from "../auth";
import { StatusCodes } from "http-status-codes";
import { deployApp, getDeployments } from "@/core/deployment";
import { getDeployments } from "@/core/deployment";
export async function POST(req: NextRequest): Promise<NextResponse> {
const auth = await testAuth(req);
+4 -6
View File
@@ -22,11 +22,7 @@ const statusDot = {
export default function DeploymentsPage() {
const { value: apps } = useAsync([], getApps);
const { value: deployments, loading } = useAsync(
[],
async () => (await getDeployments()).filter((d) => d !== null),
[apps]
);
const { value: deployments, loading } = useAsync([], getDeployments, [apps]);
const appNameByContainer = useMemo(() => {
const map = new Map<string, string>();
@@ -46,7 +42,9 @@ export default function DeploymentsPage() {
{/* Header */}
<div className="flex items-center justify-between mb-6">
<Badge>
{loading ? "Loading…" : `${deployments.length} deployments`}
{loading
? "Loading…"
: `${deployments.length} deployment${deployments.length > 0 && 's'}`}
</Badge>
<DeployAppDialog>
+15 -6
View File
@@ -6,6 +6,9 @@ import { CPUusage } from "@/lib/server/calc";
import path from "path";
import fs from "fs";
import pLimit from "p-limit";
import { randomUUID } from "crypto";
import simpleGit from "simple-git";
import { cloneRepo } from "./git";
const docker = new Docker();
@@ -93,23 +96,27 @@ export async function getDeployments(
export async function deployApp(
name: string,
appId: string
app: Application
): Promise<string | undefined> {
const appPath = path.join("data", "apps", appId, "repo");
const imageTag = `dockship/${appId}:latest`;
const deployId = randomUUID();
const deployPath = path.join("data", "deploys", deployId);
const imageTag = `dockship/${deployId}:latest`;
// Clone the repo
await cloneRepo(app.repo, deployPath);
try {
// Check if app exists
fs.accessSync(appPath);
fs.accessSync(deployPath);
} catch {
console.error("Repo does not exist:", appPath);
console.error("Repo does not exist:", deployPath);
return undefined;
}
try {
const stream = await docker.buildImage(
{
context: appPath,
context: deployPath,
src: ["Dockerfile", "."],
},
{ t: imageTag }
@@ -139,5 +146,7 @@ export async function deployApp(
} catch (err) {
console.error("Failed to deploy app:", err);
return undefined;
} finally {
fs.rmSync(deployPath, { recursive: true, force: true });
}
}
+8
View File
@@ -0,0 +1,8 @@
import simpleGit from "simple-git";
import fs from "fs";
export async function cloneRepo(repoUrl: string, deployPath: string) {
fs.mkdirSync(deployPath, { recursive: true });
const git = simpleGit();
await git.clone(repoUrl, deployPath, ["--depth=1"]);
}
+5 -5
View File
@@ -5,7 +5,7 @@ import { getNodes } from "./node";
import axios from "axios";
import { editApp, getApp, getApps } from "./application";
export async function getDeployments(): Promise<(null | Deployment)[]> {
export async function getDeployments(): Promise<Deployment[]> {
const nodes = await getNodes();
const apps = await getApps();
@@ -34,19 +34,19 @@ export async function deployApp(
if (!node) throw new Error("Node not found");
const app = await getApp(appId);
if (!app) throw new Error("App not found");
const deployId = await (
await axios.post(
`http://${node.ip}:3000/api/deploy`,
{ name, appId },
{ name, app },
{
headers: { Authorization: `ApiKey ${node.key}` },
}
)
).data.deployId;
const app = await getApp(appId);
if (!app) throw new Error("(deployId) App not found");
app.deployments.push(deployId);
editApp(appId, app);