Refactor deployment logic: remove unused remote preview deployment function, streamline deployment commands, and enhance error handling for Docker image pulling. Update build command generation for Docker source type.

This commit is contained in:
Mauricio Siu
2025-11-09 03:24:13 -06:00
parent f96114ad80
commit a05b75fc67
4 changed files with 42 additions and 211 deletions
@@ -2,7 +2,6 @@ import {
deployApplication, deployApplication,
deployCompose, deployCompose,
deployPreviewApplication, deployPreviewApplication,
deployRemotePreviewApplication,
rebuildApplication, rebuildApplication,
rebuildCompose, rebuildCompose,
updateApplicationStatus, updateApplicationStatus,
@@ -54,24 +53,14 @@ export const deploymentWorker = new Worker(
await updatePreviewDeployment(job.data.previewDeploymentId, { await updatePreviewDeployment(job.data.previewDeploymentId, {
previewStatus: "running", previewStatus: "running",
}); });
if (job.data.server) {
if (job.data.type === "deploy") { if (job.data.type === "deploy") {
await deployRemotePreviewApplication({ await deployPreviewApplication({
applicationId: job.data.applicationId, applicationId: job.data.applicationId,
titleLog: job.data.titleLog, titleLog: job.data.titleLog,
descriptionLog: job.data.descriptionLog, descriptionLog: job.data.descriptionLog,
previewDeploymentId: job.data.previewDeploymentId, previewDeploymentId: job.data.previewDeploymentId,
}); });
}
} else {
if (job.data.type === "deploy") {
await deployPreviewApplication({
applicationId: job.data.applicationId,
titleLog: job.data.titleLog,
descriptionLog: job.data.descriptionLog,
previewDeploymentId: job.data.previewDeploymentId,
});
}
} }
} }
} catch (error) { } catch (error) {
+22 -130
View File
@@ -18,10 +18,7 @@ import {
execAsyncRemote, execAsyncRemote,
} from "@dokploy/server/utils/process/execAsync"; } from "@dokploy/server/utils/process/execAsync";
import { cloneBitbucketRepository } from "@dokploy/server/utils/providers/bitbucket"; import { cloneBitbucketRepository } from "@dokploy/server/utils/providers/bitbucket";
import { import { buildRemoteDocker } from "@dokploy/server/utils/providers/docker";
buildDocker,
buildRemoteDocker,
} from "@dokploy/server/utils/providers/docker";
import { cloneGitRepository } from "@dokploy/server/utils/providers/git"; import { cloneGitRepository } from "@dokploy/server/utils/providers/git";
import { cloneGiteaRepository } from "@dokploy/server/utils/providers/gitea"; import { cloneGiteaRepository } from "@dokploy/server/utils/providers/gitea";
import { cloneGithubRepository } from "@dokploy/server/utils/providers/github"; import { cloneGithubRepository } from "@dokploy/server/utils/providers/github";
@@ -192,9 +189,12 @@ export const deployApplication = async ({
command += await cloneGitRepository(application); command += await cloneGitRepository(application);
} else if (application.sourceType === "drop") { } else if (application.sourceType === "drop") {
await buildApplication(application, deployment.logPath); await buildApplication(application, deployment.logPath);
} else if (application.sourceType === "docker") {
command += await buildRemoteDocker(application);
} }
command += getBuildCommand(application); command += getBuildCommand(application);
const commandWithLog = `(${command}) >> ${deployment.logPath} 2>&1`; const commandWithLog = `(${command}) >> ${deployment.logPath} 2>&1`;
if (application.serverId) { if (application.serverId) {
await execAsyncRemote(application.serverId, commandWithLog); await execAsyncRemote(application.serverId, commandWithLog);
@@ -206,16 +206,16 @@ export const deployApplication = async ({
await updateDeploymentStatus(deployment.deploymentId, "done"); await updateDeploymentStatus(deployment.deploymentId, "done");
await updateApplicationStatus(applicationId, "done"); await updateApplicationStatus(applicationId, "done");
// if (application.rollbackActive) { if (application.rollbackActive) {
// const tagImage = const tagImage =
// application.sourceType === "docker" application.sourceType === "docker"
// ? application.dockerImage ? application.dockerImage
// : application.appName; : application.appName;
// await createRollback({ await createRollback({
// appName: tagImage || "", appName: tagImage || "",
// deploymentId: deployment.deploymentId, deploymentId: deployment.deploymentId,
// }); });
// } }
await sendBuildSuccessNotifications({ await sendBuildSuccessNotifications({
projectName: application.environment.project.name, projectName: application.environment.project.name,
@@ -359,131 +359,23 @@ export const deployPreviewApplication = async ({
application.buildArgs = `${application.previewBuildArgs}\nDOKPLOY_DEPLOY_URL=${previewDeployment?.domain?.host}`; application.buildArgs = `${application.previewBuildArgs}\nDOKPLOY_DEPLOY_URL=${previewDeployment?.domain?.host}`;
application.buildSecrets = `${application.previewBuildSecrets}\nDOKPLOY_DEPLOY_URL=${previewDeployment?.domain?.host}`; application.buildSecrets = `${application.previewBuildSecrets}\nDOKPLOY_DEPLOY_URL=${previewDeployment?.domain?.host}`;
let command = "set -e;";
if (application.sourceType === "github") { if (application.sourceType === "github") {
await cloneGithubRepository({ command += await cloneGithubRepository({
...application, ...application,
appName: previewDeployment.appName, appName: previewDeployment.appName,
branch: previewDeployment.branch, branch: previewDeployment.branch,
logPath: deployment.logPath,
}); });
await buildApplication(application, deployment.logPath); command += getBuildCommand(application);
}
const successComment = getIssueComment(
application.name,
"success",
previewDomain,
);
await updateIssueComment({
...issueParams,
body: `### Dokploy Preview Deployment\n\n${successComment}`,
});
await updateDeploymentStatus(deployment.deploymentId, "done");
await updatePreviewDeployment(previewDeploymentId, {
previewStatus: "done",
});
} catch (error) {
const comment = getIssueComment(application.name, "error", previewDomain);
await updateIssueComment({
...issueParams,
body: `### Dokploy Preview Deployment\n\n${comment}`,
});
await updateDeploymentStatus(deployment.deploymentId, "error");
await updatePreviewDeployment(previewDeploymentId, {
previewStatus: "error",
});
throw error;
}
return true; const commandWithLog = `(${command}) >> ${deployment.logPath} 2>&1`;
}; if (application.serverId) {
await execAsyncRemote(application.serverId, commandWithLog);
export const deployRemotePreviewApplication = async ({ } else {
applicationId, await execAsync(commandWithLog);
titleLog = "Preview Deployment",
descriptionLog = "",
previewDeploymentId,
}: {
applicationId: string;
titleLog: string;
descriptionLog: string;
previewDeploymentId: string;
}) => {
const application = await findApplicationById(applicationId);
const deployment = await createDeploymentPreview({
title: titleLog,
description: descriptionLog,
previewDeploymentId: previewDeploymentId,
});
const previewDeployment =
await findPreviewDeploymentById(previewDeploymentId);
await updatePreviewDeployment(previewDeploymentId, {
createdAt: new Date().toISOString(),
});
const previewDomain = getDomainHost(previewDeployment?.domain as Domain);
const issueParams = {
owner: application?.owner || "",
repository: application?.repository || "",
issue_number: previewDeployment.pullRequestNumber,
comment_id: Number.parseInt(previewDeployment.pullRequestCommentId),
githubId: application?.githubId || "",
};
try {
const commentExists = await issueCommentExists({
...issueParams,
});
if (!commentExists) {
const result = await createPreviewDeploymentComment({
...issueParams,
previewDomain,
appName: previewDeployment.appName,
githubId: application?.githubId || "",
previewDeploymentId,
});
if (!result) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Pull request comment not found",
});
} }
issueParams.comment_id = Number.parseInt(result?.pullRequestCommentId);
}
const buildingComment = getIssueComment(
application.name,
"running",
previewDomain,
);
await updateIssueComment({
...issueParams,
body: `### Dokploy Preview Deployment\n\n${buildingComment}`,
});
application.appName = previewDeployment.appName;
application.env = `${application.previewEnv}\nDOKPLOY_DEPLOY_URL=${previewDeployment?.domain?.host}`;
application.buildArgs = `${application.previewBuildArgs}\nDOKPLOY_DEPLOY_URL=${previewDeployment?.domain?.host}`;
application.buildSecrets = `${application.previewBuildSecrets}\nDOKPLOY_DEPLOY_URL=${previewDeployment?.domain?.host}`;
if (application.serverId) {
let command = "set -e;";
if (application.sourceType === "github") {
command += await cloneGithubRepository({
...application,
appName: previewDeployment.appName,
branch: previewDeployment.branch,
serverId: application.serverId,
logPath: deployment.logPath,
});
}
command += getBuildCommand(application, deployment.logPath);
await execAsyncRemote(application.serverId, command);
await mechanizeDockerContainer(application); await mechanizeDockerContainer(application);
} }
const successComment = getIssueComment( const successComment = getIssueComment(
application.name, application.name,
"success", "success",
@@ -79,6 +79,10 @@ export const buildApplication = async (
export const getBuildCommand = (application: ApplicationNested) => { export const getBuildCommand = (application: ApplicationNested) => {
let command = ""; let command = "";
const { buildType, registry } = application; const { buildType, registry } = application;
if (application.sourceType === "docker") {
return "";
}
switch (buildType) { switch (buildType) {
case "nixpacks": case "nixpacks":
command = getNixpacksCommand(application); command = getNixpacksCommand(application);
+8 -62
View File
@@ -1,60 +1,6 @@
import { createWriteStream } from "node:fs"; import type { ApplicationNested } from "../builders";
import { type ApplicationNested, mechanizeDockerContainer } from "../builders";
import { pullImage } from "../docker/utils";
interface RegistryAuth { export const buildRemoteDocker = async (application: ApplicationNested) => {
username: string;
password: string;
registryUrl: string;
}
export const buildDocker = async (
application: ApplicationNested,
logPath: string,
): Promise<void> => {
const { buildType, dockerImage, username, password } = application;
const authConfig: Partial<RegistryAuth> = {
username: username || "",
password: password || "",
registryUrl: application.registryUrl || "",
};
const writeStream = createWriteStream(logPath, { flags: "a" });
writeStream.write(`\nBuild ${buildType}\n`);
writeStream.write(`Pulling ${dockerImage}: ✅\n`);
try {
if (!dockerImage) {
throw new Error("Docker image not found");
}
await pullImage(
dockerImage,
(data) => {
if (writeStream.writable) {
writeStream.write(`${data}\n`);
}
},
authConfig,
);
await mechanizeDockerContainer(application);
writeStream.write("\nDocker Deployed: ✅\n");
} catch (error) {
writeStream.write(
`❌ Error: ${error instanceof Error ? error.message : String(error)}`,
);
throw error;
} finally {
writeStream.end();
}
};
export const buildRemoteDocker = async (
application: ApplicationNested,
logPath: string,
) => {
const { registryUrl, dockerImage, username, password } = application; const { registryUrl, dockerImage, username, password } = application;
try { try {
@@ -62,25 +8,25 @@ export const buildRemoteDocker = async (
throw new Error("Docker image not found"); throw new Error("Docker image not found");
} }
let command = ` let command = `
echo "Pulling ${dockerImage}" >> ${logPath}; echo "Pulling ${dockerImage}";
`; `;
if (username && password) { if (username && password) {
command += ` command += `
if ! echo "${password}" | docker login --username "${username}" --password-stdin "${registryUrl || ""}" >> ${logPath} 2>&1; then if ! echo "${password}" | docker login --username "${username}" --password-stdin "${registryUrl || ""}" 2>&1; then
echo "❌ Login failed" >> ${logPath}; echo "❌ Login failed";
exit 1; exit 1;
fi fi
`; `;
} }
command += ` command += `
docker pull ${dockerImage} >> ${logPath} 2>> ${logPath} || { docker pull ${dockerImage} 2>&1 || {
echo "❌ Pulling image failed" >> ${logPath}; echo "❌ Pulling image failed";
exit 1; exit 1;
} }
echo "✅ Pulling image completed." >> ${logPath}; echo "✅ Pulling image completed.";
`; `;
return command; return command;
} catch (error) { } catch (error) {