Skip to content

Commit ceae91a

Browse files
authored
chore: add cleanup resources script for codebuild (#622)
* chore: add cleanup resources script for codebuild * fix lint error
1 parent ee9ac44 commit ceae91a

File tree

9 files changed

+799
-20
lines changed

9 files changed

+799
-20
lines changed

.codebuild/scripts/lint_pr.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ if [ -z "$PR_NUM" ]; then
88
fi
99

1010
# get PR file list, filter out removed files, filter only JS/TS files, then pass to the linter
11-
curl -fsSL https://api.github.com/repos/$PROJECT_USERNAME/$REPO_NAME/pulls/$PR_NUM/files | jq -r '.[] | select(.status!="removed") | .filename' | grep -E '\.(js|jsx|ts|tsx)$' || true | xargs yarn eslint
11+
curl -fsSL https://api.github.com/repos/$PROJECT_USERNAME/$REPO_NAME/pulls/$PR_NUM/files | jq -r '.[] | select(.status!="removed") | .filename' | (grep -E '\.(js|jsx|ts|tsx)$' || true) | xargs yarn eslint --quiet
1212
set +x

packages/amplify-codegen-e2e-core/src/init/initProjectHelper.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { nspawn as spawn, getCLIPath, singleSelect, addCircleCITags } from '..';
1+
import { nspawn as spawn, getCLIPath, singleSelect, addCITags } from '..';
22
import { KEY_DOWN_ARROW, AmplifyFrontend } from '../utils';
33
import { amplifyRegions } from '../configure';
44
import { v4 as uuid } from 'uuid';
@@ -32,7 +32,7 @@ export function initJSProjectWithProfile(cwd: string, settings: Object = {}): Pr
3232
};
3333
}
3434

35-
addCircleCITags(cwd);
35+
addCITags(cwd);
3636

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

92-
addCircleCITags(cwd);
92+
addCITags(cwd);
9393

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

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

136-
addCircleCITags(cwd);
136+
addCITags(cwd);
137137

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

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

179-
addCircleCITags(cwd);
179+
addCITags(cwd);
180180

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

223-
addCircleCITags(cwd);
223+
addCITags(cwd);
224224

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

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

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

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

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

357-
addCircleCITags(cwd);
357+
addCITags(cwd);
358358

359359
return new Promise((resolve, reject) => {
360360
spawn(getCLIPath(), ['init'], { cwd, stripColors: true, env })

packages/amplify-codegen-e2e-core/src/utils/add-circleci-tags.ts renamed to packages/amplify-codegen-e2e-core/src/utils/add-ci-tags.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,49 @@ declare global {
99
}
1010
}
1111
}
12+
export const addCITags = (projectPath: string): void => {
13+
if (process.env && process.env['CODEBUILD']) {
14+
addCodeBuildCITags(projectPath);
15+
}
16+
else if(process.env && process.env['CIRCLECI']) {
17+
addCircleCITags(projectPath);
18+
}
19+
}
20+
21+
/**
22+
* Add CI tags for code build
23+
* Refer https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html
24+
* @param projectPath
25+
*/
26+
export const addCodeBuildCITags = (projectPath: string): void => {
27+
const tags = stateManager.getProjectTags(projectPath);
28+
29+
const addTagIfNotExist = (key: string, value: string): void => {
30+
if (!tags.find(t => t.Key === key)) {
31+
tags.push({
32+
Key: key,
33+
Value: value,
34+
});
35+
}
36+
};
37+
addTagIfNotExist('codebuild', sanitizeTagValue(process.env['CODEBUILD'] || 'N/A'));
38+
addTagIfNotExist('codebuild:batch_build_identifier', sanitizeTagValue(process.env['CODEBUILD_BATCH_BUILD_IDENTIFIER'] || 'N/A'));
39+
addTagIfNotExist('codebuild:build_id', sanitizeTagValue(process.env['CODEBUILD_BUILD_ID'] || 'N/A'));
40+
// exposed by custom CLI test environment
41+
if (global.getTestName) {
42+
addTagIfNotExist('jest:test_name', sanitizeTagValue(global.getTestName().substr(0, 255) || 'N/A'));
43+
}
44+
if (global.getHookName) {
45+
addTagIfNotExist('jest:hook_name', sanitizeTagValue(global.getHookName().substr(0, 255) || 'N/A'));
46+
}
47+
if (global.getDescibeBlocks) {
48+
global.getDescibeBlocks().forEach((blockName, i) => {
49+
addTagIfNotExist(`jest:describe_${i + 1}`, sanitizeTagValue(blockName.substr(0, 255) || 'N/A'));
50+
});
51+
}
52+
53+
stateManager.setProjectFileTags(projectPath, tags);
54+
};
1255

1356
export const addCircleCITags = (projectPath: string): void => {
1457
if (process.env && process.env['CIRCLECI']) {

packages/amplify-codegen-e2e-core/src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as rimraf from 'rimraf';
44
import { config } from 'dotenv';
55
import execa from 'execa';
66

7-
export * from './add-circleci-tags';
7+
export * from './add-ci-tags';
88
export * from './api';
99
export * from './appsync';
1010
export * from './envVars';

packages/amplify-codegen-e2e-core/src/utils/pinpoint.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Pinpoint } from 'aws-sdk';
2-
import { getCLIPath, nspawn as spawn, singleSelect, amplifyRegions, addCircleCITags, KEY_DOWN_ARROW } from '..';
2+
import { getCLIPath, nspawn as spawn, singleSelect, amplifyRegions, addCITags, KEY_DOWN_ARROW } from '..';
33
import _ from 'lodash';
44

55
const settings = {
@@ -70,7 +70,7 @@ export async function pinpointAppExist(pinpointProjectId: string): Promise<boole
7070
}
7171

7272
export function initProjectForPinpoint(cwd: string): Promise<void> {
73-
addCircleCITags(cwd);
73+
addCITags(cwd);
7474

7575
return new Promise((resolve, reject) => {
7676
let chain = spawn(getCLIPath(), ['init'], {

packages/amplify-codegen-e2e-core/src/utils/sdk-calls.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export const bucketNotExists = async (bucket: string) => {
4242
}
4343
};
4444

45-
export const deleteS3Bucket = async (bucket: string) => {
46-
const s3 = new S3();
45+
export const deleteS3Bucket = async (bucket: string, providedS3Client: S3 | undefined = undefined) => {
46+
const s3 = providedS3Client ? providedS3Client : new S3();
4747
let continuationToken: Required<Pick<S3.ListObjectVersionsOutput, 'KeyMarker' | 'VersionIdMarker'>> = undefined;
4848
const objectKeyAndVersion = <S3.ObjectIdentifier[]>[];
4949
let truncated = false;

packages/amplify-codegen-e2e-tests/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"scripts": {
2020
"e2e": "npm run setup-profile && jest --verbose",
2121
"setup-profile": "ts-node ./src/configure_tests.ts",
22-
"clean-e2e-resources": "ts-node ./src/cleanup-e2e-resources.ts"
22+
"clean-e2e-resources": "ts-node ./src/cleanup-e2e-resources.ts",
23+
"clean-cb-e2e-resources": "ts-node ./src/cleanup-cb-e2e-resources.ts"
2324
},
2425
"dependencies": {
2526
"@aws-amplify/amplify-codegen-e2e-core": "1.4.9",

0 commit comments

Comments
 (0)