mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
Merge pull request #1204 from gentslava/fix/issue_1198-muliple_preview_deployments
fix: Multiple Preview Deployments
This commit is contained in:
@@ -182,8 +182,9 @@ export default async function handler(
|
|||||||
}
|
}
|
||||||
} else if (req.headers["x-github-event"] === "pull_request") {
|
} else if (req.headers["x-github-event"] === "pull_request") {
|
||||||
const prId = githubBody?.pull_request?.id;
|
const prId = githubBody?.pull_request?.id;
|
||||||
|
const action = githubBody?.action;
|
||||||
|
|
||||||
if (githubBody?.action === "closed") {
|
if (action === "closed") {
|
||||||
const previewDeploymentResult =
|
const previewDeploymentResult =
|
||||||
await findPreviewDeploymentsByPullRequestId(prId);
|
await findPreviewDeploymentsByPullRequestId(prId);
|
||||||
|
|
||||||
@@ -201,79 +202,86 @@ export default async function handler(
|
|||||||
res.status(200).json({ message: "Preview Deployment Closed" });
|
res.status(200).json({ message: "Preview Deployment Closed" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// opened or synchronize or reopened
|
// opened or synchronize or reopened
|
||||||
const repository = githubBody?.repository?.name;
|
if (
|
||||||
const deploymentHash = githubBody?.pull_request?.head?.sha;
|
action === "opened" ||
|
||||||
const branch = githubBody?.pull_request?.base?.ref;
|
action === "synchronize" ||
|
||||||
const owner = githubBody?.repository?.owner?.login;
|
action === "reopened"
|
||||||
|
) {
|
||||||
|
const repository = githubBody?.repository?.name;
|
||||||
|
const deploymentHash = githubBody?.pull_request?.head?.sha;
|
||||||
|
const branch = githubBody?.pull_request?.base?.ref;
|
||||||
|
const owner = githubBody?.repository?.owner?.login;
|
||||||
|
|
||||||
const apps = await db.query.applications.findMany({
|
const apps = await db.query.applications.findMany({
|
||||||
where: and(
|
where: and(
|
||||||
eq(applications.sourceType, "github"),
|
eq(applications.sourceType, "github"),
|
||||||
eq(applications.repository, repository),
|
eq(applications.repository, repository),
|
||||||
eq(applications.branch, branch),
|
eq(applications.branch, branch),
|
||||||
eq(applications.isPreviewDeploymentsActive, true),
|
eq(applications.isPreviewDeploymentsActive, true),
|
||||||
eq(applications.owner, owner),
|
eq(applications.owner, owner),
|
||||||
),
|
),
|
||||||
with: {
|
with: {
|
||||||
previewDeployments: true,
|
previewDeployments: true,
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const prBranch = githubBody?.pull_request?.head?.ref;
|
|
||||||
|
|
||||||
const prNumber = githubBody?.pull_request?.number;
|
|
||||||
const prTitle = githubBody?.pull_request?.title;
|
|
||||||
const prURL = githubBody?.pull_request?.html_url;
|
|
||||||
|
|
||||||
for (const app of apps) {
|
|
||||||
const previewLimit = app?.previewLimit || 0;
|
|
||||||
if (app?.previewDeployments?.length > previewLimit) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const previewDeploymentResult =
|
|
||||||
await findPreviewDeploymentByApplicationId(app.applicationId, prId);
|
|
||||||
|
|
||||||
let previewDeploymentId =
|
|
||||||
previewDeploymentResult?.previewDeploymentId || "";
|
|
||||||
|
|
||||||
if (!previewDeploymentResult) {
|
|
||||||
const previewDeployment = await createPreviewDeployment({
|
|
||||||
applicationId: app.applicationId as string,
|
|
||||||
branch: prBranch,
|
|
||||||
pullRequestId: prId,
|
|
||||||
pullRequestNumber: prNumber,
|
|
||||||
pullRequestTitle: prTitle,
|
|
||||||
pullRequestURL: prURL,
|
|
||||||
});
|
|
||||||
previewDeploymentId = previewDeployment.previewDeploymentId;
|
|
||||||
}
|
|
||||||
|
|
||||||
const jobData: DeploymentJob = {
|
|
||||||
applicationId: app.applicationId as string,
|
|
||||||
titleLog: "Preview Deployment",
|
|
||||||
descriptionLog: `Hash: ${deploymentHash}`,
|
|
||||||
type: "deploy",
|
|
||||||
applicationType: "application-preview",
|
|
||||||
server: !!app.serverId,
|
|
||||||
previewDeploymentId,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (IS_CLOUD && app.serverId) {
|
|
||||||
jobData.serverId = app.serverId;
|
|
||||||
await deploy(jobData);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
await myQueue.add(
|
|
||||||
"deployments",
|
|
||||||
{ ...jobData },
|
|
||||||
{
|
|
||||||
removeOnComplete: true,
|
|
||||||
removeOnFail: true,
|
|
||||||
},
|
},
|
||||||
);
|
});
|
||||||
|
|
||||||
|
const prBranch = githubBody?.pull_request?.head?.ref;
|
||||||
|
|
||||||
|
const prNumber = githubBody?.pull_request?.number;
|
||||||
|
const prTitle = githubBody?.pull_request?.title;
|
||||||
|
const prURL = githubBody?.pull_request?.html_url;
|
||||||
|
|
||||||
|
for (const app of apps) {
|
||||||
|
const previewLimit = app?.previewLimit || 0;
|
||||||
|
if (app?.previewDeployments?.length > previewLimit) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const previewDeploymentResult =
|
||||||
|
await findPreviewDeploymentByApplicationId(app.applicationId, prId);
|
||||||
|
|
||||||
|
let previewDeploymentId =
|
||||||
|
previewDeploymentResult?.previewDeploymentId || "";
|
||||||
|
|
||||||
|
if (!previewDeploymentResult) {
|
||||||
|
const previewDeployment = await createPreviewDeployment({
|
||||||
|
applicationId: app.applicationId as string,
|
||||||
|
branch: prBranch,
|
||||||
|
pullRequestId: prId,
|
||||||
|
pullRequestNumber: prNumber,
|
||||||
|
pullRequestTitle: prTitle,
|
||||||
|
pullRequestURL: prURL,
|
||||||
|
});
|
||||||
|
previewDeploymentId = previewDeployment.previewDeploymentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
const jobData: DeploymentJob = {
|
||||||
|
applicationId: app.applicationId as string,
|
||||||
|
titleLog: "Preview Deployment",
|
||||||
|
descriptionLog: `Hash: ${deploymentHash}`,
|
||||||
|
type: "deploy",
|
||||||
|
applicationType: "application-preview",
|
||||||
|
server: !!app.serverId,
|
||||||
|
previewDeploymentId,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (IS_CLOUD && app.serverId) {
|
||||||
|
jobData.serverId = app.serverId;
|
||||||
|
await deploy(jobData);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
await myQueue.add(
|
||||||
|
"deployments",
|
||||||
|
{ ...jobData },
|
||||||
|
{
|
||||||
|
removeOnComplete: true,
|
||||||
|
removeOnFail: true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return res.status(200).json({ message: "Apps Deployed" });
|
||||||
}
|
}
|
||||||
return res.status(200).json({ message: "Apps Deployed" });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.status(400).json({ message: "No Actions matched" });
|
return res.status(400).json({ message: "No Actions matched" });
|
||||||
|
|||||||
Reference in New Issue
Block a user