Skip to content

Commit 735b70b

Browse files
authored
Merge pull request #3018 from Dokploy/2508-git-based-deployments-should-have-git-hash-and-commit-message-on-deploy-manually
feat: add git commit info extraction to deployment logic
2 parents ea5d86e + 61d9ae3 commit 735b70b

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

packages/server/src/services/application.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import {
1818
} from "@dokploy/server/utils/process/execAsync";
1919
import { cloneBitbucketRepository } from "@dokploy/server/utils/providers/bitbucket";
2020
import { buildRemoteDocker } from "@dokploy/server/utils/providers/docker";
21-
import { cloneGitRepository } from "@dokploy/server/utils/providers/git";
21+
import {
22+
cloneGitRepository,
23+
getGitCommitInfo,
24+
} from "@dokploy/server/utils/providers/git";
2225
import { cloneGiteaRepository } from "@dokploy/server/utils/providers/gitea";
2326
import { cloneGithubRepository } from "@dokploy/server/utils/providers/github";
2427
import { cloneGitlabRepository } from "@dokploy/server/utils/providers/gitlab";
@@ -29,6 +32,7 @@ import { getDokployUrl } from "./admin";
2932
import {
3033
createDeployment,
3134
createDeploymentPreview,
35+
updateDeployment,
3236
updateDeploymentStatus,
3337
} from "./deployment";
3438
import { type Domain, getDomainHost } from "./domain";
@@ -243,6 +247,18 @@ export const deployApplication = async ({
243247
});
244248

245249
throw error;
250+
} finally {
251+
// Only extract commit info for non-docker sources
252+
if (application.sourceType !== "docker") {
253+
const commitInfo = await getGitCommitInfo(application);
254+
255+
if (commitInfo) {
256+
await updateDeployment(deployment.deploymentId, {
257+
title: commitInfo.message,
258+
description: `Commit: ${commitInfo.hash}`,
259+
});
260+
}
261+
}
246262
}
247263
return true;
248264
};

packages/server/src/services/compose.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,22 @@ import {
2222
execAsyncRemote,
2323
} from "@dokploy/server/utils/process/execAsync";
2424
import { cloneBitbucketRepository } from "@dokploy/server/utils/providers/bitbucket";
25-
import { cloneGitRepository } from "@dokploy/server/utils/providers/git";
25+
import {
26+
cloneGitRepository,
27+
getGitCommitInfo,
28+
} from "@dokploy/server/utils/providers/git";
2629
import { cloneGiteaRepository } from "@dokploy/server/utils/providers/gitea";
2730
import { cloneGithubRepository } from "@dokploy/server/utils/providers/github";
2831
import { cloneGitlabRepository } from "@dokploy/server/utils/providers/gitlab";
2932
import { getCreateComposeFileCommand } from "@dokploy/server/utils/providers/raw";
3033
import { TRPCError } from "@trpc/server";
3134
import { eq } from "drizzle-orm";
3235
import { getDokployUrl } from "./admin";
33-
import { createDeploymentCompose, updateDeploymentStatus } from "./deployment";
36+
import {
37+
createDeploymentCompose,
38+
updateDeployment,
39+
updateDeploymentStatus,
40+
} from "./deployment";
3441
import { validUniqueServerAppName } from "./project";
3542

3643
export type Compose = typeof compose.$inferSelect;
@@ -239,6 +246,7 @@ export const deployCompose = async ({
239246
await execAsync(commandWithLog);
240247
}
241248

249+
command = "set -e;";
242250
command += await getBuildComposeCommand(entity);
243251
commandWithLog = `(${command}) >> ${deployment.logPath} 2>&1`;
244252
if (compose.serverId) {
@@ -275,6 +283,19 @@ export const deployCompose = async ({
275283
organizationId: compose.environment.project.organizationId,
276284
});
277285
throw error;
286+
} finally {
287+
if (compose.sourceType !== "raw") {
288+
const commitInfo = await getGitCommitInfo({
289+
...compose,
290+
type: "compose",
291+
});
292+
if (commitInfo) {
293+
await updateDeployment(deployment.deploymentId, {
294+
title: commitInfo.message,
295+
description: `Commit: ${commitInfo.hash}`,
296+
});
297+
}
298+
}
278299
}
279300
};
280301

packages/server/src/utils/docker/domain.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ exit 1;
131131
exit 1;
132132
`;
133133
}
134+
135+
return "";
134136
};
135137
export const addDomainToCompose = async (
136138
compose: Compose,

packages/server/src/utils/providers/git.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
findSSHKeyById,
55
updateSSHKeyById,
66
} from "@dokploy/server/services/ssh-key";
7+
import { execAsync, execAsyncRemote } from "../process/execAsync";
78

89
interface CloneGitRepository {
910
appName: string;
@@ -145,3 +146,44 @@ const sanitizeRepoPathSSH = (input: string) => {
145146
},
146147
};
147148
};
149+
150+
interface Props {
151+
appName: string;
152+
type?: "application" | "compose";
153+
serverId: string | null;
154+
}
155+
156+
export const getGitCommitInfo = async ({
157+
appName,
158+
type = "application",
159+
serverId,
160+
}: Props) => {
161+
const { COMPOSE_PATH, APPLICATIONS_PATH } = paths(!!serverId);
162+
const basePath = type === "compose" ? COMPOSE_PATH : APPLICATIONS_PATH;
163+
const outputPath = join(basePath, appName, "code");
164+
let stdoutResult = "";
165+
const result = {
166+
message: "",
167+
hash: "",
168+
};
169+
try {
170+
const gitCommand = `git -C ${outputPath} log -1 --pretty=format:"%H---DELIMITER---%B"`;
171+
if (serverId) {
172+
const { stdout } = await execAsyncRemote(serverId, gitCommand);
173+
stdoutResult = stdout.trim();
174+
} else {
175+
const { stdout } = await execAsync(gitCommand);
176+
stdoutResult = stdout.trim();
177+
}
178+
179+
const parts = stdoutResult.split("---DELIMITER---");
180+
if (parts && parts.length === 2) {
181+
result.hash = parts[0]?.trim() || "";
182+
result.message = parts[1]?.trim() || "";
183+
}
184+
} catch (error) {
185+
console.error(`Error getting git commit info: ${error}`);
186+
return null;
187+
}
188+
return result;
189+
};

0 commit comments

Comments
 (0)