feat: implement rollback functionality with UI components and database schema updates

- Added ShowEnv and ShowRollbackSettings components for displaying and configuring rollback settings.
- Implemented ShowRollbacks component to list and manage rollbacks for applications.
- Created rollback database schema and updated application schema to include rollback settings.
- Added API routes for managing rollbacks, including fetching, creating, and deleting rollbacks.
- Integrated rollback functionality into the application deployment process.
This commit is contained in:
Mauricio Siu
2025-05-10 20:28:34 -06:00
parent aa475e6123
commit 2ad8bf355b
17 changed files with 12317 additions and 2 deletions

View File

@@ -27,7 +27,7 @@ import { server } from "./server";
import { applicationStatus, certificateType, triggerType } from "./shared";
import { sshKeys } from "./ssh-key";
import { generateAppName } from "./utils";
import { rollbacks } from "./rollbacks";
export const sourceType = pgEnum("sourceType", [
"docker",
"git",
@@ -132,6 +132,8 @@ export const applications = pgTable("application", {
isPreviewDeploymentsActive: boolean("isPreviewDeploymentsActive").default(
false,
),
rollbackActive: boolean("rollbackActive").default(false),
limitRollback: integer("limitRollback").default(5),
buildArgs: text("buildArgs"),
memoryReservation: text("memoryReservation"),
memoryLimit: text("memoryLimit"),
@@ -274,6 +276,7 @@ export const applicationsRelations = relations(
references: [server.serverId],
}),
previewDeployments: many(previewDeployments),
rollbacks: many(rollbacks),
}),
);

View File

@@ -32,3 +32,4 @@ export * from "./preview-deployments";
export * from "./ai";
export * from "./account";
export * from "./schedule";
export * from "./rollbacks";

View File

@@ -0,0 +1,45 @@
import { relations } from "drizzle-orm";
import { pgTable, serial, text } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { nanoid } from "nanoid";
import { z } from "zod";
import { applications } from "./application";
export const rollbacks = pgTable("rollback", {
rollbackId: text("rollbackId")
.notNull()
.primaryKey()
.$defaultFn(() => nanoid()),
env: text("env"),
applicationId: text("applicationId")
.notNull()
.references(() => applications.applicationId, {
onDelete: "cascade",
}),
version: serial(),
image: text("image"),
createdAt: text("createdAt")
.notNull()
.$defaultFn(() => new Date().toISOString()),
});
export type Rollback = typeof rollbacks.$inferSelect;
export const rollbacksRelations = relations(rollbacks, ({ one }) => ({
application: one(applications, {
fields: [rollbacks.applicationId],
references: [applications.applicationId],
}),
}));
export const createRollbackSchema = createInsertSchema(rollbacks).extend({
appName: z.string().min(1),
});
export const updateRollbackSchema = createRollbackSchema.extend({
rollbackId: z.string().min(1),
});
export const apiFindOneRollback = z.object({
rollbackId: z.string().min(1),
});

View File

@@ -32,6 +32,7 @@ export * from "./services/gitea";
export * from "./services/server";
export * from "./services/schedule";
export * from "./services/application";
export * from "./services/rollbacks";
export * from "./utils/databases/rebuild";
export * from "./setup/config-paths";
export * from "./setup/postgres-setup";

View File

