From a1b49a3a6b6e498538463eb06fa7b474f126b0bd Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Mon, 26 Sep 2022 10:20:53 +0200 Subject: [PATCH] fix: base directory & docker bp --- apps/api/src/lib/buildPacks/common.ts | 2 +- apps/api/src/lib/buildPacks/docker.ts | 11 ++--- apps/api/src/lib/common.ts | 60 ++++++++++++++++----------- 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/apps/api/src/lib/buildPacks/common.ts b/apps/api/src/lib/buildPacks/common.ts index f89568360..9919cf863 100644 --- a/apps/api/src/lib/buildPacks/common.ts +++ b/apps/api/src/lib/buildPacks/common.ts @@ -383,7 +383,7 @@ export const setDefaultConfiguration = async (data: any) => { if (!publishDirectory) publishDirectory = template?.publishDirectory || null; if (baseDirectory) { if (!baseDirectory.startsWith('/')) baseDirectory = `/${baseDirectory}`; - if (!baseDirectory.endsWith('/')) baseDirectory = `${baseDirectory}/`; + if (baseDirectory.endsWith('/') && baseDirectory !== '/') baseDirectory = baseDirectory.slice(0, -1); } if (dockerFileLocation) { if (!dockerFileLocation.startsWith('/')) dockerFileLocation = `/${dockerFileLocation}`; diff --git a/apps/api/src/lib/buildPacks/docker.ts b/apps/api/src/lib/buildPacks/docker.ts index c017b4bed..e8cfd89bc 100644 --- a/apps/api/src/lib/buildPacks/docker.ts +++ b/apps/api/src/lib/buildPacks/docker.ts @@ -14,12 +14,8 @@ export default async function (data) { dockerFileLocation } = data try { - const file = `${workdir}${dockerFileLocation}`; - let dockerFileOut = `${workdir}`; - if (baseDirectory) { - dockerFileOut = `${workdir}${baseDirectory}`; - workdir = `${workdir}${baseDirectory}`; - } + const file = `${workdir}${baseDirectory}${dockerFileLocation}`; + data.workdir = `${workdir}${baseDirectory}`; const Dockerfile: Array = (await fs.readFile(`${file}`, 'utf8')) .toString() .trim() @@ -28,7 +24,6 @@ export default async function (data) { if (secrets.length > 0) { secrets.forEach((secret) => { if (secret.isBuildSecret) { - // TODO: fix secrets if ( (pullmergeRequestId && secret.isPRMRSecret) || (!pullmergeRequestId && !secret.isPRMRSecret) @@ -45,7 +40,7 @@ export default async function (data) { }); } - await fs.writeFile(`${dockerFileOut}${dockerFileLocation}`, Dockerfile.join('\n')); + await fs.writeFile(`${workdir}${dockerFileLocation}`, Dockerfile.join('\n')); await buildImage(data); } catch (error) { throw error; diff --git a/apps/api/src/lib/common.ts b/apps/api/src/lib/common.ts index 8b95a765e..1b11cd5ac 100644 --- a/apps/api/src/lib/common.ts +++ b/apps/api/src/lib/common.ts @@ -88,39 +88,51 @@ export const asyncExecShellStream = async ({ const subprocess = execaCommand(command, { env: { DOCKER_BUILDKIT: '1', DOCKER_HOST: engine } }); - if (debug) { - subprocess.stdout.on('data', async (data) => { - const stdout = data.toString(); - const array = stdout.split('\n'); - for (const line of array) { - if (line !== '\n' && line !== '') { - debug && await saveBuildLog({ - line: `${line.replace('\n', '')}`, - buildId, - applicationId - }); + const logs = []; + subprocess.stdout.on('data', async (data) => { + const stdout = data.toString(); + const array = stdout.split('\n'); + for (const line of array) { + if (line !== '\n' && line !== '') { + const log = { + line: `${line.replace('\n', '')}`, + buildId, + applicationId + } + logs.push(log); + if (debug) { + await saveBuildLog(log); } } - }); - subprocess.stderr.on('data', async (data) => { - const stderr = data.toString(); - const array = stderr.split('\n'); - for (const line of array) { - if (line !== '\n' && line !== '') { - debug && await saveBuildLog({ - line: `${line.replace('\n', '')}`, - buildId, - applicationId - }); + } + }); + subprocess.stderr.on('data', async (data) => { + const stderr = data.toString(); + const array = stderr.split('\n'); + for (const line of array) { + if (line !== '\n' && line !== '') { + const log = { + line: `${line.replace('\n', '')}`, + buildId, + applicationId + } + logs.push(log); + if (debug) { + await saveBuildLog(log); } } - }); - } + } + }); subprocess.on('exit', async (code) => { await asyncSleep(1000); if (code === 0) { resolve(code); } else { + if (!debug) { + for (const log of logs) { + await saveBuildLog(log); + } + } reject(code); } });