From 1a28e65e50017987765045916ecb79869bdf7650 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 1 Dec 2022 15:51:18 +0100 Subject: [PATCH] feat: revert to remote image --- apps/api/src/jobs/deployApplication.ts | 56 ++++++++++++++--- .../routes/api/v1/applications/handlers.ts | 39 +++++++++--- .../routes/applications/[id]/revert.svelte | 61 +++++++++++++++---- 3 files changed, 125 insertions(+), 31 deletions(-) diff --git a/apps/api/src/jobs/deployApplication.ts b/apps/api/src/jobs/deployApplication.ts index 48e3a3173..5fa01cd45 100644 --- a/apps/api/src/jobs/deployApplication.ts +++ b/apps/api/src/jobs/deployApplication.ts @@ -214,7 +214,6 @@ import * as buildpacks from '../lib/buildPacks'; return; } - const originalApplicationId = application.id const { id: applicationId, @@ -259,7 +258,16 @@ import * as buildpacks from '../lib/buildPacks'; let imageId = applicationId; let domain = getDomain(fqdn); - let tag = null + + let location = null; + + let tag = null; + let customTag = null; + let imageName = null; + + let imageFoundLocally = false; + let imageFoundRemotely = false; + if (pullmergeRequestId) { const previewApplications = await prisma.previewApplication.findMany({ where: { applicationId: originalApplicationId, pullmergeRequestId } }) if (previewApplications.length > 0) { @@ -363,6 +371,12 @@ import * as buildpacks from '../lib/buildPacks'; tag = `${commit.slice(0, 7)}-${pullmergeRequestId}`; } + customTag = application.dockerRegistryImageName.split(':')[1] || tag; + if (pullmergeRequestId) { + customTag = `${customTag}-${pullmergeRequestId}`; + } + imageName = application.dockerRegistryImageName.split(':')[0]; + try { await prisma.build.update({ where: { id: buildId }, data: { commit } }); } catch (err) { } @@ -380,16 +394,32 @@ import * as buildpacks from '../lib/buildPacks'; deployNeeded = true; } - let imageFound = false; try { await executeDockerCmd({ dockerId: destinationDocker.id, command: `docker image inspect ${applicationId}:${tag}` }) - imageFound = true; + imageFoundLocally = true; } catch (error) { // } + + const { url, username, password } = dockerRegistry + location = await saveDockerRegistryCredentials({ url, username, password, workdir }) + + try { + await executeDockerCmd({ + dockerId: destinationDocker.id, + command: `docker ${location ? `--config ${location}` : ''} pull ${imageName}:${customTag}` + }) + imageFoundRemotely = true; + } catch (error) { + // + } + let imageFound = `${applicationId}:${tag}` + if (imageFoundRemotely) { + imageFound = `${imageName}:${customTag}` + } await copyBaseConfigurationFiles(buildPack, workdir, buildId, applicationId, baseImage); const labels = makeLabelForStandaloneApplication({ applicationId, @@ -410,7 +440,7 @@ import * as buildpacks from '../lib/buildPacks'; publishDirectory }); if (forceRebuild) deployNeeded = true - if (!imageFound || deployNeeded) { + if ((!imageFoundLocally && !imageFoundRemotely) || deployNeeded) { if (buildpacks[buildPack]) await buildpacks[buildPack]({ dockerId: destinationDocker.id, @@ -456,7 +486,15 @@ import * as buildpacks from '../lib/buildPacks'; throw new Error(`Build pack ${buildPack} not found.`); } } else { - await saveBuildLog({ line: 'Build image already available - no rebuild required.', buildId, applicationId }); + if (imageFoundRemotely) { + await saveBuildLog({ line: `Container image ${imageFound} found in Docker Registry - reuising it`, buildId, applicationId }); + } else { + if (imageFoundLocally) { + await saveBuildLog({ line: `Container image ${imageFound} found locally - reuising it`, buildId, applicationId }); + } + } + + } if (buildPack === 'compose') { @@ -550,7 +588,7 @@ import * as buildpacks from '../lib/buildPacks'; version: '3.8', services: { [imageId]: { - image: `${applicationId}:${tag}`, + image: imageFound, container_name: imageId, volumes, env_file: envFound ? [`${workdir}/.env`] : [], @@ -610,9 +648,7 @@ import * as buildpacks from '../lib/buildPacks'; return; } try { - if (application.dockerRegistryImageName) { - const customTag = application.dockerRegistryImageName.split(':')[1] || tag; - const imageName = application.dockerRegistryImageName.split(':')[0]; + if (application.dockerRegistryImageName && (!imageFoundRemotely || forceRebuild)) { await saveBuildLog({ line: `Pushing ${imageName}:${customTag} to Docker Registry... It could take a while...`, buildId, applicationId: application.id }); await pushToRegistry(application, workdir, tag, imageName, customTag) await saveBuildLog({ line: "Success 🎉", buildId, applicationId: application.id }); diff --git a/apps/api/src/routes/api/v1/applications/handlers.ts b/apps/api/src/routes/api/v1/applications/handlers.ts index bcebb21d8..f44b53bc3 100644 --- a/apps/api/src/routes/api/v1/applications/handlers.ts +++ b/apps/api/src/routes/api/v1/applications/handlers.ts @@ -7,7 +7,7 @@ import yaml from 'js-yaml'; import csv from 'csvtojson'; import { day } from '../../../../lib/dayjs'; -import { setDefaultBaseImage, setDefaultConfiguration } from '../../../../lib/buildPacks/common'; +import { saveDockerRegistryCredentials, setDefaultBaseImage, setDefaultConfiguration } from '../../../../lib/buildPacks/common'; import { checkDomainsIsValidInDNS, checkExposedPort, createDirectories, decrypt, defaultComposeConfiguration, encrypt, errorHandler, executeDockerCmd, generateSshKeyPair, getContainerUsage, getDomain, isDev, isDomainConfigured, listSettings, prisma, stopBuild, uniqueName } from '../../../../lib/common'; import { checkContainer, formatLabelsOnDocker, removeContainer } from '../../../../lib/docker'; @@ -458,8 +458,8 @@ export async function restartApplication(request: FastifyRequest
-
Revert
+
+ Revert +
-
- You can revert application to a previously built image. Currently only locally stored images - supported. -
-
-
+
If you do not want the next commit to overwrite the reverted application, temporary disable Automatic Deployment feature here.
{#if imagesAvailables.length > 0} +
Local Images
@@ -104,7 +127,7 @@ {#if image.repository + ':' + image.tag !== runningImage} revertToLocal(image)}>Revert Now {:else} +