Revert double build

This commit is contained in:
Andras Bacsai 2022-03-11 22:48:55 +01:00
parent fa6cf068c7
commit c6b4d04e26
4 changed files with 237 additions and 264 deletions

View File

@ -70,7 +70,7 @@ export async function removeApplication({ id, teamId }) {
export async function getApplicationWebhook({ projectId, branch }) {
try {
let applications = await prisma.application.findMany({
let application = await prisma.application.findFirst({
where: { projectId, branch, settings: { autodeploy: true } },
include: {
destinationDocker: true,
@ -79,7 +79,6 @@ export async function getApplicationWebhook({ projectId, branch }) {
secrets: true
}
});
for (const application of applications) {
if (application.gitSource?.githubApp?.clientSecret) {
application.gitSource.githubApp.clientSecret = decrypt(
application.gitSource.githubApp.clientSecret
@ -111,8 +110,7 @@ export async function getApplicationWebhook({ projectId, branch }) {
return s;
});
}
}
return [...applications];
return { ...application };
} catch (e) {
throw { status: 404, body: { message: e.message } };
}

View File

@ -108,11 +108,9 @@
try {
loading = true;
await post(`/applications/${id}/stop.json`, {});
isRunning = false;
return window.location.reload();
} catch ({ error }) {
return errorNotification(error);
} finally {
loading = false;
}
}
</script>

View File

@ -20,6 +20,7 @@ export const options: RequestHandler = async () => {
export const post: RequestHandler = async (event) => {
try {
const buildId = cuid();
const allowedGithubEvents = ['push', 'pull_request'];
const allowedActions = ['opened', 'reopened', 'synchronize', 'closed'];
const githubEvent = event.request.headers.get('x-github-event')?.toLowerCase();
@ -44,12 +45,9 @@ export const post: RequestHandler = async (event) => {
branch = body.pull_request.head.ref.split('/')[2];
}
const applications = await db.getApplicationWebhook({ projectId, branch });
if (applications.length > 0) {
for (const application of applications) {
const buildId = cuid();
const webhookSecret = application.gitSource.githubApp.webhookSecret;
const applicationFound = await db.getApplicationWebhook({ projectId, branch });
if (applicationFound) {
const webhookSecret = applicationFound.gitSource.githubApp.webhookSecret;
const hmac = crypto.createHmac('sha256', webhookSecret);
const digest = Buffer.from(
'sha256=' + hmac.update(JSON.stringify(body)).digest('hex'),
@ -68,32 +66,32 @@ export const post: RequestHandler = async (event) => {
}
if (githubEvent === 'push') {
if (!application.configHash) {
if (!applicationFound.configHash) {
const configHash = crypto
.createHash('sha256')
.update(
JSON.stringify({
buildPack: application.buildPack,
port: application.port,
installCommand: application.installCommand,
buildCommand: application.buildCommand,
startCommand: application.startCommand
buildPack: applicationFound.buildPack,
port: applicationFound.port,
installCommand: applicationFound.installCommand,
buildCommand: applicationFound.buildCommand,
startCommand: applicationFound.startCommand
})
)
.digest('hex');
await db.prisma.application.update({
where: { id: application.id },
await db.prisma.application.updateMany({
where: { branch, projectId },
data: { configHash }
});
}
await db.prisma.application.update({
where: { id: application.id },
where: { id: applicationFound.id },
data: { updatedAt: new Date() }
});
await buildQueue.add(buildId, {
build_id: buildId,
type: 'webhook_commit',
...application
...applicationFound
});
return {
status: 200,
@ -114,11 +112,11 @@ export const post: RequestHandler = async (event) => {
};
}
if (application.settings.previews) {
if (application.destinationDockerId) {
if (applicationFound.settings.previews) {
if (applicationFound.destinationDockerId) {
const isRunning = await checkContainer(
application.destinationDocker.engine,
application.id
applicationFound.destinationDocker.engine,
applicationFound.id
);
if (!isRunning) {
return {
@ -135,13 +133,13 @@ export const post: RequestHandler = async (event) => {
pullmergeRequestAction === 'synchronize'
) {
await db.prisma.application.update({
where: { id: application.id },
where: { id: applicationFound.id },
data: { updatedAt: new Date() }
});
await buildQueue.add(buildId, {
build_id: buildId,
type: 'webhook_pr',
...application,
...applicationFound,
sourceBranch,
pullmergeRequestId
});
@ -152,9 +150,9 @@ export const post: RequestHandler = async (event) => {
}
};
} else if (pullmergeRequestAction === 'closed') {
if (application.destinationDockerId) {
const id = `${application.id}-${pullmergeRequestId}`;
const engine = application.destinationDocker.engine;
if (applicationFound.destinationDockerId) {
const id = `${applicationFound.id}-${pullmergeRequestId}`;
const engine = applicationFound.destinationDocker.engine;
await removeDestinationDocker({ id, engine });
}
return {
@ -180,13 +178,6 @@ export const post: RequestHandler = async (event) => {
message: 'Not handled event.'
}
};
}
return {
status: 500,
body: {
message: 'No applications configured in Coolify.'
}
};
} catch (err) {
console.log(err);
return {

View File

@ -21,46 +21,42 @@ export const options: RequestHandler = async () => {
export const post: RequestHandler = async (event) => {
const allowedActions = ['opened', 'reopen', 'close', 'open', 'update'];
const body = await event.request.json();
const buildId = cuid();
try {
const { object_kind: objectKind } = body;
if (objectKind === 'push') {
const { ref } = body;
const projectId = Number(body['project_id']);
const branch = ref.split('/')[2];
const applications = await db.getApplicationWebhook({ projectId, branch });
if (applications.length > 0) {
for (const application of applications) {
const buildId = cuid();
if (!application.configHash) {
const applicationFound = await db.getApplicationWebhook({ projectId, branch });
if (applicationFound) {
if (!applicationFound.configHash) {
const configHash = crypto
.createHash('sha256')
.update(
JSON.stringify({
buildPack: application.buildPack,
port: application.port,
installCommand: application.installCommand,
buildCommand: application.buildCommand,
startCommand: application.startCommand
buildPack: applicationFound.buildPack,
port: applicationFound.port,
installCommand: applicationFound.installCommand,
buildCommand: applicationFound.buildCommand,
startCommand: applicationFound.startCommand
})
)
.digest('hex');
await db.prisma.application.update({
where: { id: application.id },
await db.prisma.application.updateMany({
where: { branch, projectId },
data: { configHash }
});
}
await db.prisma.application.update({
where: { id: application.id },
where: { id: applicationFound.id },
data: { updatedAt: new Date() }
});
await buildQueue.add(buildId, {
build_id: buildId,
type: 'webhook_commit',
...application
...applicationFound
});
}
return {
status: 200,
body: {
@ -68,12 +64,6 @@ export const post: RequestHandler = async (event) => {
}
};
}
return {
status: 500,
body: {
message: 'No applications configured in Coolify.'
}
};
} else if (objectKind === 'merge_request') {
const webhookToken = event.request.headers.get('x-gitlab-token');
if (!webhookToken) {
@ -108,15 +98,13 @@ export const post: RequestHandler = async (event) => {
};
}
const applications = await db.getApplicationWebhook({ projectId, branch: targetBranch });
if (applications.length > 0) {
for (const application of applications) {
const buildId = cuid();
if (application.settings.previews) {
if (application.destinationDockerId) {
const applicationFound = await db.getApplicationWebhook({ projectId, branch: targetBranch });
if (applicationFound) {
if (applicationFound.settings.previews) {
if (applicationFound.destinationDockerId) {
const isRunning = await checkContainer(
application.destinationDocker.engine,
application.id
applicationFound.destinationDocker.engine,
applicationFound.id
);
if (!isRunning) {
return {
@ -127,7 +115,7 @@ export const post: RequestHandler = async (event) => {
};
}
}
if (!dev && application.gitSource.gitlabApp.webhookToken !== webhookToken) {
if (!dev && applicationFound.gitSource.gitlabApp.webhookToken !== webhookToken) {
return {
status: 500,
body: {
@ -142,13 +130,13 @@ export const post: RequestHandler = async (event) => {
action === 'update'
) {
await db.prisma.application.update({
where: { id: application.id },
where: { id: applicationFound.id },
data: { updatedAt: new Date() }
});
await buildQueue.add(buildId, {
build_id: buildId,
type: 'webhook_mr',
...application,
...applicationFound,
sourceBranch,
pullmergeRequestId
});
@ -159,9 +147,9 @@ export const post: RequestHandler = async (event) => {
}
};
} else if (action === 'close') {
if (application.destinationDockerId) {
const id = `${application.id}-${pullmergeRequestId}`;
const engine = application.destinationDocker.engine;
if (applicationFound.destinationDockerId) {
const id = `${applicationFound.id}-${pullmergeRequestId}`;
const engine = applicationFound.destinationDocker.engine;
await removeDestinationDocker({ id, engine });
}
@ -173,8 +161,6 @@ export const post: RequestHandler = async (event) => {
};
}
}
}
return {
status: 500,
body: {