Skip to content

Commit

Permalink
[DOP-4033]: Add stage job
Browse files Browse the repository at this point in the history
  • Loading branch information
branberry committed Oct 4, 2023
1 parent c5d6680 commit 17830fb
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 57 deletions.
13 changes: 6 additions & 7 deletions Dockerfile.local
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ RUN cd ./modules/oas-page-builder \
# Root project build
COPY package*.json ./
RUN npm ci --legacy-peer-deps
COPY tsconfig*.json ./
COPY config config/
COPY api api/
COPY src src/

# Build persistence module

COPY --chown=docsworker modules/persistence/tsconfig*.json ./modules/persistence
Expand All @@ -85,18 +80,22 @@ RUN cd ./modules/persistence \

# Build modules
# OAS Page Builder

COPY --chown=docsworker modules/oas-page-builder/tsconfig*.json ./modules/oas-page-builder
COPY --chown=docsworker modules/oas-page-builder/src ./modules/oas-page-builder/src/
COPY --chown=docsworker modules/oas-page-builder/index.ts ./modules/oas-page-builder

RUN cd ./modules/oas-page-builder \
&& npm run build

COPY tsconfig*.json ./
COPY config config/
COPY api api/
COPY src src/

RUN npm run build:esbuild

RUN mkdir repos && chmod 755 repos

EXPOSE 3000