@@ -41,7 +41,10 @@ import {
import { createTraefikConfig } from "@dokploy/server/utils/traefik/application";
import { TRPCError } from "@trpc/server";
import { eq } from "drizzle-orm";
import { encodeBase64 } from "../utils/docker/utils";
import {
encodeBase64,
prepareEnvironmentVariables,
} from "../utils/docker/utils";
import { getDokployUrl } from "./admin";
import {
createDeployment,
@@ -60,6 +63,7 @@ import {
updatePreviewDeployment,
} from "./preview-deployment";
import { validUniqueServerAppName } from "./project";
import { createRollback } from "./rollbacks";
export type Application = typeof applications.$inferSelect;
export const createApplication = async (
@@ -214,6 +218,21 @@ export const deployApplication = async ({
await updateDeploymentStatus(deployment.deploymentId, "done");
await updateApplicationStatus(applicationId, "done");
if (application.rollbackActive) {
const resolveEnvs = prepareEnvironmentVariables(
application.env,
application.project.env,
);
console.log(resolveEnvs);
await createRollback({
appName: application.appName,
env: resolveEnvs.join("\n"),
applicationId: applicationId,
});
}
await sendBuildSuccessNotifications({
projectName: application.project.name,
applicationName: application.name,

View File

@@ -0,0 +1,145 @@
import { eq } from "drizzle-orm";
import { db } from "../db";
import { type createRollbackSchema, rollbacks } from "../db/schema";
import type { z } from "zod";
import { findApplicationById } from "./application";
import { getRemoteDocker } from "../utils/servers/remote-docker";
import type { ApplicationNested } from "../utils/builders";
import { execAsync, execAsyncRemote } from "../utils/process/execAsync";
import type { CreateServiceOptions } from "dockerode";
export const createRollback = async (
input: z.infer<typeof createRollbackSchema>,
) => {
await db.transaction(async (tx) => {
const rollback = await tx
.insert(rollbacks)
.values(input)
.returning()
.then((res) => res[0]);
if (!rollback) {
throw new Error("Failed to create rollback");
}
const tagImage = `${input.appName}:v${rollback.version}`;
await tx
.update(rollbacks)
.set({
image: tagImage,
})
.where(eq(rollbacks.rollbackId, rollback.rollbackId));
const application = await findApplicationById(input.applicationId);
await createRollbackImage(application, tagImage);
return rollback;
});
};
const findRollbackById = async (rollbackId: string) => {
const result = await db.query.rollbacks.findFirst({
where: eq(rollbacks.rollbackId, rollbackId),
});
if (!result) {
throw new Error("Rollback not found");
}
return result;
};
const createRollbackImage = async (
application: ApplicationNested,
tagImage: string,
) => {
const docker = await getRemoteDocker(application.serverId);
const result = docker.getImage(`${application.appName}:latest`);
const version = tagImage.split(":")[1];
await result.tag({
repo: tagImage,
tag: version,
});
};
const deleteRollbackImage = async (image: string, serverId?: string | null) => {
const command = `docker image rm ${image} --force`;
if (serverId) {
await execAsyncRemote(command, serverId);
} else {
await execAsync(command);
}
};
export const removeRollbackById = async (rollbackId: string) => {
const result = await db
.delete(rollbacks)
.where(eq(rollbacks.rollbackId, rollbackId))
.returning()
.then((res) => res[0]);
if (result?.image) {
try {
const application = await findApplicationById(result.applicationId);
await deleteRollbackImage(result.image, application.serverId);
} catch (error) {
console.error(error);
}
}
return result;
};
export const rollback = async (rollbackId: string) => {
const result = await findRollbackById(rollbackId);
const application = await findApplicationById(result.applicationId);
await rollbackApplication(
application.appName,
result.image || "",
result.env || "",
application.serverId,
);
};
const rollbackApplication = async (
appName: string,
image: string,
env: string,
serverId?: string | null,
) => {
const docker = await getRemoteDocker(serverId);
const settings: CreateServiceOptions = {
Name: appName,
TaskTemplate: {
ContainerSpec: {
Image: image,
Env: env.split("\n"),
},
},
};
try {
const service = docker.getService(appName);
const inspect = await service.inspect();
await service.update({
version: Number.parseInt(inspect.Version.Index),
...settings,
TaskTemplate: {
...settings.TaskTemplate,
ForceUpdate: inspect.Spec.TaskTemplate.ForceUpdate + 1,
},
});
} catch (_error: unknown) {
await docker.createService(settings);
}
};