coolify/apps/api/src/lib/buildPacks/static.ts

55 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-02-10 14:47:44 +00:00
import { promises as fs } from 'fs';
2022-12-21 09:11:03 +00:00
import { generateSecrets } from '../common';
2022-07-06 09:02:36 +00:00
import { buildCacheImageWithNode, buildImage } from './common';
2022-02-10 14:47:44 +00:00
const createDockerfile = async (data, image): Promise<void> => {
const {
applicationId,
tag,
workdir,
buildCommand,
baseDirectory,
publishDirectory,
secrets,
2022-04-26 12:51:08 +00:00
pullmergeRequestId,
2022-05-02 12:15:50 +00:00
baseImage,
2022-05-04 13:45:44 +00:00
buildId,
port
} = data;
2022-02-10 14:47:44 +00:00
const Dockerfile: Array<string> = [];
Dockerfile.push(`FROM ${image}`);
2022-11-30 10:18:19 +00:00
if (baseImage?.includes('httpd')) {
Dockerfile.push('WORKDIR /usr/local/apache2/htdocs/');
} else {
Dockerfile.push('WORKDIR /app');
}
2022-05-02 12:15:50 +00:00
Dockerfile.push(`LABEL coolify.buildId=${buildId}`);
2022-02-10 14:47:44 +00:00
if (secrets.length > 0) {
2022-12-21 09:11:03 +00:00
generateSecrets(secrets, pullmergeRequestId, true).forEach((env) => {
Dockerfile.push(env);
2022-02-10 14:47:44 +00:00
});
}
if (buildCommand) {
2022-03-20 22:51:50 +00:00
Dockerfile.push(`COPY --from=${applicationId}:${tag}-cache /app/${publishDirectory} ./`);
2022-02-10 14:47:44 +00:00
} else {
Dockerfile.push(`COPY .${baseDirectory || ''} ./`);
2022-02-10 14:47:44 +00:00
}
if (baseImage?.includes('nginx')) {
2022-04-26 12:51:08 +00:00
Dockerfile.push(`COPY /nginx.conf /etc/nginx/nginx.conf`);
}
2022-05-04 13:45:44 +00:00
Dockerfile.push(`EXPOSE ${port}`);
2022-02-10 14:47:44 +00:00
await fs.writeFile(`${workdir}/Dockerfile`, Dockerfile.join('\n'));
};
export default async function (data) {
try {
2022-04-26 12:51:08 +00:00
const { baseImage, baseBuildImage } = data;
if (data.buildCommand) await buildCacheImageWithNode(data, baseBuildImage);
await createDockerfile(data, baseImage);
2022-02-10 14:47:44 +00:00
await buildImage(data);
} catch (error) {
throw error;
}
}