Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 47 additions & 29 deletions packages/server/src/services/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ export const deployApplication = async ({
descriptionLog: string;
}) => {
const application = await findApplicationById(applicationId);
const serverId = application.buildServerId || application.serverId;
const buildServerId = application.buildServerId || application.serverId;
const buildApplication = {
...application,
serverId: buildServerId,
};

const buildLink = `${await getDokployUrl()}/dashboard/project/${application.environment.projectId}/environment/${application.environmentId}/services/application/${application.applicationId}?tab=deployments`;
const deployment = await createDeployment({
Expand All @@ -185,24 +189,24 @@ export const deployApplication = async ({
try {
let command = "set -e;";
if (application.sourceType === "github") {
command += await cloneGithubRepository(application);
command += await cloneGithubRepository(buildApplication);
} else if (application.sourceType === "gitlab") {
command += await cloneGitlabRepository(application);
command += await cloneGitlabRepository(buildApplication);
} else if (application.sourceType === "gitea") {
command += await cloneGiteaRepository(application);
command += await cloneGiteaRepository(buildApplication);
} else if (application.sourceType === "bitbucket") {
command += await cloneBitbucketRepository(application);
command += await cloneBitbucketRepository(buildApplication);
} else if (application.sourceType === "git") {
command += await cloneGitRepository(application);
command += await cloneGitRepository(buildApplication);
} else if (application.sourceType === "docker") {
command += await buildRemoteDocker(application);
command += await buildRemoteDocker(buildApplication);
}

command += await getBuildCommand(application);
command += await getBuildCommand(buildApplication);

const commandWithLog = `(${command}) >> ${deployment.logPath} 2>&1`;
if (serverId) {
await execAsyncRemote(serverId, commandWithLog);
if (buildServerId) {
await execAsyncRemote(buildServerId, commandWithLog);
} else {
await execAsync(commandWithLog);
}
Expand Down Expand Up @@ -231,8 +235,8 @@ export const deployApplication = async ({
}

command += `echo "\nError occurred ❌, check the logs for details." >> ${deployment.logPath};`;
if (serverId) {
await execAsyncRemote(serverId, command);
if (buildServerId) {
await execAsyncRemote(buildServerId, command);
} else {
await execAsync(command);
}
Expand Down Expand Up @@ -276,7 +280,11 @@ export const rebuildApplication = async ({
descriptionLog: string;
}) => {
const application = await findApplicationById(applicationId);
const serverId = application.buildServerId || application.serverId;
const buildServerId = application.buildServerId || application.serverId;
const buildApplication = {
...application,
serverId: buildServerId,
};
const buildLink = `${await getDokployUrl()}/dashboard/project/${application.environment.projectId}/environment/${application.environmentId}/services/application/${application.applicationId}?tab=deployments`;

const deployment = await createDeployment({
Expand All @@ -288,10 +296,10 @@ export const rebuildApplication = async ({
try {
let command = "set -e;";
// Check case for docker only
command += await getBuildCommand(application);
command += await getBuildCommand(buildApplication);
const commandWithLog = `(${command}) >> ${deployment.logPath} 2>&1`;
if (serverId) {
await execAsyncRemote(serverId, commandWithLog);
if (buildServerId) {
await execAsyncRemote(buildServerId, commandWithLog);
} else {
await execAsync(commandWithLog);
}
Expand Down Expand Up @@ -319,8 +327,8 @@ export const rebuildApplication = async ({
}

command += `echo "\nError occurred ❌, check the logs for details." >> ${deployment.logPath};`;
if (serverId) {
await execAsyncRemote(serverId, command);
if (buildServerId) {
await execAsyncRemote(buildServerId, command);
} else {
await execAsync(command);
}
Expand Down Expand Up @@ -406,18 +414,24 @@ export const deployPreviewApplication = async ({
application.rollbackRegistry = null;
application.registry = null;

const buildServerId = application.buildServerId || application.serverId;
const buildApplication = {
...application,
serverId: buildServerId,
};

let command = "set -e;";
if (application.sourceType === "github") {
command += await cloneGithubRepository({
...application,
...buildApplication,
appName: previewDeployment.appName,
branch: previewDeployment.branch,
});
command += await getBuildCommand(application);
command += await getBuildCommand(buildApplication);

const commandWithLog = `(${command}) >> ${deployment.logPath} 2>&1`;
if (application.serverId) {
await execAsyncRemote(application.serverId, commandWithLog);
if (buildServerId) {
await execAsyncRemote(buildServerId, commandWithLog);
} else {
await execAsync(commandWithLog);
}
Expand Down Expand Up @@ -481,6 +495,7 @@ export const rebuildPreviewApplication = async ({
comment_id: Number.parseInt(previewDeployment.pullRequestCommentId),
githubId: application?.githubId || "",
};
const buildServerId = application.buildServerId || application.serverId;

try {
const commentExists = await issueCommentExists({
Expand Down Expand Up @@ -525,13 +540,17 @@ export const rebuildPreviewApplication = async ({
application.rollbackRegistry = null;
application.registry = null;

const serverId = application.serverId;
const buildApplication = {
...application,
serverId: buildServerId,
};

let command = "set -e;";
// Only rebuild, don't clone repository
command += await getBuildCommand(application);
command += await getBuildCommand(buildApplication);
const commandWithLog = `(${command}) >> ${deployment.logPath} 2>&1`;
if (serverId) {
await execAsyncRemote(serverId, commandWithLog);
if (buildServerId) {
await execAsyncRemote(buildServerId, commandWithLog);
} else {
await execAsync(commandWithLog);
}
Expand Down Expand Up @@ -561,9 +580,8 @@ export const rebuildPreviewApplication = async ({
}

command += `echo "\nError occurred ❌, check the logs for details." >> ${deployment.logPath};`;
const serverId = application.buildServerId || application.serverId;
if (serverId) {
await execAsyncRemote(serverId, command);
if (buildServerId) {
await execAsyncRemote(buildServerId, command);
} else {
await execAsync(command);
}
Expand Down
21 changes: 14 additions & 7 deletions packages/server/src/services/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ export const createDeployment = async (

if (serverId) {
const server = await findServerById(serverId);
const isBuildServer = !!application.buildServerId;

const command = `
mkdir -p ${LOGS_PATH}/${application.appName};
echo "Initializing deployment" >> ${logFilePath};
echo "Building on ${serverId ? "Build Server" : "Dokploy Server"}" >> ${logFilePath};
echo "Building on ${isBuildServer ? "Build Server" : "Dokploy Server"}" >> ${logFilePath};
`;

await execAsyncRemote(server.serverId, command);
Expand Down Expand Up @@ -157,27 +158,30 @@ export const createDeploymentPreview = async (
const previewDeployment = await findPreviewDeploymentById(
deployment.previewDeploymentId,
);
const buildServerId =
previewDeployment?.application?.buildServerId ||
previewDeployment?.application?.serverId;
try {
await removeLastTenDeployments(
deployment.previewDeploymentId,
"previewDeployment",
previewDeployment?.application?.serverId,
buildServerId,
);

const appName = `${previewDeployment.appName}`;
const { LOGS_PATH } = paths(!!previewDeployment?.application?.serverId);
const { LOGS_PATH } = paths(!!buildServerId);
const formattedDateTime = format(new Date(), "yyyy-MM-dd:HH:mm:ss");
const fileName = `${appName}-${formattedDateTime}.log`;
const logFilePath = path.join(LOGS_PATH, appName, fileName);

if (previewDeployment?.application?.serverId) {
const server = await findServerById(
previewDeployment?.application?.serverId,
);
if (buildServerId) {
const server = await findServerById(buildServerId);
const isBuildServer = !!previewDeployment?.application?.buildServerId;

const command = `
mkdir -p ${LOGS_PATH}/${appName};
echo "Initializing deployment" >> ${logFilePath};
echo "Building on ${isBuildServer ? "Build Server" : "Dokploy Server"}" >> ${logFilePath};
`;

await execAsyncRemote(server.serverId, command);
Expand All @@ -197,6 +201,9 @@ export const createDeploymentPreview = async (
description: deployment.description || "",
previewDeploymentId: deployment.previewDeploymentId,
startedAt: new Date().toISOString(),
...(previewDeployment?.application?.buildServerId && {
buildServerId: previewDeployment.application.buildServerId,
}),
})
.returning();
if (deploymentCreate.length === 0 || !deploymentCreate[0]) {
Expand Down