79 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-02-10 15:47:44 +01:00
import { promises as fs } from 'fs';
import { buildCacheImageWithNode, buildImage, checkPnpm } from './common';
2022-02-10 15:47:44 +01:00
const createDockerfile = async (data, image): Promise<void> => {
const {
applicationId,
2022-05-02 14:15:50 +02:00
buildId,
tag,
workdir,
publishDirectory,
port,
installCommand,
buildCommand,
startCommand,
baseDirectory,
secrets,
pullmergeRequestId,
deploymentType,
baseImage
} = 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) {
2022-09-09 15:46:47 +02:00
const isSecretFound = secrets.filter(s => s.name === secret.name && s.isPRMRSecret)
if (isSecretFound.length > 0) {
Dockerfile.push(`ARG ${secret.name}=${isSecretFound[0].value}`);
} else {
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');
}
if (deploymentType === 'node') {
Dockerfile.push(`COPY .${baseDirectory || ''} ./`);
Dockerfile.push(`RUN ${installCommand}`);
2022-02-10 15:47:44 +01:00
Dockerfile.push(`RUN ${buildCommand}`);
Dockerfile.push(`EXPOSE ${port}`);
Dockerfile.push(`CMD ${startCommand}`);
} else if (deploymentType === 'static') {
if (baseImage?.includes('nginx')) {
Dockerfile.push(`COPY /nginx.conf /etc/nginx/nginx.conf`);
}
Dockerfile.push(`COPY --from=${applicationId}:${tag}-cache /app/${publishDirectory} ./`);
Dockerfile.push(`EXPOSE 80`);
2022-02-10 15:47:44 +01:00
}
2022-02-10 15:47:44 +01:00
await fs.writeFile(`${workdir}/Dockerfile`, Dockerfile.join('\n'));
};
export default async function (data) {
try {
const { baseImage, baseBuildImage, deploymentType, buildCommand } = data;
if (deploymentType === 'node') {
await createDockerfile(data, baseImage);
await buildImage(data);
} else if (deploymentType === 'static') {
if (buildCommand) await buildCacheImageWithNode(data, baseBuildImage);
await createDockerfile(data, baseImage);
await buildImage(data);
}
2022-02-10 15:47:44 +01:00
} catch (error) {
throw error;
}
}