Merge pull request #170 from Dokploy/147-add-commit-hash-and-commit-message-in-the-deployment-list

147 add commit hash and commit message in the deployment list
This commit is contained in:
Mauricio Siu 2024-06-27 22:41:56 -06:00 committed by GitHub
commit 39abd7e374
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 84 additions and 7 deletions

View File

@ -86,6 +86,11 @@ export const ShowDeployments = ({ applicationId }: Props) => {
<span className="text-sm text-muted-foreground">
{deployment.title}
</span>
{deployment.description && (
<span className="text-sm text-muted-foreground">
{deployment.description}
</span>
)}
</div>
<div className="flex flex-col items-end gap-2">
<div className="text-sm capitalize text-muted-foreground">

View File

@ -0,0 +1 @@
ALTER TABLE "deployment" ADD COLUMN "description" text;

View File

@ -1 +0,0 @@
ALTER TABLE "user" ALTER COLUMN "token" DROP NOT NULL;

View File

@ -1,5 +1,5 @@
{
"id": "7610c85e-c3e4-4a32-8ce9-7f48b298f956",
"id": "ec852f38-886a-43b4-9295-73984ed8ef45",
"prevId": "2d8d7670-b942-4573-9c44-6e81d2a2fa16",
"version": "6",
"dialect": "postgresql",
@ -465,7 +465,7 @@
"name": "token",
"type": "text",
"primaryKey": false,
"notNull": false
"notNull": true
},
"isRegistered": {
"name": "isRegistered",
@ -1585,6 +1585,12 @@
"primaryKey": false,
"notNull": true
},
"description": {
"name": "description",
"type": "text",
"primaryKey": false,
"notNull": false
},
"status": {
"name": "status",
"type": "deploymentStatus",

View File

@ -124,8 +124,8 @@
{
"idx": 17,
"version": "6",
"when": 1719109531147,
"tag": "0017_yummy_norrin_radd",
"when": 1719547174326,
"tag": "0017_minor_post",
"breakpoints": true
}
]

View File

@ -33,6 +33,7 @@ export default async function handler(
}
const deploymentTitle = extractCommitMessage(req.headers, req.body);
const deploymentHash = extractHash(req.headers, req.body);
const sourceType = application.sourceType;
@ -75,6 +76,7 @@ export default async function handler(
const jobData: DeploymentJob = {
applicationId: application.applicationId as string,
titleLog: deploymentTitle,
descriptionLog: `Hash: ${deploymentHash}`,
type: "deploy",
applicationType: "application",
};
@ -166,6 +168,37 @@ export const extractCommitMessage = (headers: any, body: any) => {
return "NEW CHANGES";
};
export const extractHash = (headers: any, body: any) => {
// GitHub
if (headers["x-github-event"]) {
return body.head_commit ? body.head_commit.id : "";
}
// GitLab
if (headers["x-gitlab-event"]) {
return (
body.checkout_sha ||
(body.commits && body.commits.length > 0
? body.commits[0].id
: "NEW COMMIT")
);
}
// Bitbucket
if (headers["x-event-key"]?.includes("repo:push")) {
return body.push.changes && body.push.changes.length > 0
? body.push.changes[0].new.target.hash
: "NEW COMMIT";
}
// Gitea
if (headers["x-gitea-event"]) {
return body.after || "NEW COMMIT";
}
return "";
};
export const extractBranchName = (headers: any, body: any) => {
if (headers["x-github-event"] || headers["x-gitea-event"]) {
return body?.ref?.replace("refs/heads/", "");

View File

@ -4,7 +4,11 @@ import type { DeploymentJob } from "@/server/queues/deployments-queue";
import { myQueue } from "@/server/queues/queueSetup";
import { eq } from "drizzle-orm";
import type { NextApiRequest, NextApiResponse } from "next";
import { extractBranchName, extractCommitMessage } from "../[refreshToken]";
import {
extractBranchName,
extractCommitMessage,
extractHash,
} from "../[refreshToken]";
import { updateCompose } from "@/server/api/services/compose";
export default async function handler(
@ -34,7 +38,7 @@ export default async function handler(
}
const deploymentTitle = extractCommitMessage(req.headers, req.body);
const deploymentHash = extractHash(req.headers, req.body);
const sourceType = composeResult.sourceType;
if (sourceType === "github") {
@ -61,6 +65,7 @@ export default async function handler(
titleLog: deploymentTitle,
type: "deploy",
applicationType: "compose",
descriptionLog: `Hash: ${deploymentHash}`,
};
await myQueue.add(
"deployments",

View File

@ -162,6 +162,7 @@ export const applicationRouter = createTRPCRouter({
const jobData: DeploymentJob = {
applicationId: input.applicationId,
titleLog: "Rebuild deployment",
descriptionLog: "",
type: "redeploy",
applicationType: "application",
};
@ -294,6 +295,7 @@ export const applicationRouter = createTRPCRouter({
const jobData: DeploymentJob = {
applicationId: input.applicationId,
titleLog: "Manual deployment",
descriptionLog: "",
type: "deploy",
applicationType: "application",
};

View File

@ -138,6 +138,7 @@ export const composeRouter = createTRPCRouter({
titleLog: "Manual deployment",
type: "deploy",
applicationType: "compose",
descriptionLog: "",
};
await myQueue.add(
"deployments",
@ -156,6 +157,7 @@ export const composeRouter = createTRPCRouter({
titleLog: "Rebuild deployment",
type: "redeploy",
applicationType: "compose",
descriptionLog: "",
};
await myQueue.add(
"deployments",

View File

@ -130,15 +130,18 @@ export const updateApplicationStatus = async (
export const deployApplication = async ({
applicationId,
titleLog = "Manual deployment",
descriptionLog = "",
}: {
applicationId: string;
titleLog: string;
descriptionLog: string;
}) => {
const application = await findApplicationById(applicationId);
const admin = await findAdmin();
const deployment = await createDeployment({
applicationId: applicationId,
title: titleLog,
description: descriptionLog,
});
try {
@ -173,14 +176,17 @@ export const deployApplication = async ({
export const rebuildApplication = async ({
applicationId,
titleLog = "Rebuild deployment",
descriptionLog = "",
}: {
applicationId: string;
titleLog: string;
descriptionLog: string;
}) => {
const application = await findApplicationById(applicationId);
const deployment = await createDeployment({
applicationId: applicationId,
title: titleLog,
description: descriptionLog,
});
try {

View File

@ -134,15 +134,18 @@ export const updateCompose = async (
export const deployCompose = async ({
composeId,
titleLog = "Manual deployment",
descriptionLog = "",
}: {
composeId: string;
titleLog: string;
descriptionLog: string;
}) => {
const compose = await findComposeById(composeId);
const admin = await findAdmin();
const deployment = await createDeploymentCompose({
composeId: composeId,
title: titleLog,
description: descriptionLog,
});
try {
@ -170,14 +173,17 @@ export const deployCompose = async ({
export const rebuildCompose = async ({
composeId,
titleLog = "Rebuild deployment",
descriptionLog = "",
}: {
composeId: string;
titleLog: string;
descriptionLog: string;
}) => {
const compose = await findComposeById(composeId);
const deployment = await createDeploymentCompose({
composeId: composeId,
title: titleLog,
description: descriptionLog,
});
try {

View File

@ -60,6 +60,7 @@ export const createDeployment = async (
title: deployment.title || "Deployment",
status: "running",
logPath: logFilePath,
description: deployment.description || "",
})
.returning();
if (deploymentCreate.length === 0 || !deploymentCreate[0]) {
@ -100,6 +101,7 @@ export const createDeploymentCompose = async (
.values({
composeId: deployment.composeId,
title: deployment.title || "Deployment",
description: deployment.description || "",
status: "running",
logPath: logFilePath,
})

View File

@ -18,6 +18,7 @@ export const deployments = pgTable("deployment", {
.primaryKey()
.$defaultFn(() => nanoid()),
title: text("title").notNull(),
description: text("description"),
status: deploymentStatus("status").default("running"),
logPath: text("logPath").notNull(),
applicationId: text("applicationId").references(
@ -49,6 +50,7 @@ const schema = createInsertSchema(deployments, {
logPath: z.string().min(1),
applicationId: z.string(),
composeId: z.string(),
description: z.string().optional(),
});
export const apiCreateDeployment = schema
@ -57,6 +59,7 @@ export const apiCreateDeployment = schema
status: true,
logPath: true,
applicationId: true,
description: true,
})
.extend({
applicationId: z.string().min(1),
@ -68,6 +71,7 @@ export const apiCreateDeploymentCompose = schema
status: true,
logPath: true,
composeId: true,
description: true,
})
.extend({
composeId: z.string().min(1),

View File

@ -10,12 +10,14 @@ type DeployJob =
| {
applicationId: string;
titleLog: string;
descriptionLog: string;
type: "deploy" | "redeploy";
applicationType: "application";
}
| {
composeId: string;
titleLog: string;
descriptionLog: string;
type: "deploy" | "redeploy";
applicationType: "compose";
};
@ -31,11 +33,13 @@ export const deploymentWorker = new Worker(
await rebuildApplication({
applicationId: job.data.applicationId,
titleLog: job.data.titleLog,
descriptionLog: job.data.descriptionLog,
});
} else if (job.data.type === "deploy") {
await deployApplication({
applicationId: job.data.applicationId,
titleLog: job.data.titleLog,
descriptionLog: job.data.descriptionLog,
});
}
} else if (job.data.applicationType === "compose") {
@ -43,11 +47,13 @@ export const deploymentWorker = new Worker(
await deployCompose({
composeId: job.data.composeId,
titleLog: job.data.titleLog,
descriptionLog: job.data.descriptionLog,
});
} else if (job.data.type === "redeploy") {
await rebuildCompose({
composeId: job.data.composeId,
titleLog: job.data.titleLog,
descriptionLog: job.data.descriptionLog,
});
}
}