fix: wrong port in case of docker compose

This commit is contained in:
Andras Bacsai 2022-12-09 11:21:25 +01:00
parent 2e3c815e53
commit 95e8b29fa2

View File

@ -1,115 +1,126 @@
import { promises as fs } from 'fs'; import { promises as fs } from 'fs';
import { defaultComposeConfiguration, executeCommand } from '../common'; import { defaultComposeConfiguration, executeCommand } from '../common';
import { buildImage, saveBuildLog } from './common'; import { saveBuildLog } from './common';
import yaml from 'js-yaml'; import yaml from 'js-yaml';
export default async function (data) { export default async function (data) {
let { let {
applicationId, applicationId,
debug, debug,
buildId, buildId,
dockerId, dockerId,
network, network,
volumes, volumes,
labels, labels,
workdir, workdir,
baseDirectory, baseDirectory,
secrets, secrets,
pullmergeRequestId, pullmergeRequestId,
port, dockerComposeConfiguration,
dockerComposeConfiguration, dockerComposeFileLocation
dockerComposeFileLocation } = data;
} = data const fileYaml = `${workdir}${baseDirectory}${dockerComposeFileLocation}`;
const fileYaml = `${workdir}${baseDirectory}${dockerComposeFileLocation}` const dockerComposeRaw = await fs.readFile(fileYaml, 'utf8');
const dockerComposeRaw = await fs.readFile(fileYaml, 'utf8'); const dockerComposeYaml = yaml.load(dockerComposeRaw);
const dockerComposeYaml = yaml.load(dockerComposeRaw) if (!dockerComposeYaml.services) {
if (!dockerComposeYaml.services) { throw 'No Services found in docker-compose file.';
throw 'No Services found in docker-compose file.' }
} const envs = [];
const envs = []; if (secrets.length > 0) {
if (Object.entries(dockerComposeYaml.services).length === 1) { secrets.forEach((secret) => {
envs.push(`PORT=${port}`) if (pullmergeRequestId) {
} const isSecretFound = secrets.filter((s) => s.name === secret.name && s.isPRMRSecret);
if (secrets.length > 0) { if (isSecretFound.length > 0) {
secrets.forEach((secret) => { envs.push(`${secret.name}=${isSecretFound[0].value}`);
if (pullmergeRequestId) { } else {
const isSecretFound = secrets.filter(s => s.name === secret.name && s.isPRMRSecret) envs.push(`${secret.name}=${secret.value}`);
if (isSecretFound.length > 0) { }
envs.push(`${secret.name}=${isSecretFound[0].value}`); } else {
} else { if (!secret.isPRMRSecret) {
envs.push(`${secret.name}=${secret.value}`); envs.push(`${secret.name}=${secret.value}`);
} }
} else { }
if (!secret.isPRMRSecret) { });
envs.push(`${secret.name}=${secret.value}`); }
} await fs.writeFile(`${workdir}/.env`, envs.join('\n'));
} let envFound = false;
}); try {
} envFound = !!(await fs.stat(`${workdir}/.env`));
await fs.writeFile(`${workdir}/.env`, envs.join('\n')); } catch (error) {
let envFound = false; //
try { }
envFound = !!(await fs.stat(`${workdir}/.env`)); const composeVolumes = [];
} catch (error) { if (volumes.length > 0) {
// for (const volume of volumes) {
} let [v, path] = volume.split(':');
const composeVolumes = []; composeVolumes[v] = {
if (volumes.length > 0) { name: v
for (const volume of volumes) { };
let [v, path] = volume.split(':'); }
composeVolumes[v] = { }
name: v,
}
}
}
let networks = {} let networks = {};
for (let [key, value] of Object.entries(dockerComposeYaml.services)) { for (let [key, value] of Object.entries(dockerComposeYaml.services)) {
value['container_name'] = `${applicationId}-${key}` value['container_name'] = `${applicationId}-${key}`;
value['env_file'] = envFound ? [`${workdir}/.env`] : [] value['env_file'] = envFound ? [`${workdir}/.env`] : [];
value['labels'] = labels value['labels'] = labels;
// TODO: If we support separated volume for each service, we need to add it here // TODO: If we support separated volume for each service, we need to add it here
if (value['volumes']?.length > 0) { if (value['volumes']?.length > 0) {
value['volumes'] = value['volumes'].map((volume) => { value['volumes'] = value['volumes'].map((volume) => {
let [v, path, permission] = volume.split(':'); let [v, path, permission] = volume.split(':');
if (!path) { if (!path) {
path = v; path = v;
v = `${applicationId}${v.replace(/\//gi, '-').replace(/\./gi, '')}` v = `${applicationId}${v.replace(/\//gi, '-').replace(/\./gi, '')}`;
} else { } else {
v = `${applicationId}${v.replace(/\//gi, '-').replace(/\./gi, '')}` v = `${applicationId}${v.replace(/\//gi, '-').replace(/\./gi, '')}`;
} }
composeVolumes[v] = { composeVolumes[v] = {
name: v name: v
} };
return `${v}:${path}${permission ? ':' + permission : ''}` return `${v}:${path}${permission ? ':' + permission : ''}`;
}) });
} }
if (volumes.length > 0) { if (volumes.length > 0) {
for (const volume of volumes) { for (const volume of volumes) {
value['volumes'].push(volume) value['volumes'].push(volume);
} }
} }
if (dockerComposeConfiguration[key].port) { if (dockerComposeConfiguration[key].port) {
value['expose'] = [dockerComposeConfiguration[key].port] value['expose'] = [dockerComposeConfiguration[key].port];
} }
if (value['networks']?.length > 0) { if (value['networks']?.length > 0) {
value['networks'].forEach((network) => { value['networks'].forEach((network) => {
networks[network] = { networks[network] = {
name: network name: network
} };
}) });
} }
value['networks'] = [...value['networks'] || '', network] value['networks'] = [...(value['networks'] || ''), network];
dockerComposeYaml.services[key] = { ...dockerComposeYaml.services[key], restart: defaultComposeConfiguration(network).restart, deploy: defaultComposeConfiguration(network).deploy } dockerComposeYaml.services[key] = {
...dockerComposeYaml.services[key],
} restart: defaultComposeConfiguration(network).restart,
if (Object.keys(composeVolumes).length > 0) { deploy: defaultComposeConfiguration(network).deploy
dockerComposeYaml['volumes'] = { ...composeVolumes } };
} }
dockerComposeYaml['networks'] = Object.assign({ ...networks }, { [network]: { external: true } }) if (Object.keys(composeVolumes).length > 0) {
await fs.writeFile(fileYaml, yaml.dump(dockerComposeYaml)); dockerComposeYaml['volumes'] = { ...composeVolumes };
await executeCommand({ debug, buildId, applicationId, dockerId, command: `docker compose --project-directory ${workdir} pull` }) }
await saveBuildLog({ line: 'Pulling images from Compose file...', buildId, applicationId }); dockerComposeYaml['networks'] = Object.assign({ ...networks }, { [network]: { external: true } });
await executeCommand({ debug, buildId, applicationId, dockerId, command: `docker compose --project-directory ${workdir} build --progress plain` }) await fs.writeFile(fileYaml, yaml.dump(dockerComposeYaml));
await saveBuildLog({ line: 'Building images from Compose file...', buildId, applicationId }); await executeCommand({
debug,
buildId,
applicationId,
dockerId,
command: `docker compose --project-directory ${workdir} pull`
});
await saveBuildLog({ line: 'Pulling images from Compose file...', buildId, applicationId });
await executeCommand({
debug,
buildId,
applicationId,
dockerId,
command: `docker compose --project-directory ${workdir} build --progress plain`
});
await saveBuildLog({ line: 'Building images from Compose file...', buildId, applicationId });
} }