Skip to content

Commit

Permalink
chore: add cleanup resources script for codebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronZyLee committed Jul 12, 2023
1 parent 8fb4331 commit a81e3f6
Show file tree
Hide file tree
Showing 9 changed files with 799 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .codebuild/cleanup_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ phases:
build:
commands:
- yarn production-build
- cd packages/amplify-e2e-tests && yarn clean-cb-e2e-resources
- cd packages/amplify-codegen-e2e-tests && yarn clean-cb-e2e-resources
22 changes: 11 additions & 11 deletions packages/amplify-codegen-e2e-core/src/init/initProjectHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nspawn as spawn, getCLIPath, singleSelect, addCircleCITags } from '..';
import { nspawn as spawn, getCLIPath, singleSelect, addCITags } from '..';
import { KEY_DOWN_ARROW, AmplifyFrontend } from '../utils';
import { amplifyRegions } from '../configure';
import { v4 as uuid } from 'uuid';
Expand Down Expand Up @@ -32,7 +32,7 @@ export function initJSProjectWithProfile(cwd: string, settings: Object = {}): Pr
};
}

addCircleCITags(cwd);
addCITags(cwd);

const cliArgs = ['init'];
const providerConfigSpecified = !!s.providerConfig && typeof s.providerConfig === 'object';
Expand Down Expand Up @@ -89,7 +89,7 @@ export function initJSProjectWithProfile(cwd: string, settings: Object = {}): Pr
export function initAndroidProjectWithProfile(cwd: string, settings: Object): Promise<void> {
const s = { ...defaultSettings, ...settings };

addCircleCITags(cwd);
addCITags(cwd);

return new Promise((resolve, reject) => {
spawn(getCLIPath(), ['init'], {
Expand Down Expand Up @@ -120,7 +120,7 @@ export function initAndroidProjectWithProfile(cwd: string, settings: Object): Pr
.wait('Try "amplify add api" to create a backend API and then "amplify push" to deploy everything')
.run((err: Error) => {
if (!err) {
addCircleCITags(cwd);
addCITags(cwd);

resolve();
} else {
Expand All @@ -133,7 +133,7 @@ export function initAndroidProjectWithProfile(cwd: string, settings: Object): Pr
export function initIosProjectWithProfile(cwd: string, settings: Object): Promise<void> {
const s = { ...defaultSettings, ...settings };

addCircleCITags(cwd);
addCITags(cwd);

return new Promise((resolve, reject) => {
spawn(getCLIPath(), ['init'], {
Expand Down Expand Up @@ -163,7 +163,7 @@ export function initIosProjectWithProfile(cwd: string, settings: Object): Promis
.wait('Try "amplify add api" to create a backend API and then "amplify push" to deploy everything')
.run((err: Error) => {
if (!err) {
addCircleCITags(cwd);
addCITags(cwd);

resolve();
} else {
Expand All @@ -176,7 +176,7 @@ export function initIosProjectWithProfile(cwd: string, settings: Object): Promis
export function initFlutterProjectWithProfile(cwd: string, settings: Object): Promise<void> {
const s = { ...defaultSettings, ...settings };

addCircleCITags(cwd);
addCITags(cwd);

return new Promise((resolve, reject) => {
let chain = spawn(getCLIPath(), ['init'], { cwd, stripColors: true })
Expand Down Expand Up @@ -220,7 +220,7 @@ export function initProjectWithAccessKey(
): Promise<void> {
const s = { ...defaultSettings, ...settings };

addCircleCITags(cwd);
addCITags(cwd);

return new Promise((resolve, reject) => {
let chain = spawn(getCLIPath(), ['init'], {
Expand Down Expand Up @@ -275,7 +275,7 @@ export function initProjectWithAccessKey(
}

export function initNewEnvWithAccessKey(cwd: string, s: { envName: string; accessKeyId: string; secretAccessKey: string }): Promise<void> {
addCircleCITags(cwd);
addCITags(cwd);

return new Promise((resolve, reject) => {
let chain = spawn(getCLIPath(), ['init'], {
Expand Down Expand Up @@ -314,7 +314,7 @@ export function initNewEnvWithAccessKey(cwd: string, s: { envName: string; acces
}

export function initNewEnvWithProfile(cwd: string, s: { envName: string }): Promise<void> {
addCircleCITags(cwd);
addCITags(cwd);

return new Promise((resolve, reject) => {
spawn(getCLIPath(), ['init'], {
Expand Down Expand Up @@ -354,7 +354,7 @@ export function amplifyInitSandbox(cwd: string, settings: {}): Promise<void> {
};
}

addCircleCITags(cwd);
addCITags(cwd);

return new Promise((resolve, reject) => {
spawn(getCLIPath(), ['init'], { cwd, stripColors: true, env })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,49 @@ declare global {
}
}
}
export const addCITags = (projectPath: string): void => {
if (process.env && process.env['CODEBUILD']) {
addCodeBuildCITags(projectPath);
}
else if(process.env && process.env['CIRCLECI']) {
addCircleCITags(projectPath);
}
}

/**
* Add CI tags for code build
* Refer https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html
* @param projectPath
*/
export const addCodeBuildCITags = (projectPath: string): void => {
const tags = stateManager.getProjectTags(projectPath);

const addTagIfNotExist = (key: string, value: string): void => {
if (!tags.find(t => t.Key === key)) {
tags.push({
Key: key,
Value: value,
});
}
};
addTagIfNotExist('codebuild', sanitizeTagValue(process.env['CODEBUILD'] || 'N/A'));
addTagIfNotExist('codebuild:batch_build_identifier', sanitizeTagValue(process.env['CODEBUILD_BATCH_BUILD_IDENTIFIER'] || 'N/A'));
addTagIfNotExist('codebuild:build_id', sanitizeTagValue(process.env['CODEBUILD_BUILD_ID'] || 'N/A'));
// exposed by custom CLI test environment
if (global.getTestName) {
addTagIfNotExist('jest:test_name', sanitizeTagValue(global.getTestName().substr(0, 255) || 'N/A'));
}
if (global.getHookName) {
addTagIfNotExist('jest:hook_name', sanitizeTagValue(global.getHookName().substr(0, 255) || 'N/A'));
}
if (global.getDescibeBlocks) {
global.getDescibeBlocks().forEach((blockName, i) => {
addTagIfNotExist(`jest:describe_${i + 1}`, sanitizeTagValue(blockName.substr(0, 255) || 'N/A'));
});
}

stateManager.setProjectFileTags(projectPath, tags);
};

export const addCircleCITags = (projectPath: string): void => {
if (process.env && process.env['CIRCLECI']) {
Expand Down
2 changes: 1 addition & 1 deletion packages/amplify-codegen-e2e-core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as rimraf from 'rimraf';
import { config } from 'dotenv';
import execa from 'execa';

export * from './add-circleci-tags';
export * from './add-ci-tags';
export * from './api';
export * from './appsync';
export * from './envVars';
Expand Down
4 changes: 2 additions & 2 deletions packages/amplify-codegen-e2e-core/src/utils/pinpoint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Pinpoint } from 'aws-sdk';
import { getCLIPath, nspawn as spawn, singleSelect, amplifyRegions, addCircleCITags, KEY_DOWN_ARROW } from '..';
import { getCLIPath, nspawn as spawn, singleSelect, amplifyRegions, addCITags, KEY_DOWN_ARROW } from '..';
import _ from 'lodash';

const settings = {
Expand Down Expand Up @@ -70,7 +70,7 @@ export async function pinpointAppExist(pinpointProjectId: string): Promise<boole
}

export function initProjectForPinpoint(cwd: string): Promise<void> {
addCircleCITags(cwd);
addCITags(cwd);

return new Promise((resolve, reject) => {
let chain = spawn(getCLIPath(), ['init'], {
Expand Down
4 changes: 2 additions & 2 deletions packages/amplify-codegen-e2e-core/src/utils/sdk-calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export const bucketNotExists = async (bucket: string) => {
}
};

export const deleteS3Bucket = async (bucket: string) => {
const s3 = new S3();
export const deleteS3Bucket = async (bucket: string, providedS3Client: S3 | undefined = undefined) => {
const s3 = providedS3Client ? providedS3Client : new S3();
let continuationToken: Required<Pick<S3.ListObjectVersionsOutput, 'KeyMarker' | 'VersionIdMarker'>> = undefined;
const objectKeyAndVersion = <S3.ObjectIdentifier[]>[];
let truncated = false;
Expand Down
3 changes: 2 additions & 1 deletion packages/amplify-codegen-e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"scripts": {
"e2e": "npm run setup-profile && jest --verbose",
"setup-profile": "ts-node ./src/configure_tests.ts",
"clean-e2e-resources": "ts-node ./src/cleanup-e2e-resources.ts"
"clean-e2e-resources": "ts-node ./src/cleanup-e2e-resources.ts",
"clean-cb-e2e-resources": "ts-node ./src/cleanup-cb-e2e-resources.ts"
},
"dependencies": {
"@aws-amplify/amplify-codegen-e2e-core": "1.4.9",
Expand Down
Loading

0 comments on commit a81e3f6

Please sign in to comment.