Skip to content
This repository was archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
feat: add tags to ecs containers
Browse files Browse the repository at this point in the history
  • Loading branch information
arantespp committed May 17, 2021
1 parent 7b1eca9 commit 9288397
Show file tree
Hide file tree
Showing 10 changed files with 1,158 additions and 1,705 deletions.
1 change: 0 additions & 1 deletion config/eslintrc.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module.exports = {
'plugin:@typescript-eslint/recommended',
'plugin:import/typescript',
'plugin:prettier/recommended',
'prettier/@typescript-eslint',
],
rules: {
/**
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
"devDependencies": {
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@typescript-eslint/eslint-plugin": "^4.14.1",
"@typescript-eslint/parser": "^4.14.1",
"eslint": "^7.19.0",
"@typescript-eslint/eslint-plugin": "^4.23.0",
"@typescript-eslint/parser": "^4.23.0",
"eslint": "^7.26.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-config-prettier": "^7.2.0",
Expand All @@ -40,15 +40,15 @@
"eslint-plugin-jest": "^24.1.3",
"eslint-plugin-jest-dom": "^3.6.5",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-react": "^7.22.0",
"eslint-plugin-react-hooks": "^4.2.0",
"eslint-plugin-relay": "^1.8.1",
"faker": "^5.2.0",
"husky": "^4.3.8",
"imagemin-lint-staged": "^0.4.0",
"jest": "^26.6.3",
"lerna": "^3.22.1",
"lerna": "^4.0.0",
"lint-staged": "^10.5.3",
"npm-check-updates": "^11.1.1",
"prettier": "^2.2.1",
Expand Down
38 changes: 38 additions & 0 deletions packages/cli/src/deploy/addDefaults.cloudFormation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { CloudFormationTemplate } from '../utils/cloudFormationTemplate';

import { addDefaults } from './addDefaults.cloudFormation';

describe('testing template update', () => {
test.each<[string, CloudFormationTemplate, Partial<CloudFormationTemplate>]>([
[
'add AppSync outputs',
{
AWSTemplateFormatVersion: '2010-09-09',
Resources: {
AppSyncGraphQLApi: {
Type: 'AWS::AppSync::GraphQLApi',
Properties: {},
},
},
},
{
Outputs: {
AppSyncGraphQLApi: {
Description: expect.any(String),
Value: { 'Fn::GetAtt': ['AppSyncGraphQLApi', 'GraphQLUrl'] },
Export: {
Name: {
'Fn::Join': [':', [{ Ref: 'AWS::StackName' }, 'GraphQLApiUrl']],
},
},
},
},
},
],
])('%s', async (_, template, newTemplate) => {
const params = { StackName: 'stackName' };
expect((await addDefaults({ params, template })).template).toEqual(
expect.objectContaining(newTemplate),
);
});
});
63 changes: 39 additions & 24 deletions packages/cli/src/deploy/addDefaults.cloudFormation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CloudFormation } from 'aws-sdk';
import log from 'npmlog';

import { NAME } from '../config';

import {
CloudFormationTemplate,
Expand All @@ -10,33 +11,29 @@ import {
getProjectName,
} from '../utils';

const logPrefix = 'addDefaultsCloudFormation';
// const logPrefix = 'addDefaultsCloudFormation';

type CloudFormationParams =
| CloudFormation.CreateStackInput
| CloudFormation.UpdateStackInput;

type Args = {
export type Args = {
params: CloudFormationParams;
template: CloudFormationTemplate;
};

type TemplateModifier = (
template: CloudFormationTemplate,
) => Promise<CloudFormationTemplate>;

const addDefaultsParametersAndTagsToParams = async (
params: CloudFormationParams,
): Promise<CloudFormationParams> => {
const [
branchName,
environment,
packageName,
packageVersion,
projectName,
] = await Promise.all([
getCurrentBranch(),
getEnvironment(),
getPackageName(),
getPackageVersion(),
getProjectName(),
]);
const branchName = await getCurrentBranch();
const environment = await getEnvironment();
const packageName = await getPackageName();
const packageVersion = await getPackageVersion();
const projectName = await getProjectName();

return {
...params,
Expand All @@ -58,9 +55,7 @@ const addDefaultsParametersAndTagsToParams = async (
};
};

const addDefaultParametersToTemplate = async (
template: CloudFormationTemplate,
): Promise<CloudFormationTemplate> => {
const addDefaultParametersToTemplate: TemplateModifier = async (template) => {
const [environment, projectName] = await Promise.all([
getEnvironment(),
getProjectName(),
Expand Down Expand Up @@ -107,8 +102,6 @@ const addLogGroupToResources = (
});

if (!logGroup) {
log.info(logPrefix, `Adding log group to Lambda resource: ${key}.`);

Resources[`${key}LogsLogGroup`] = {
Type: 'AWS::Logs::LogGroup',
DeletionPolicy: 'Delete',
Expand All @@ -123,9 +116,7 @@ const addLogGroupToResources = (
return template;
};

const addEnvironmentsToLambdaResources = (
template: CloudFormationTemplate,
): CloudFormationTemplate => {
const addEnvironmentsToLambdaResources: TemplateModifier = async (template) => {
const environment = getEnvironment();

const { Resources } = template;
Expand Down Expand Up @@ -165,6 +156,29 @@ const addEnvironmentsToLambdaResources = (
return template;
};

const addAppSyncApiOutputs: TemplateModifier = async (template) => {
const newTemplate = { ...template };

Object.entries(template.Resources).forEach(([key, resource]) => {
if (resource.Type === 'AWS::AppSync::GraphQLApi') {
newTemplate.Outputs = {
[key]: {
Description: `Automatically added by ${NAME}`,
Value: { 'Fn::GetAtt': [key, 'GraphQLUrl'] },
Export: {
Name: {
'Fn::Join': [':', [{ Ref: 'AWS::StackName' }, 'GraphQLApiUrl']],
},
},
},
...newTemplate.Outputs,
};
}
});

return newTemplate;
};

export const addDefaults = async ({
params,
template,
Expand All @@ -173,6 +187,7 @@ export const addDefaults = async ({
addDefaultParametersToTemplate,
addLogGroupToResources,
addEnvironmentsToLambdaResources,
addAppSyncApiOutputs,
].reduce(async (acc, addFn) => addFn(await acc), Promise.resolve(template));

const response = {
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/deploy/cicd/lambdas/executeTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ export const executeTasks = async ({
cpu,
memory,
taskEnvironment = [],
tags = [],
}: {
commands: string[];
cpu?: string;
memory?: string;
taskEnvironment?: Array<{ name: string; value: string }>;
tags?: Array<{ key: string; value?: string }>;
}) => {
const command = compileCommands([
/**
Expand Down Expand Up @@ -96,6 +98,7 @@ export const executeTasks = async ({
cpu,
memory,
},
tags,
})
.promise();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ export const githubWebhooksApiV1Handler: ProxyHandler = async (
}),
}),
],
tags: [
{ key: 'Pipeline', value: 'pr' },
{ key: 'PullRequest', value: payload.number.toString() },
{ key: 'PullRequestTitle', value: payload.pull_request.title },
{ key: 'PullRequestUrl', value: payload.pull_request.url },
{ key: 'Action', value: payload.action },
{ key: 'Branch', value: payload.pull_request.head.ref },
],
});
},
);
Expand All @@ -114,6 +122,14 @@ export const githubWebhooksApiV1Handler: ProxyHandler = async (
}),
}),
],
tags: [
{ key: 'Pipeline', value: 'pr' },
{ key: 'PullRequest', value: payload.number.toString() },
{ key: 'PullRequestTitle', value: payload.pull_request.title },
{ key: 'PullRequestUrl', value: payload.pull_request.url },
{ key: 'Action', value: payload.action },
{ key: 'Branch', value: payload.pull_request.head.ref },
],
});
});
}
Expand Down
43 changes: 39 additions & 4 deletions packages/cli/src/deploy/cicd/lambdas/pipelines.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,28 @@ const putJobFailureResult = (event: CodePipelineEvent, message: string) =>
})
.promise();

const executeMainPipeline = async () => {
const executeMainPipeline = async ({
payload,
}: {
payload: EventPayloadMap['push'];
}) => {
const command = shConditionalCommands({
conditionalCommands: getMainCommands(),
});
await executeTasks({ commands: [command] });

await executeTasks({
commands: [command],
tags: [
{
key: 'Pipeline',
value: 'main',
},
{
key: 'AfterCommit',
value: payload.after,
},
],
});
};

const executeTagPipeline = async ({
Expand All @@ -80,10 +97,28 @@ const executeTagPipeline = async ({
payload: EventPayloadMap['push'];
}) => {
const tag = payload.ref.split('/')[2];

const command = shConditionalCommands({
conditionalCommands: getTagCommands({ tag }),
});
await executeTasks({ commands: [command] });

await executeTasks({
commands: [command],
tags: [
{
key: 'Pipeline',
value: 'tag',
},
{
key: 'Tag',
value: tag,
},
{
key: 'AfterCommit',
value: payload.after,
},
],
});
};

export const pipelinesHandler: CodePipelineHandler = async (event) => {
Expand All @@ -93,7 +128,7 @@ export const pipelinesHandler: CodePipelineHandler = async (event) => {
const jobDetails = await getJobDetails(event);

if (pipeline === 'main') {
await executeMainPipeline();
await executeMainPipeline(jobDetails);
await putJobSuccessResult(event);
return;
}
Expand Down
14 changes: 7 additions & 7 deletions packages/cli/src/deploy/cicd/pipelines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ export const getPrCommands = ({ branch }: { branch: string }) => [
/**
* Apply lint only on the modified files.
*/
`git diff --name-only HEAD..main | grep -E '\\.(j|t)sx?$' | xargs npx eslint --no-error-on-unmatched-pattern`,
/**
* Execute tests only on the modified packages.
*/
`npx lerna run "test" --since=main --stream --parallel`,
`git diff --name-only HEAD..main | grep -E "\\.(j|t)sx?$" | xargs npx eslint --no-error-on-unmatched-pattern`,
/**
* Build only modified packages.
*/
`npx lerna run "build" --since=main --stream --parallel`,
/**
* Execute tests only on the modified packages.
*/
`npx lerna run "test" --since=main --stream --parallel`,
/**
* Deploy only the modified packages.
*/
Expand All @@ -54,8 +54,8 @@ export const getMainCommands = () => [
'git rev-parse HEAD',
'yarn',
`export CARLIN_ENVIRONMENT=Staging`,
`npx lerna run "test" --stream --parallel`,
`npx lerna run "build" --stream --parallel`,
`npx lerna run "test" --stream --parallel`,
`npx lerna run "deploy" --stream --parallel`,
];

Expand All @@ -66,7 +66,7 @@ export const getTagCommands = ({ tag }: { tag: string }) => [
'git rev-parse HEAD',
'yarn',
`export CARLIN_ENVIRONMENT=Production`,
`npx lerna run "test" --stream --parallel`,
`npx lerna run "build" --stream --parallel`,
`npx lerna run "test" --stream --parallel`,
`npx lerna run "deploy" --stream --parallel`,
];
6 changes: 5 additions & 1 deletion packages/cli/src/deploy/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,15 @@ export const buildLambdaSingleFile = async ({
const compiler = webpack(webpackConfig);

return new Promise<void>((resolve, reject) => {
compiler.run((err) => {
compiler.run((err, stats) => {
if (err) {
return reject(err);
}

if (stats?.hasErrors()) {
return reject(stats.toJson().errors);
}

return resolve();
});
});
Expand Down
Loading

0 comments on commit 9288397

Please sign in to comment.