mirror of
https://github.com/ershisan99/coolify.git
synced 2026-01-21 20:52:10 +00:00
v1.0.16 (#51)
This commit is contained in:
62
src/routes/api/v1/application/config/previewDeployment.ts
Normal file
62
src/routes/api/v1/application/config/previewDeployment.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { updateServiceLabels } from '$lib/api/applications/configuration';
|
||||
import { execShellAsync } from '$lib/api/common';
|
||||
import { docker } from '$lib/api/docker';
|
||||
import ApplicationLog from '$models/ApplicationLog';
|
||||
import Configuration from '$models/Configuration';
|
||||
import Deployment from '$models/Deployment';
|
||||
import type { Request } from '@sveltejs/kit';
|
||||
|
||||
export async function post(request: Request) {
|
||||
const { name, organization, branch, isPreviewDeploymentEnabled }: any = request.body || {};
|
||||
if (name && organization && branch) {
|
||||
const configuration = await Configuration.findOneAndUpdate({
|
||||
'repository.name': name,
|
||||
'repository.organization': organization,
|
||||
'repository.branch': branch
|
||||
}, { $set: { 'general.isPreviewDeploymentEnabled': isPreviewDeploymentEnabled, 'general.pullRequest': 0 } }, { new: true }).select('-_id -__v -createdAt -updatedAt')
|
||||
if (!isPreviewDeploymentEnabled) {
|
||||
const found = await Configuration.find({
|
||||
'repository.name': name,
|
||||
'repository.organization': organization,
|
||||
'repository.branch': branch,
|
||||
'general.pullRequest': { '$ne': 0 }
|
||||
})
|
||||
for (const prDeployment of found) {
|
||||
await Configuration.findOneAndRemove({
|
||||
'repository.name': name,
|
||||
'repository.organization': organization,
|
||||
'repository.branch': branch,
|
||||
'publish.domain': prDeployment.publish.domain
|
||||
})
|
||||
const deploys = await Deployment.find({ organization, branch, name, domain: prDeployment.publish.domain });
|
||||
for (const deploy of deploys) {
|
||||
await ApplicationLog.deleteMany({ deployId: deploy.deployId });
|
||||
await Deployment.deleteMany({ deployId: deploy.deployId });
|
||||
}
|
||||
await execShellAsync(`docker stack rm ${prDeployment.build.container.name}`);
|
||||
}
|
||||
return {
|
||||
status: 200,
|
||||
body: {
|
||||
organization,
|
||||
name,
|
||||
branch
|
||||
}
|
||||
};
|
||||
}
|
||||
updateServiceLabels(configuration);
|
||||
return {
|
||||
status: 200,
|
||||
body: {
|
||||
success: true
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
return {
|
||||
status: 500,
|
||||
body: {
|
||||
error: 'Cannot save.'
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user