CMD ["node", "dist/entrypoints/localApp.js"]
CMD ["node", "--enable-source-maps", "dist/entrypoints/localApp.js"]
26 changes: 23 additions & 3 deletions src/commands/src/helpers/execution-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,36 @@ import { checkIfPatched, getCommitBranch, getCommitHash, getPatchId, getRepoDir
import { promisify } from 'util';

const existsAsync = promisify(fs.exists);
const writeFileAsync = promisify(fs.writeFile);

export async function getCliBuildDependencies(repoName: string) {
const repoDir = getRepoDir(repoName);
async function createEnvProdFile(repoDir: string, projectName: string, baseUrl: string, prefix = '') {
const prodFileName = `${process.cwd()}/snooty/.env.production`;

try {
await writeFileAsync(
prodFileName,
`GATSBY_BASE_URL=docs.mongodb.com
GATSBY_SITE=${projectName}
GATSBY_MANIFEST_PATH=${repoDir}/bundle.zip
GATSBY_PARSER_USER=${process.env.USER}
GATSBY_BASE_URL=${baseUrl}
GATSBY_PATH_PREFIX=${prefix}`,
'utf8'
);
} catch (e) {
console.error(`ERROR! Could not write to .env.production`);
throw e;
}
}

export async function getCliBuildDependencies(repoDir: string, projectName: string, baseUrl: string) {
const commandPromises = [
checkIfPatched(repoDir),
getCommitHash(repoDir),
getCommitBranch(repoDir),
getPatchId(repoDir),
existsAsync(path.join(process.cwd(), 'config/redirects')),
createEnvProdFile(repoDir, projectName, baseUrl),
];

const deps = await Promise.all(commandPromises);
Expand All @@ -23,6 +44,5 @@ export async function getCliBuildDependencies(repoName: string) {
patchId: deps[3] as string | undefined,
hasRedirects: deps[4] as boolean,
bundlePath: `${repoDir}/bundle.zip`,
repoDir,
};
}
13 changes: 0 additions & 13 deletions src/commands/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,6 @@ export async function getPatchId(repoDir: string): Promise<string | undefined> {
}
}

export async function addProjectToEnv(project: string) {
return new Promise((resolve, reject) => {
const stream = fs.createWriteStream(path.join(process.cwd(), 'snooty/.env.production'), { flags: 'a+' });

stream.write(project);
stream.close();
stream.on('error', (error) => {
console.log('Error when writing to file!', error);
reject(error);
});
stream.on('close', resolve);
});
}
export async function getCommitBranch(repoDir: string): Promise<string> {
// equivalent to git rev-parse --short HEAD
const response = await executeCliCommand({
Expand Down
23 changes: 1 addition & 22 deletions src/commands/src/shared/next-gen-html.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
import fs from 'fs';
import { promisify } from 'util';
import { executeCliCommand } from '../helpers';

const writeFileAsync = promisify(fs.writeFile);

async function createEnvProdFile(repoDir: string, projectName: string) {
try {
await writeFileAsync(
`${process.cwd()}/snooty/.env.production`,
`GATSBY_BASE_URL=docs.mongodb.com
GATSBY_SITE=${projectName}
GATSBY_MANIFEST_PATH=${repoDir}/bundle.zip`,
'utf8'
);
} catch (e) {
console.error(`ERROR! Could not write to .env.production`);
throw e;
}
}
export async function nextGenHtml(repoDir: string) {
// might move this since technically next-gen-html doesn't create the file
await createEnvProdFile(repoDir, 'java');

export async function nextGenHtml() {
const result = await executeCliCommand({
command: 'npm',
args: ['run', 'build'],
Expand Down
28 changes: 22 additions & 6 deletions src/commands/src/shared/next-gen-stage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { checkIfPatched, executeCliCommand, getCommitBranch, getCommitHash, getPatchId, getRepoDir } from '../helpers';
import {
checkIfPatched,
executeAndWriteToFile,
executeCliCommand,
getCommitBranch,
getCommitHash,
getPatchId,
} from '../helpers';

interface StageParams {
repoDir: string;
Expand All @@ -11,17 +18,26 @@ interface StageParams {
export async function nextGenStage({ repoDir, mutPrefix, projectName, bucketName, url }: StageParams) {
const [hasPatch, commitBranch] = await Promise.all([checkIfPatched(repoDir), getCommitBranch(repoDir)]);

let hostedAtUrl = `${url}/${mutPrefix}/${process.env.USER}/${commitBranch}/`;
let hostedAtUrl = `${url}/${mutPrefix}/docsworker/${commitBranch}/`;
let prefix = mutPrefix;

const commandArgs = ['public', bucketName, '--stage'];

if (hasPatch && projectName !== mutPrefix) {
if (hasPatch && projectName === mutPrefix) {
const [commitHash, patchId] = await Promise.all([getCommitHash(repoDir), getPatchId(repoDir)]);
commandArgs.push(`--prefix="${commitHash}/${patchId}/${mutPrefix}"`);
hostedAtUrl = `${url}/${commitHash}/${patchId}/${mutPrefix}/${process.env.USER}/${commitBranch}/`;
prefix = `${commitHash}/${patchId}/${mutPrefix}`;
hostedAtUrl = `${url}/${commitHash}/${patchId}/${mutPrefix}/docsworker/${commitBranch}/`;
}

const { outputText } = await executeCliCommand({ command: 'mut-publish', args: commandArgs });
commandArgs.push(`--prefix="${prefix}"`);

const { outputText } = await executeCliCommand({
command: 'mut-publish',
args: commandArgs,
options: {
cwd: `${process.cwd()}/snooty`,
},
});
const resultMessage = `${outputText}\n Hosted at ${hostedAtUrl}`;
return resultMessage;
}
32 changes: 26 additions & 6 deletions src/entrypoints/localApp.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import { executeCliCommand } from '../commands/src/helpers';
import { executeCliCommand, getRepoDir } from '../commands/src/helpers';
import { nextGenParse } from '../commands/src/shared/next-gen-parse';
import { nextGenHtml } from '../commands/src/shared/next-gen-html';
import { getCliBuildDependencies } from '../commands/src/helpers/execution-helper';
import { nextGenStage } from '../commands/src/shared/next-gen-stage';
import { oasPageBuild } from '../commands/src/shared/oas-page-build';

async function localApp() {
const repoName = 'docs-landing';
const { repoDir, commitHash, patchId } = await getCliBuildDependencies(repoName);
const repoName = 'docs-java';
const projectName = 'java';
const baseUrl = 'https://www.mongodb.com';
const repoDir = getRepoDir(repoName);

// mocking out the clone aspect of a job
await executeCliCommand({
command: 'git',
args: ['clone', `https://github.com/mongodb/${repoName}`],
options: { cwd: repoDir },
options: { cwd: `${process.cwd()}/repos` },
});

console.log('Hello');
const { commitHash, patchId, bundlePath } = await getCliBuildDependencies(repoDir, projectName, baseUrl);

console.log('Begin snooty build...');
const snootyBuildRes = await nextGenParse({ repoDir, commitHash, patchId });

Expand All @@ -23,13 +29,27 @@ async function localApp() {

console.log('Begin next-gen-html...');

const nextGenHtmlRes = await nextGenHtml(repoName);
const nextGenHtmlRes = await nextGenHtml();

console.log('Begin oas-page-build...');
const mutPrefix = 'docs';
const siteUrl = mutPrefix ? `${baseUrl}/${mutPrefix}` : `${baseUrl}`;
const oasPageBuildRes = await oasPageBuild({ repoDir, bundlePath, siteUrl });
console.log(nextGenHtmlRes.outputText);

console.log('next-gen-html complete');

console.log('Begin next-gen-stage...');

const resultMessage = await nextGenStage({
repoDir,
projectName,
bucketName: 'docs-mongodb-org-stg',
url: baseUrl,
mutPrefix,
});
console.log(resultMessage);
console.log('Begin next-gen-stage complete');
}

localApp();

0 comments on commit 17830fb

Please sign in to comment.