2022-02-10 15:47:44 +01:00
|
|
|
import { promises as fs } from 'fs';
|
2022-07-08 16:51:58 +02:00
|
|
|
import { buildCacheImageWithNode, buildImage, checkPnpm } from './common';
|
2022-02-10 15:47:44 +01:00
|
|
|
|
|
|
|
const createDockerfile = async (data, image): Promise<void> => {
|
2022-02-20 00:00:31 +01:00
|
|
|
const {
|
2022-07-08 16:51:58 +02:00
|
|
|
applicationId,
|
2022-05-02 14:15:50 +02:00
|
|
|
buildId,
|
2022-07-08 16:51:58 +02:00
|
|
|
tag,
|
2022-02-20 00:00:31 +01:00
|
|
|
workdir,
|
2022-07-08 16:51:58 +02:00
|
|
|
publishDirectory,
|
2022-02-20 00:00:31 +01:00
|
|
|
port,
|
|
|
|
installCommand,
|
|
|
|
buildCommand,
|
|
|
|
startCommand,
|
|
|
|
baseDirectory,
|
|
|
|
secrets,
|
2022-07-08 16:51:58 +02:00
|
|
|
pullmergeRequestId,
|
|
|
|
deploymentType,
|
|
|
|
baseImage
|
2022-02-20 00:00:31 +01:00
|
|
|
} = data;
|
2022-02-10 15:47:44 +01:00
|
|
|
const Dockerfile: Array<string> = [];
|
2022-03-19 15:04:52 +01:00
|
|
|
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) {
|
2022-02-20 00:00:31 +01:00
|
|
|
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}`);
|
2022-02-20 00:00:31 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!secret.isPRMRSecret) {
|
2022-04-01 14:29:06 +02:00
|
|
|
Dockerfile.push(`ARG ${secret.name}=${secret.value}`);
|
2022-02-20 00:00:31 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-10 15:47:44 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-03-11 23:55:57 +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');
|
2022-03-11 23:55:57 +01:00
|
|
|
}
|
2022-07-08 16:51:58 +02:00
|
|
|
if (deploymentType === 'node') {
|
|
|
|
Dockerfile.push(`COPY .${baseDirectory || ''} ./`);
|
|
|
|
Dockerfile.push(`RUN ${installCommand}`);
|
2022-02-10 15:47:44 +01:00
|
|
|
Dockerfile.push(`RUN ${buildCommand}`);
|
2022-07-08 16:51:58 +02:00
|
|
|
Dockerfile.push(`EXPOSE ${port}`);
|
|
|
|
Dockerfile.push(`CMD ${startCommand}`);
|
|
|
|
} else if (deploymentType === 'static') {
|
2022-07-12 09:58:59 +02:00
|
|
|
if (baseImage?.includes('nginx')) {
|
2022-07-08 16:51:58 +02:00
|
|
|
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-07-08 16:51:58 +02:00
|
|
|
|
2022-02-10 15:47:44 +01:00
|
|
|
await fs.writeFile(`${workdir}/Dockerfile`, Dockerfile.join('\n'));
|
|
|
|
};
|
|
|
|
|
|
|
|
export default async function (data) {
|
|
|
|
try {
|
2022-07-08 16:51:58 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|