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

Commit

Permalink
feat: update commands pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
arantespp committed Sep 15, 2021
1 parent ca85057 commit 88b0444
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 162 deletions.
2 changes: 1 addition & 1 deletion cicd/carlin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs';

export default {
pipelines: ['tag'],
pipelines: ['main', 'tag'],
sshKey: './ssh-key',
sshUrl: '[email protected]:ttoss/carlin.git',
taskEnvironment: [
Expand Down
6 changes: 6 additions & 0 deletions cicd/commands/main
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

set -e

npx lerna run "build" --stream --parallel
npx lerna run "test" --stream --parallel
7 changes: 7 additions & 0 deletions cicd/commands/tag
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

set -e

npx lerna run "build" --stream --parallel
npx lerna run "test" --stream --parallel
npx lerna run "deploy" --stream --parallel
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"change-case": "^4.1.2",
"deep-equal": "^2.0.5",
"deepmerge": "^4.2.2",
"esbuild": "^0.12.26",
"find-up": "^5.0.0",
"glob": "^7.1.7",
"js-yaml": "^4.1.0",
Expand All @@ -50,7 +51,6 @@
"ts-node": "^9.1.1",
"typescript": "^4.2.4",
"uglify-js": "^3.13.6",
"webpack": "^5.37.0",
"yargs": "^17.0.1"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/deploy/cicd/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export const options = {
description: 'Pipelines that will be implemented with the CICD stack.',
type: 'array',
},
'repository-update': {
alias: ['update-repository', 'ur', 'ru'],
'update-repository': {
alias: ['ur'],
description: 'Determine if the repository image will be updated.',
default: true,
type: 'boolean',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export const githubWebhooksApiV1Handler: ProxyHandler = async (
});

return { statusCode: 200, body: JSON.stringify({ ok: true }) };
} catch (error) {
} catch (error: any) {
console.error(error);
return { statusCode: error.status || 500, body: error.message };
}
Expand Down
67 changes: 3 additions & 64 deletions packages/cli/src/deploy/cicd/pipelines.spec.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,7 @@
import * as faker from 'faker';

import * as pipelinesModule from './pipelines';

const branch = faker.random.word();

const tag = faker.random.word();

test('pipelines', () => {
expect(pipelinesModule.pipelines).toEqual(['pr', 'main', 'tag']);
});

test('getClosedPrCommands', () => {
expect(pipelinesModule.getClosedPrCommands({ branch })).toEqual([
'git status',
'git fetch',
'git pull origin main',
'git rev-parse HEAD',
`export CARLIN_BRANCH=${branch}`,
`npx lerna run "deploy" --stream --parallel -- --destroy`,
]);
});

test('getMainCommands', () => {
expect(pipelinesModule.getMainCommands()).toEqual([
'git status',
'git fetch',
'git pull origin main',
'git rev-parse HEAD',
'yarn',
`export CARLIN_ENVIRONMENT=Staging`,
`npx lerna run "build" --stream --parallel`,
`npx lerna run "test" --stream --parallel`,
`npx lerna run "deploy" --stream --parallel`,
]);
});

test('getTagCommands', () => {
expect(pipelinesModule.getTagCommands({ tag })).toEqual([
'git status',
'git fetch --tags',
`git checkout tags/${tag} -b ${tag}-branch`,
'git rev-parse HEAD',
'yarn',
`export CARLIN_ENVIRONMENT=Production`,
`npx lerna run "build" --stream --parallel`,
`npx lerna run "test" --stream --parallel`,
`npx lerna run "deploy" --stream --parallel`,
]);
});

test('getPrCommands', () => {
expect(pipelinesModule.getPrCommands({ branch })).toEqual([
'git status',
'git fetch',
'git pull origin main',
`git checkout ${branch}`,
`git pull origin ${branch}`,
'git rev-parse HEAD',
'git status',
'yarn',
'npx lerna ls --since=main',
`git diff --name-only HEAD..main | grep -E "\\.(j|t)sx?$" | xargs npx eslint --no-error-on-unmatched-pattern`,
`npx lerna run "build" --since=main --stream --parallel`,
`npx lerna run "test" --since=main --stream --parallel`,
`npx lerna run "deploy" --since=main --stream --parallel`,
]);
expect(pipelinesModule.pipelines).toMatchObject(
expect.arrayContaining(['pr', 'main', 'tag', 'closed-pr', 'ss']),
);
});
37 changes: 10 additions & 27 deletions packages/cli/src/deploy/cicd/pipelines.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
export const pipelines = ['pr', 'main', 'tag'] as const;
export const pipelines = ['pr', 'closed-pr', 'main', 'tag'] as const;

export type Pipelines = typeof pipelines;

export type Pipeline = Pipelines[number];

const executeCommandFile = (pipeline: Pipeline) =>
`chmod +x ./cicd/commands/${pipeline} && ./cicd/commands/${pipeline}`;

export const getPrCommands = ({ branch }: { branch: string }) => [
'git status',
'git fetch',
Expand All @@ -16,23 +19,7 @@ export const getPrCommands = ({ branch }: { branch: string }) => [
'git rev-parse HEAD',
'git status',
'yarn',
'npx lerna ls --since=main',
/**
* 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`,
/**
* 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.
*/
`npx lerna run "deploy" --since=main --stream --parallel`,
executeCommandFile('pr'),
];

export const getClosedPrCommands = ({ branch }: { branch: string }) => [
Expand All @@ -44,29 +31,25 @@ export const getClosedPrCommands = ({ branch }: { branch: string }) => [
'git pull origin main',
'git rev-parse HEAD',
`export CARLIN_BRANCH=${branch}`,
`npx lerna run "deploy" --stream --parallel -- --destroy`,
executeCommandFile('closed-pr'),
];

export const getMainCommands = () => [
`export CARLIN_ENVIRONMENT=Staging`,
'git status',
'git fetch',
'git pull origin main',
'git rev-parse HEAD',
'yarn',
`export CARLIN_ENVIRONMENT=Staging`,
`npx lerna run "build" --stream --parallel`,
`npx lerna run "test" --stream --parallel`,
`npx lerna run "deploy" --stream --parallel`,
executeCommandFile('main'),
];

export const getTagCommands = ({ tag }: { tag: string }) => [
`export CARLIN_ENVIRONMENT=Production`,
'git status',
'git fetch --tags',
`git checkout tags/${tag} -b ${tag}-branch`,
'git rev-parse HEAD',
'yarn',
`export CARLIN_ENVIRONMENT=Production`,
`npx lerna run "build" --stream --parallel`,
`npx lerna run "test" --stream --parallel`,
`npx lerna run "deploy" --stream --parallel`,
executeCommandFile('tag'),
];
79 changes: 18 additions & 61 deletions packages/cli/src/deploy/lambda.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import AdmZip from 'adm-zip';
import { CodeBuild } from 'aws-sdk';
import builtins from 'builtin-modules';
import * as esbuild from 'esbuild';
import fs from 'fs';
import log from 'npmlog';
import path from 'path';
import webpack from 'webpack';

import { AWS_DEFAULT_REGION } from '../config';
import { waitCodeBuildFinish, getPackageVersion } from '../utils';
Expand All @@ -19,7 +19,7 @@ const logPrefix = 'lambda';

const outFolder = 'dist';

const webpackOutputFilename = 'index.js';
const outFile = 'index.js';

/**
* Using Webpack because of issue #8.
Expand All @@ -34,73 +34,30 @@ export const buildLambdaSingleFile = async ({
}) => {
log.info(logPrefix, 'Building Lambda single file...');

const webpackConfig: webpack.Configuration = {
entry: path.resolve(process.cwd(), lambdaInput),
mode: 'production',
externals: ['aws-sdk', ...builtins, ...lambdaExternals],
module: {
rules: [
{
exclude: /node_modules/,
test: /\.tsx?$/,
loader: require.resolve('ts-loader'),
options: {
compilerOptions: {
/**
* Packages like 'serverless-http' cannot be used without this
* property.
*/
allowSyntheticDefaultImports: true,
esModuleInterop: true,
declaration: false,
target: 'es2017',
module: 'esnext',
noEmit: false,
},
},
},
],
},
optimization: {
// usedExports: true,
minimize: false,
const { errors } = esbuild.buildSync({
banner: {
js: '// Powered by carlin (https://carlin.ttoss.dev)',
},
resolve: {
extensions: ['.tsx', '.ts', '.js', '.json'],
},
target: 'node',
output: {
filename: webpackOutputFilename,
libraryTarget: 'commonjs',
path: path.join(process.cwd(), outFolder),
},
};

const compiler = webpack(webpackConfig);

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

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

return resolve();
});
bundle: true,
entryPoints: [path.resolve(process.cwd(), lambdaInput)],
external: ['aws-sdk', ...builtins, ...lambdaExternals],
platform: 'node',
outfile: path.join(process.cwd(), outFolder, outFile),
target: 'node12',
treeShaking: true,
});

