From 61d9ae397adb0a2704626322f3b9ede40f717ded Mon Sep 17 00:00:00 2001 From: Mauricio Siu Date: Fri, 14 Nov 2025 22:27:38 -0600 Subject: [PATCH] feat: add git commit info extraction to deployment logic - Integrated `getGitCommitInfo` function to retrieve the latest commit message and hash for applications and compose services. - Updated deployment logic to conditionally include commit information in deployment updates, enhancing traceability. - Refactored import statements for better organization and clarity. --- packages/server/src/services/application.ts | 18 ++++++++- packages/server/src/services/compose.ts | 25 +++++++++++- packages/server/src/utils/docker/domain.ts | 2 + packages/server/src/utils/providers/git.ts | 42 +++++++++++++++++++++ 4 files changed, 84 insertions(+), 3 deletions(-) diff --git a/packages/server/src/services/application.ts b/packages/server/src/services/application.ts index 8dc67ddb6..a3eb959b4 100644 --- a/packages/server/src/services/application.ts +++ b/packages/server/src/services/application.ts @@ -18,7 +18,10 @@ import { } from "@dokploy/server/utils/process/execAsync"; import { cloneBitbucketRepository } from "@dokploy/server/utils/providers/bitbucket"; import { buildRemoteDocker } from "@dokploy/server/utils/providers/docker"; -import { cloneGitRepository } from "@dokploy/server/utils/providers/git"; +import { + cloneGitRepository, + getGitCommitInfo, +} from "@dokploy/server/utils/providers/git"; import { cloneGiteaRepository } from "@dokploy/server/utils/providers/gitea"; import { cloneGithubRepository } from "@dokploy/server/utils/providers/github"; import { cloneGitlabRepository } from "@dokploy/server/utils/providers/gitlab"; @@ -29,6 +32,7 @@ import { getDokployUrl } from "./admin"; import { createDeployment, createDeploymentPreview, + updateDeployment, updateDeploymentStatus, } from "./deployment"; import { type Domain, getDomainHost } from "./domain"; @@ -243,6 +247,18 @@ export const deployApplication = async ({ }); throw error; + } finally { + // Only extract commit info for non-docker sources + if (application.sourceType !== "docker") { + const commitInfo = await getGitCommitInfo(application); + + if (commitInfo) { + await updateDeployment(deployment.deploymentId, { + title: commitInfo.message, + description: `Commit: ${commitInfo.hash}`, + }); + } + } } return true; }; diff --git a/packages/server/src/services/compose.ts b/packages/server/src/services/compose.ts index 2e2a2fc59..519a0c404 100644 --- a/packages/server/src/services/compose.ts +++ b/packages/server/src/services/compose.ts @@ -22,7 +22,10 @@ import { execAsyncRemote, } from "@dokploy/server/utils/process/execAsync"; import { cloneBitbucketRepository } from "@dokploy/server/utils/providers/bitbucket"; -import { cloneGitRepository } from "@dokploy/server/utils/providers/git"; +import { + cloneGitRepository, + getGitCommitInfo, +} from "@dokploy/server/utils/providers/git"; import { cloneGiteaRepository } from "@dokploy/server/utils/providers/gitea"; import { cloneGithubRepository } from "@dokploy/server/utils/providers/github"; import { cloneGitlabRepository } from "@dokploy/server/utils/providers/gitlab"; @@ -30,7 +33,11 @@ import { getCreateComposeFileCommand } from "@dokploy/server/utils/providers/raw import { TRPCError } from "@trpc/server"; import { eq } from "drizzle-orm"; import { getDokployUrl } from "./admin"; -import { createDeploymentCompose, updateDeploymentStatus } from "./deployment"; +import { + createDeploymentCompose, + updateDeployment, + updateDeploymentStatus, +} from "./deployment"; import { validUniqueServerAppName } from "./project"; export type Compose = typeof compose.$inferSelect; @@ -239,6 +246,7 @@ export const deployCompose = async ({ await execAsync(commandWithLog); } + command = "set -e;"; command += await getBuildComposeCommand(entity); commandWithLog = `(${command}) >> ${deployment.logPath} 2>&1`; if (compose.serverId) { @@ -275,6 +283,19 @@ export const deployCompose = async ({ organizationId: compose.environment.project.organizationId, }); throw error; + } finally { + if (compose.sourceType !== "raw") { + const commitInfo = await getGitCommitInfo({ + ...compose, + type: "compose", + }); + if (commitInfo) { + await updateDeployment(deployment.deploymentId, { + title: commitInfo.message, + description: `Commit: ${commitInfo.hash}`, + }); + } + } } }; diff --git a/packages/server/src/utils/docker/domain.ts b/packages/server/src/utils/docker/domain.ts index ffe900302..a176a4560 100644 --- a/packages/server/src/utils/docker/domain.ts +++ b/packages/server/src/utils/docker/domain.ts @@ -131,6 +131,8 @@ exit 1; exit 1; `; } + + return ""; }; export const addDomainToCompose = async ( compose: Compose, diff --git a/packages/server/src/utils/providers/git.ts b/packages/server/src/utils/providers/git.ts index 19c8ab8a0..8e640892d 100644 --- a/packages/server/src/utils/providers/git.ts +++ b/packages/server/src/utils/providers/git.ts @@ -4,6 +4,7 @@ import { findSSHKeyById, updateSSHKeyById, } from "@dokploy/server/services/ssh-key"; +import { execAsync, execAsyncRemote } from "../process/execAsync"; interface CloneGitRepository { appName: string; @@ -145,3 +146,44 @@ const sanitizeRepoPathSSH = (input: string) => { }, }; }; + +interface Props { + appName: string; + type?: "application" | "compose"; + serverId: string | null; +} + +export const getGitCommitInfo = async ({ + appName, + type = "application", + serverId, +}: Props) => { + const { COMPOSE_PATH, APPLICATIONS_PATH } = paths(!!serverId); + const basePath = type === "compose" ? COMPOSE_PATH : APPLICATIONS_PATH; + const outputPath = join(basePath, appName, "code"); + let stdoutResult = ""; + const result = { + message: "", + hash: "", + }; + try { + const gitCommand = `git -C ${outputPath} log -1 --pretty=format:"%H---DELIMITER---%B"`; + if (serverId) { + const { stdout } = await execAsyncRemote(serverId, gitCommand); + stdoutResult = stdout.trim(); + } else { + const { stdout } = await execAsync(gitCommand); + stdoutResult = stdout.trim(); + } + + const parts = stdoutResult.split("---DELIMITER---"); + if (parts && parts.length === 2) { + result.hash = parts[0]?.trim() || ""; + result.message = parts[1]?.trim() || ""; + } + } catch (error) { + console.error(`Error getting git commit info: ${error}`); + return null; + } + return result; +};