60 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-02-10 15:47:44 +01:00
import { buildImage } from '$lib/docker';
import { promises as fs } from 'fs';
import { checkPnpm } from './common';
2022-02-10 15:47:44 +01:00
const createDockerfile = async (data, image): Promise<void> => {
const {
workdir,
port,
installCommand,
buildCommand,
startCommand,
baseDirectory,
secrets,
2022-05-02 14:15:50 +02:00
pullmergeRequestId,
buildId
} = data;
2022-02-10 15:47:44 +01:00
const Dockerfile: Array<string> = [];
const isPnpm = checkPnpm(installCommand, buildCommand, startCommand);
2022-02-10 15:47:44 +01:00
Dockerfile.push(`FROM ${image}`);
2022-03-20 23:51:50 +01:00
Dockerfile.push('WORKDIR /app');
2022-05-02 14:15:50 +02:00
Dockerfile.push(`LABEL coolify.buildId=${buildId}`);
2022-02-10 15:47:44 +01:00
if (secrets.length > 0) {
secrets.forEach((secret) => {
if (secret.isBuildSecret) {
if (pullmergeRequestId) {
if (secret.isPRMRSecret) {
2022-04-01 14:29:06 +02:00
Dockerfile.push(`ARG ${secret.name}=${secret.value}`);
}
} else {
if (!secret.isPRMRSecret) {
2022-04-01 14:29:06 +02:00
Dockerfile.push(`ARG ${secret.name}=${secret.value}`);
}
}
2022-02-10 15:47:44 +01:00
}
});
}
if (isPnpm) {
2022-06-02 21:59:51 +02:00
Dockerfile.push('RUN curl -f https://get.pnpm.io/v6.16.js | node - add --global pnpm@7');
}
Dockerfile.push(`COPY .${baseDirectory || ''} ./`);
2022-02-10 15:47:44 +01:00
Dockerfile.push(`RUN ${installCommand}`);
if (buildCommand) {
Dockerfile.push(`RUN ${buildCommand}`);
}
Dockerfile.push(`EXPOSE ${port}`);
Dockerfile.push(`CMD ${startCommand}`);
await fs.writeFile(`${workdir}/Dockerfile`, Dockerfile.join('\n'));
};
export default async function (data) {
try {
2022-04-26 14:51:08 +02:00
const { baseImage, baseBuildImage } = data;
await createDockerfile(data, baseImage);
2022-02-10 15:47:44 +01:00
await buildImage(data);
} catch (error) {
throw error;
}
}