if (errors.length > 0) {
throw errors;
}
};

export const uploadCodeToS3 = async ({ stackName }: { stackName: string }) => {
log.info(logPrefix, `Uploading code to S3...`);

const zip = new AdmZip();

const code = fs.readFileSync(
path.join(process.cwd(), outFolder, webpackOutputFilename),
);
const code = fs.readFileSync(path.join(process.cwd(), outFolder, outFile));

zip.addFile('index.js', code);

Expand Down Expand Up @@ -142,7 +99,7 @@ export const deployLambdaLayers = async ({
try {
// eslint-disable-next-line global-require, import/no-dynamic-require
return require(path.resolve(process.cwd(), 'package.json')) || {};
} catch (err) {
} catch (err: any) {
log.error(
logPrefix,
'Cannot read package.json. Error message: %j',
Expand Down
20 changes: 17 additions & 3 deletions packages/website/docs/CLI.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Carlin from '../src/components/Carlin';
import InnerHTML from '../src/components/InnerHTML';
import OptionAliasesInline from '../src/components/OptionAliasesInline';
import OptionHeader from '../src/components/OptionHeader';
Expand Down Expand Up @@ -33,12 +34,25 @@ The options below, if defined, is passed to all commands, but a command might no

<OptionHeader option="environment" options={cliOptions} />

We can set the environment of your deployment. Every deploy operation allow we set the <OptionAliasesInline option="environment" options={cliOptions} /> option that defines our environment.
Deployments with an environment commonly tends to be a deploy with permanent resources, like staging and production. Due to this, when environment is defined, some properties changes. For instance:
We can set the environment of your deployment. Every deploy operation allows us to set the <OptionAliasesInline option="environment" options={cliOptions} /> option that defines our environment.
Deployments with an environment commonly tend to be deployment with permanent resources, like `Staging` and `Production`. Due to this, when the environment is defined, some properties changes. For instance:

- [Termination protection](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-protect-stacks.html) is automatically enabled.

On the other hand, not defining an environment generally means that the resources are ephemeral. These kind of deploy is used to create parts of applications to be tested by the QA team.
On the other hand, not defining an environment generally means that the resources are ephemeral. You can use this kind of deployment to create parts of your applications for your QA team to test before shipping to production.

As <Carlin /> passes this option to CloudFormation as a parameter, you can restrict the deployment to specifics environments:

```yaml title="cloudformation.yml"
Parameters:
Environment:
Type: String
AllowedValues:
- Staging
- Production
```
This way, the algorithm will ignore deployments without an environment or other than `Staging` and `Production`.

### environments

Expand Down
Loading

0 comments on commit 88b0444

Please sign in to comment.