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

Commit

Permalink
feat: cicd (#27)
Browse files Browse the repository at this point in the history
* feat: vpc on base stack

* feat: api execute task working

* docs: cicd ecr docs

* feat: deploy cicd lambdas

* fix: lambda name

* feat: improve tests and documentation

* chore: update faker number

* test: add more tests

* feat: pipelines first implementation

* feat: lambdas implementation

* fix: improve pr pipeline

* fix: update cicd commands

* feat: add close pr commands
  • Loading branch information
arantespp authored May 11, 2021
1 parent 961b0f7 commit acc4039
Show file tree
Hide file tree
Showing 65 changed files with 5,741 additions and 3,859 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,6 @@ node_modules/
.cache/
tmp_*
storybook-static/
package-lock.json
package-lock.json

github-app/
1 change: 1 addition & 0 deletions cicd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-key*
5 changes: 5 additions & 0 deletions cicd/carlin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pipelines:
- pr
# - main
sshKey: './ssh-key'
sshUrl: [email protected]:ttoss/carlin.git
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"publish": "lerna publish && lerna run deploy --scope carlin-website",
"version": "lerna version",
"test": "lerna run test",
"build": "lerna run build",
"deploy": "lerna run deploy"
},
"repository": {
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ module.exports = {
collectCoverage: true,
coverageThreshold: {
global: {
branches: 28,
functions: 37,
lines: 50,
statements: 53,
branches: 31,
functions: 51,
lines: 57,
statements: 60,
},
},
preset: 'ts-jest',
Expand Down
45 changes: 24 additions & 21 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,45 @@
},
"homepage": "https://github.com/ttoss/carlin#readme",
"dependencies": {
"adm-zip": "^0.5.1",
"@octokit/webhooks": "^9.4.0",
"adm-zip": "^0.5.5",
"app-root-path": "^3.0.0",
"aws-sdk": "^2.805.0",
"builtin-modules": "^3.1.0",
"aws-sdk": "^2.897.0",
"builtin-modules": "^3.2.0",
"change-case": "^4.1.2",
"deepmerge": "^4.2.2",
"find-up": "^5.0.0",
"glob": "^7.1.6",
"js-yaml": "^3.14.1",
"mime-types": "^2.1.29",
"js-yaml": "^4.1.0",
"mime-types": "^2.1.30",
"npmlog": "^4.1.2",
"prettier": "^2.2.1",
"semver": "^7.3.4",
"simple-git": "^2.25.0",
"ts-loader": "^8.0.14",
"semver": "^7.3.5",
"simple-git": "^2.38.0",
"ts-loader": "^9.1.1",
"ts-node": "^9.1.1",
"typescript": "^4.1.2",
"uglify-js": "^3.13.0",
"webpack": "^5.15.0",
"typescript": "^4.2.4",
"uglify-js": "^3.13.5",
"webpack": "^5.36.2",
"yargs": "^16.2.0"
},
"devDependencies": {
"@types/adm-zip": "^0.4.33",
"@types/adm-zip": "^0.4.34",
"@types/app-root-path": "^1.2.4",
"@types/eslint": "^7.2.6",
"@types/faker": "^5.1.5",
"@types/jest": "^26.0.17",
"@types/js-yaml": "^3.12.5",
"@types/aws-lambda": "^8.10.76",
"@types/eslint": "^7.2.10",
"@types/faker": "^5.5.3",
"@types/jest": "^26.0.23",
"@types/js-yaml": "^4.0.1",
"@types/mime-types": "^2.1.0",
"@types/node": "^14.14.10",
"@types/node": "^15.0.1",
"@types/npmlog": "^4.1.2",
"@types/semver": "^7.3.4",
"@types/yargs": "^16.0.0",
"faker": "^5.1.0",
"@types/semver": "^7.3.5",
"@types/uglify-js": "^3.13.0",
"@types/yargs": "^16.0.1",
"faker": "^5.5.3",
"jest": "^26.6.3",
"ts-jest": "^26.4.4"
"ts-jest": "^26.5.5"
},
"bin": {
"carlin": "./bin/carlin"
Expand Down
43 changes: 36 additions & 7 deletions packages/cli/src/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,50 @@ jest.mock('find-up', () => ({
.mockReturnValueOnce(undefined),
}));

jest.mock('./deploy/staticApp/staticApp', () => ({
...(jest.requireActual('./deploy/staticApp/staticApp') as any),
deployStaticApp: jest.fn(),
jest.mock('./deploy/baseStack/deployBaseStack', () => ({
...(jest.requireActual('./deploy/baseStack/deployBaseStack') as any),
deployBaseStack: jest.fn(),
}));

import { getCurrentBranch, getEnvironment, getProjectName } from './utils';

import cli from './cli';

import { deployStaticApp } from './deploy/staticApp/staticApp';
import { deployBaseStack } from './deploy/baseStack/deployBaseStack';

const parse = async (arg: any, context: any) => {
return cli().strict(false).parse(arg, context);
};

describe('environment type', () => {
test('throw error if it is an object', () => {
expect(() =>
parse(`print-args`, {
environment: { obj: faker.random.word() },
}),
).rejects.toThrow();
});

test('throw error if it is an array', () => {
expect(() =>
parse(`print-args`, {
environment: [faker.random.word()],
}),
).rejects.toThrow();
});

test("don't throw error if it is a string", async () => {
const environment = faker.random.word();
const argv = await parse(`print-args`, { environment });
expect(argv.environment).toEqual(environment);
});

test("don't throw error if it is a string", async () => {
const argv = await parse(`print-args`, {});
expect(argv.environment).toBeUndefined();
});
});

describe('validating environment variables', () => {
afterEach(() => {
delete process.env.CARLIN_BRANCH;
Expand Down Expand Up @@ -96,11 +125,11 @@ describe('validating environment variables', () => {

describe('handle merge config correctly', () => {
describe('Config merging errors when default values is present #16 https://github.com/ttoss/carlin/issues/16', () => {
test('deploy static-app --region should not be the default', async () => {
await parse('deploy static-app', {
test('deploy base-stack --region should not be the default', async () => {
await parse('deploy base-stack', {
environment: 'Production',
});
expect(deployStaticApp).toHaveBeenCalledWith(
expect(deployBaseStack).toHaveBeenCalledWith(
expect.objectContaining({ region }),
);
});
Expand Down
12 changes: 12 additions & 0 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,18 @@ const cli = () => {
});
}
}) as any)
/**
* Sometimes "environments" can be written as "environment" on config file.
*/
.middleware(({ environment }) => {
if (!['string', 'undefined'].includes(typeof environment)) {
throw new Error(
`environment type is invalid. The value: ${JSON.stringify(
environment,
)}`,
);
}
})
.pkgConf(getPkgConfig())
.config(getConfig())
.config('config', (configPath: string) =>
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export const NAME = 'carlin';

export const AWS_DEFAULT_REGION = 'us-east-1';

/**
* CloudFront triggers can be only in US East (N. Virginia) Region.
* https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-requirements-limits.html#lambda-requirements-cloudfront-triggers
*/
export const CLOUDFRONT_REGION = 'us-east-1';
35 changes: 21 additions & 14 deletions packages/cli/src/deploy/baseStack/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,39 @@ import { pascalCase } from 'change-case';

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

export const BASE_STACK_NAME = `${pascalCase(NAME)}BaseStack`;
const pascalCaseName = pascalCase(NAME);

export const BASE_STACK_NAME = `${pascalCaseName}BaseStack`;
export const BASE_STACK_BUCKET_TEMPLATES_FOLDER = 'cloudformation-templates';

/**
* S3 Bucket.
*/
export const BASE_STACK_BUCKET_LOGICAL_NAME = `${pascalCase(NAME)}Bucket`;
export const BASE_STACK_BUCKET_LOGICAL_NAME = `${pascalCaseName}Bucket`;

export const BASE_STACK_BUCKET_NAME_EXPORTED_NAME = `${pascalCase(
NAME,
)}BucketNameExportedName`;
export const BASE_STACK_BUCKET_NAME_EXPORTED_NAME = `${pascalCaseName}BucketNameExportedName`;

/**
* Lambda image builder.
*/
export const BASE_STACK_LAMBDA_IMAGE_BUILDER_LOGICAL_NAME = `${pascalCase(
NAME,
)}LambdaImageBuilder`;
export const BASE_STACK_LAMBDA_IMAGE_BUILDER_LOGICAL_NAME = `${pascalCaseName}LambdaImageBuilder`;

export const BASE_STACK_LAMBDA_IMAGE_BUILDER_EXPORTED_NAME = `${pascalCase(
NAME,
)}LambdaImageBuilderExportedName`;
export const BASE_STACK_LAMBDA_IMAGE_BUILDER_EXPORTED_NAME = `${pascalCaseName}LambdaImageBuilderExportedName`;

/**
* Lambda layer builder.
*/
export const BASE_STACK_LAMBDA_LAYER_BUILDER_LOGICAL_NAME = `${pascalCase(
NAME,
)}LambdaLayerBuilder`;
export const BASE_STACK_LAMBDA_LAYER_BUILDER_LOGICAL_NAME = `${pascalCaseName}LambdaLayerBuilder`;

/**
* VPC
*/
export const BASE_STACK_VPC_ID_EXPORTED_NAME = `${pascalCaseName}VPCIDExportedName`;

export const BASE_STACK_VPC_DEFAULT_SECURITY_GROUP_EXPORTED_NAME = `${pascalCaseName}DefaultSecurityGroupExportedName`;

export const BASE_STACK_VPC_PUBLIC_SUBNET_0_EXPORTED_NAME = `${pascalCaseName}VPCPublicSubnet0ExportedName`;

export const BASE_STACK_VPC_PUBLIC_SUBNET_1_EXPORTED_NAME = `${pascalCaseName}VPCPublicSubnet1ExportedName`;

export const BASE_STACK_VPC_PUBLIC_SUBNET_2_EXPORTED_NAME = `${pascalCaseName}VPCPublicSubnet2ExportedName`;
1 change: 1 addition & 0 deletions packages/cli/src/deploy/baseStack/deployBaseStack.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ test('should create base resources', async () => {

expect(deployMock).toHaveBeenCalledWith({
template: expect.objectContaining({
AWSTemplateFormatVersion: '2010-09-09',
Resources: expect.objectContaining({
[BASE_STACK_BUCKET_LOGICAL_NAME]: expect.anything(),
[BASE_STACK_LAMBDA_IMAGE_BUILDER_LOGICAL_NAME]: expect.anything(),
Expand Down
Loading

0 comments on commit acc4039

Please sign in to comment.