feat: Ability to change deployment type for nextjs

This commit is contained in:
Andras Bacsai
2022-07-08 16:51:58 +02:00
parent c478c1b7ad
commit 88a62be30c
13 changed files with 163 additions and 48 deletions

View File

@@ -55,7 +55,8 @@ import * as buildpacks from '../lib/buildPacks';
denoOptions,
exposePort,
baseImage,
baseBuildImage
baseBuildImage,
deploymentType
} = message
let {
branch,
@@ -225,7 +226,8 @@ import * as buildpacks from '../lib/buildPacks';
denoMainFile,
denoOptions,
baseImage,
baseBuildImage
baseBuildImage,
deploymentType
});
else {
await saveBuildLog({ line: `Build pack ${buildPack} not found`, buildId, applicationId });

View File

@@ -17,7 +17,7 @@ const nodeBased = [
'nextjs'
];
export function setDefaultBaseImage(buildPack: string | null) {
export function setDefaultBaseImage(buildPack: string | null, deploymentType: string | null) {
const nodeVersions = [
{
value: 'node:lts',
@@ -259,10 +259,17 @@ export function setDefaultBaseImage(buildPack: string | null) {
baseBuildImages: []
};
if (nodeBased.includes(buildPack)) {
payload.baseImage = 'node:lts';
payload.baseImages = nodeVersions;
payload.baseBuildImage = 'node:lts';
payload.baseBuildImages = nodeVersions;
if (deploymentType === 'static') {
payload.baseImage = 'webdevops/nginx:alpine';
payload.baseImages = staticVersions;
payload.baseBuildImage = 'node:lts';
payload.baseBuildImages = nodeVersions;
} else {
payload.baseImage = 'node:lts';
payload.baseImages = nodeVersions;
payload.baseBuildImage = 'node:lts';
payload.baseBuildImages = nodeVersions;
}
}
if (staticApps.includes(buildPack)) {
payload.baseImage = 'webdevops/nginx:alpine';
@@ -431,7 +438,7 @@ export async function copyBaseConfigurationFiles(
buildId,
applicationId
});
} else if (staticApps.includes(buildPack) && baseImage.includes('nginx')) {
} else if (baseImage.includes('nginx')) {
await fs.writeFile(
`${workdir}/nginx.conf`,
`user nginx;

View File

@@ -1,17 +1,22 @@
import { promises as fs } from 'fs';
import { buildImage, checkPnpm } from './common';
import { buildCacheImageWithNode, buildImage, checkPnpm } from './common';
const createDockerfile = async (data, image): Promise<void> => {
const {
applicationId,
buildId,
tag,
workdir,
publishDirectory,
port,
installCommand,
buildCommand,
startCommand,
baseDirectory,
secrets,
pullmergeRequestId
pullmergeRequestId,
deploymentType,
baseImage
} = data;
const Dockerfile: Array<string> = [];
const isPnpm = checkPnpm(installCommand, buildCommand, startCommand);
@@ -36,22 +41,35 @@ const createDockerfile = async (data, image): Promise<void> => {
if (isPnpm) {
Dockerfile.push('RUN curl -f https://get.pnpm.io/v6.16.js | node - add --global pnpm@7');
}
Dockerfile.push(`COPY .${baseDirectory || ''} ./`);
Dockerfile.push(`RUN ${installCommand}`);
if (buildCommand) {
if (deploymentType === 'node') {
Dockerfile.push(`COPY .${baseDirectory || ''} ./`);
Dockerfile.push(`RUN ${installCommand}`);
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`);
}
Dockerfile.push(`EXPOSE ${port}`);
Dockerfile.push(`CMD ${startCommand}`);
await fs.writeFile(`${workdir}/Dockerfile`, Dockerfile.join('\n'));
};
export default async function (data) {
try {
const { baseImage, baseBuildImage } = data;
await createDockerfile(data, baseImage);
await buildImage(data);
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);
}
} catch (error) {
throw error;
}

View File

@@ -31,6 +31,31 @@ export async function listApplications(request: FastifyRequest) {
return errorHandler({ status, message })
}
}
export async function getImages(request: FastifyRequest) {
try {
const { buildPack, deploymentType } = request.body
let publishDirectory = undefined;
let port = undefined
const { baseImage, baseBuildImage, baseBuildImages, baseImages, } = setDefaultBaseImage(
buildPack, deploymentType
);
if (buildPack === 'nextjs') {
if (deploymentType === 'static') {
publishDirectory = 'out'
port = '80'
} else {
publishDirectory = ''
port = '3000'
}
}
return { baseImage, baseBuildImage, baseBuildImages, baseImages, publishDirectory, port }
} catch ({ status, message }) {
return errorHandler({ status, message })
}
}
export async function getApplication(request: FastifyRequest<GetApplication>) {
try {
const { id } = request.params
@@ -184,7 +209,8 @@ export async function saveApplication(request: FastifyRequest<SaveApplication>,
denoMainFile,
denoOptions,
baseImage,
baseBuildImage
baseBuildImage,
deploymentType
} = request.body
if (port) port = Number(port);
@@ -215,6 +241,7 @@ export async function saveApplication(request: FastifyRequest<SaveApplication>,
denoOptions,
baseImage,
baseBuildImage,
deploymentType,
...defaultConfiguration
}
});

View File

@@ -1,5 +1,5 @@
import { FastifyPluginAsync } from 'fastify';
import { cancelDeployment, checkDNS, checkRepository, deleteApplication, deleteSecret, deleteStorage, deployApplication, getApplication, getApplicationLogs, getBuildIdLogs, getBuildLogs, getBuildPack, getGitHubToken, getGitLabSSHKey, getPreviews, getSecrets, getStorages, getUsage, listApplications, newApplication, saveApplication, saveApplicationSettings, saveApplicationSource, saveBuildPack, saveDeployKey, saveDestination, saveGitLabSSHKey, saveRepository, saveSecret, saveStorage, stopApplication } from './handlers';
import { cancelDeployment, checkDNS, checkRepository, deleteApplication, deleteSecret, deleteStorage, deployApplication, getApplication, getApplicationLogs, getBuildIdLogs, getBuildLogs, getBuildPack, getGitHubToken, getGitLabSSHKey, getImages, getPreviews, getSecrets, getStorages, getUsage, listApplications, newApplication, saveApplication, saveApplicationSettings, saveApplicationSource, saveBuildPack, saveDeployKey, saveDestination, saveGitLabSSHKey, saveRepository, saveSecret, saveStorage, stopApplication } from './handlers';
export interface GetApplication {
Params: { id: string; }
@@ -37,6 +37,7 @@ const root: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
return await request.jwtVerify()
})
fastify.get('/', async (request) => await listApplications(request));
fastify.post('/images', async (request) => await getImages(request));
fastify.post('/new', async (request, reply) => await newApplication(request, reply));
@@ -67,7 +68,7 @@ const root: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
fastify.post<DeployApplication>('/:id/deploy', async (request) => await deployApplication(request))
fastify.post('/:id/cancel', async (request, reply) => await cancelDeployment(request, reply));
fastify.post('/:id/configuration/source', async (request, reply) => await saveApplicationSource(request, reply));
fastify.get('/:id/configuration/repository', async (request) => await checkRepository(request));