Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scheduled deploys for docs-landing #689

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v14.17.6
16 changes: 16 additions & 0 deletions api/controllers/v1/scheduled/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { DeployRepo } from './utils';
import { options as docsLandingOptions } from './repos/docs-landing';

export const DeployDocsLanding = async () => {
const { branches, repoName, repoOwner } = docsLandingOptions;
Promise.all(
branches.map((branch) => {
return DeployRepo(repoOwner, repoName, branch);
})
);

return {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
};
};
5 changes: 5 additions & 0 deletions api/controllers/v1/scheduled/repos/docs-landing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const options = {
repoOwner: 'mongodb',
repoName: 'docs-landing',
branches: ['master'],
};
165 changes: 165 additions & 0 deletions api/controllers/v1/scheduled/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import * as c from 'config';
import * as mongodb from 'mongodb';
import { BranchRepository } from '../../../../../src/repositories/branchRepository';
import { ConsoleLogger, ILogger } from '../../../../../src/services/logger';
import { JobRepository } from '../../../../../src/repositories/jobRepository';
// Used solely for adding parallel deploy jobs to another array
const deployHelper = (deployable, payload, jobTitle, jobUserName) => {
deployable.push(createJob({ ...payload }, jobTitle, jobUserName));
};

function createJob(payload: any, jobTitle: string, jobUserName: string) {
return {
title: jobTitle,
user: jobUserName,
email: '',
status: 'inQueue',
createdTime: new Date(),
startTime: null,
endTime: null,
priority: 1,
error: {},
result: null,
payload: payload,
logs: [],
};
}

function createPayload(
jobType: string,
repoOwner: string,
repoName: string,
branchName: string,
newHead: string,
project: string,
prefix: string,
urlSlug,
aliased = false,
primaryAlias = false,
stable = ''
) {
return {
jobType,
source: 'github',
action: 'push',
repoName,
branchName,
project,
prefix,
aliased,
urlSlug,
isFork: true,
private: repoOwner === '10gen',
isXlarge: true,
repoOwner,
url: 'https://github.com/' + repoOwner + '/' + repoName,
newHead,
primaryAlias,
stable,
};
}
// For every repo/branch selected to be deployed, return an array of jobs with the payload data
// needed for a successful build.
const getDeployableJobs = async (
repoOwner: string,
repoName: string,
branchName: string,
branchRepository: BranchRepository
) => {
const deployable = [];
const hashOption = null;
const jobUserName = 'scheduled_deploy';
const jobTitle = `Scheduled Deploy Action`;

const repoInfo = await branchRepository.getRepo(repoName);
const non_versioned = repoInfo.branches.length === 1;

const branchObject = await branchRepository.getRepoBranchAliases(repoName, branchName);
if (!branchObject?.aliasObject) return;

const publishOriginalBranchName = branchObject.aliasObject.publishOriginalBranchName; //bool
let aliases = branchObject.aliasObject.urlAliases; // array or null
let urlSlug = branchObject.aliasObject.urlSlug; // string or null, string must match value in urlAliases or gitBranchName
const isStableBranch = branchObject.aliasObject.isStableBranch; // bool or Falsey
aliases = aliases?.filter((a) => a);
if (!urlSlug || !urlSlug.trim()) {
urlSlug = branchName;
}

// Generic payload, will be conditionally modified appropriately
const newPayload = createPayload(
'productionDeploy',
repoOwner,
repoName,
branchName,
hashOption,
repoInfo.project,
repoInfo.prefix[c.get<string>('env')],
urlSlug,
false,
false,
'-g'
);

if (!aliases || aliases.length === 0) {
if (non_versioned) {
newPayload.urlSlug = '';
}
deployHelper(deployable, newPayload, jobTitle, jobUserName);
return;
}

// if this is stable branch, we want autobuilder to know this is unaliased branch and therefore can reindex for search
newPayload.stable = isStableBranch ? '-g' : '';
newPayload.aliased = true;

// we use the primary alias for indexing search, not the original branch name (ie 'master'), for aliased repos
if (urlSlug) {
newPayload.urlSlug = urlSlug;
newPayload.primaryAlias = true;
deployHelper(deployable, newPayload, jobTitle, jobUserName);
}

// handle non-versioned repos AND repos where only 1 version is active
if (non_versioned || (!publishOriginalBranchName && urlSlug === null)) {
newPayload.urlSlug = '';
deployHelper(deployable, newPayload, jobTitle, jobUserName);
} else if (publishOriginalBranchName && urlSlug !== branchName) {
newPayload.urlSlug = branchName;
newPayload.primaryAlias = false;
deployHelper(deployable, newPayload, jobTitle, jobUserName);
}

aliases.forEach(async (alias: string) => {
if (alias !== urlSlug) {
newPayload.stable = '';
newPayload.urlSlug = alias;
newPayload.primaryAlias = false;
deployHelper(deployable, newPayload, jobTitle, jobUserName);
}
});

return deployable;
};

async function deployRepo(deployable: Array<any>, logger: ILogger, jobRepository: JobRepository, jobQueueUrl) {
try {
return jobRepository.insertBulkJobs(deployable, jobQueueUrl);
} catch (err) {
logger.error('deployRepo', err);
}
}

export const DeployRepo = async (repoOwner, repoName, branchName): Promise<any> => {
const consoleLogger = new ConsoleLogger();
const client = new mongodb.MongoClient(c.get('dbUrl'));
await client.connect();
const db = client.db(c.get('dbName'));
const branchRepository = new BranchRepository(db, c, consoleLogger);
const jobRepository = new JobRepository(db, c, consoleLogger);

const deployable = await getDeployableJobs(repoOwner, repoName, branchName, branchRepository);
if (deployable.length > 0) {
return deployRepo(deployable, consoleLogger, jobRepository, c.get('jobsQueueUrl'));
}
};
18 changes: 18 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,24 @@ functions:
environment:
<<: *webhook-env-core

# SCHEDULED DEPLOYS START

v1ScheduledDeployDocsLanding:
handler: api/controllers/v1/scheduled/index.DeployDocsLanding
events:
- schedule: cron(0/2 * ? * MON-FRI *)
iamRoleStatementsName: 'dwpapi-v1ScheduledDeployDocsLanding-lr-${self:provider.region}-${self:provider.stage}'
iamRoleStatements:
- Effect: Allow
Action:
- "sqs:SendMessage"
Resource:
- "*"
environment:
<<: *webhook-env-core

# SCHEDULED DEPLOYS END

v1TriggerLocalBuild:
handler: api/controllers/v1/jobs.TriggerLocalBuild
events:
Expand Down
Loading