Skip to content

Commit

Permalink
feat: some dx improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
phani-srikar committed Jul 29, 2023
1 parent 119fe97 commit 4bb037c
Show file tree
Hide file tree
Showing 8 changed files with 326 additions and 61 deletions.
4 changes: 0 additions & 4 deletions .codebuild/release_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ batch:
buildspec: .codebuild/test.yml
depend-on:
- build_linux
- identifier: lint
buildspec: .codebuild/lint.yml
depend-on:
- build_linux
- identifier: verify_api_extract
buildspec: .codebuild/verify_api_extract.yml
depend-on:
Expand Down
17 changes: 13 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@
"refresh-lockfile": "rimraf yarn.lock && yarn",
"extract-api": "lerna run extract-api",
"verify-api-extract": "yarn extract-api && ./scripts/verify-extract-api.sh",
"trigger-release": "source ./scripts/cloud-release.sh && release",
"trigger-release": "source ./scripts/cloud-release.sh && triggerRelease",
"trigger-tag-release": "source ./scripts/cloud-release.sh && triggerTagRelease",
"view-test-artifact": "./scripts/view-test-artifacts.sh",
"cleanup-stale-resources": "source ./scripts/cloud-utils.sh && cleanupStaleResources",
"cloud-e2e": "source scripts/cloud-utils.sh && cloudE2E",
"cloud-e2e-beta": "source scripts/cloud-utils.sh && cloudE2EBeta",
"split-e2e-tests": "yarn ts-node ./scripts/split-e2e-tests.ts && git add .codebuild/e2e_workflow.yml"
"split-e2e-tests": "yarn ts-node ./scripts/split-e2e-tests.ts && git add .codebuild/e2e_workflow.yml",
"view-test-artifacts": "yarn ts-node ./scripts/view-test-artifacts.ts",
"generate-debug-e2e-spec": "./scripts/generate-debug-spec-for-failed-e2e-tests.sh",
"cloud-e2e-debug": "source scripts/cloud-utils.sh && cloudE2EDebug"
},
"bugs": {
"url": "https://github.com/aws-amplify/amplify-codegen/issues"
Expand Down Expand Up @@ -71,11 +75,16 @@
"@commitlint/config-conventional": "^17.0.3",
"@commitlint/config-lerna-scopes": "^17.0.2",
"@microsoft/api-extractor": "^7.33.5",
"@types/cli-progress": "3.11.0",
"@types/fs-extra": "^8.0.1",
"@types/glob": "^7.2.0",
"@types/jest": "^27.0.0",
"@types/js-yaml": "^4.0.0",
"@typescript-eslint/eslint-plugin": "^4.0.0",
"@typescript-eslint/parser": "^4.0.0",
"amplify-headless-interface": "^1.13.1",
"aws-sdk": "2.1414.0",
"cli-progress": "3.12.0",
"cmd-shim": "^3.0.3",
"codecov": "^3.7.0",
"commitizen": "^4.2.4",
Expand All @@ -92,6 +101,7 @@
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-spellcheck": "^0.0.17",
"execa": "^5.1.1",
"glob": "^7.1.6",
"graphql-transformer-core": "^8.0.0",
"husky": "^3.0.3",
Expand All @@ -106,8 +116,7 @@
"rimraf": "^3.0.0",
"ts-jest": "^27.0.0",
"ts-node": "^8.10.1",
"typescript": "4.7.4",
"execa": "^5.1.1"
"typescript": "4.7.4"
},
"resolutions": {
"@aws-amplify/amplify-cli-core": "4.0.4",
Expand Down
15 changes: 13 additions & 2 deletions scripts/cloud-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@ export RELEASE_ROLE_NAME=CodebuildDeveloper
export RELEASE_PROFILE_NAME=AmplifyAPIPluginRelease
export RELEASE_PROJECT_NAME=amplify-codegen-release-workflow

function release {
function triggerRelease {
triggerProjectBatch $RELEASE_ACCOUNT_PROD $RELEASE_ROLE_NAME "${RELEASE_PROFILE_NAME}Prod" $RELEASE_PROJECT_NAME "release"
}

function triggerTagRelease {
branch_name=$(git branch --show-current)
echo "Running release workflow from branch ${branch_name}"
echo "Running tag release workflow from branch ${branch_name}"

git fetch origin
if [ $(git rev-parse HEAD) != $(git rev-parse origin/${branch_name}) ]
then
echo "You have local commits on branch ${branch_name} that are not pushed to origin. Push them before running the tag release workflow."
exit 1
fi
triggerProjectBatch $RELEASE_ACCOUNT_PROD $RELEASE_ROLE_NAME "${RELEASE_PROFILE_NAME}Prod" $RELEASE_PROJECT_NAME $branch_name
}
27 changes: 27 additions & 0 deletions scripts/cloud-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,30 @@ function cleanupStaleResources {
TARGET_BRANCH=$CURR_BRANCH
triggerProject $E2E_ACCOUNT_PROD $CLEANUP_ROLE_NAME $CLEANUP_PROFILE_NAME $CLEANUP_PROJECT_NAME $TARGET_BRANCH
}

function triggerProjectBatchWithDebugSession {
account_number=$1
role_name=$2
profile_name=$3
project_name=$4
target_branch=$5
authenticate $account_number $role_name $profile_name
echo AWS Account: $account_number
echo Project: $project_name
echo Target Branch: $target_branch
RESULT=$(aws codebuild start-build-batch --region=$REGION --profile="${profile_name}" --project-name $project_name --source-version=$target_branch \
--debug-session-enabled \
--buildspec-override "codebuild_specs/debug_workflow.yml" \
--environment-variables-override name=BRANCH_NAME,value=$target_branch,type=PLAINTEXT \
--query 'buildBatch.id' --output text)
echo "https://$REGION.console.aws.amazon.com/codesuite/codebuild/$account_number/projects/$project_name/batch/$RESULT?region=$REGION"
}

function cloudE2EDebug {
echo Running Prod E2E Test Suite
E2E_ROLE_NAME=CodebuildDeveloper
E2E_PROFILE_NAME=AmplifyAPIE2EProd
E2E_PROJECT_NAME=amplify-category-api-e2e-workflow
TARGET_BRANCH=$CURR_BRANCH
triggerProjectBatchWithDebugSession $E2E_ACCOUNT_PROD $E2E_ROLE_NAME $E2E_PROFILE_NAME $E2E_PROJECT_NAME $TARGET_BRANCH
}
30 changes: 30 additions & 0 deletions scripts/generate-debug-spec-for-failed-e2e-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

source ./scripts/cloud-utils.sh

function generatedDebugSpecForFailedTests {
# Check if an batch build name is provided
if [ $# -eq 0 ]; then
echo "Please provide the batch build id of codebuild"
exit 1
fi
# Get temporary access for the account
E2E_ROLE_NAME=CodebuildDeveloper
E2E_PROFILE_NAME=AmplifyAPIE2EProd
authenticate $E2E_ACCOUNT_PROD $E2E_ROLE_NAME "$E2E_PROFILE_NAME"
local batch_build_id=$1
echo "Getting failed test suites"
failed_tests=$(aws codebuild batch-get-build-batches --profile="$E2E_PROFILE_NAME" --ids "$batch_build_id" --region us-east-1 --query 'buildBatches[0].buildGroups' | jq -c '.[] | select(.currentBuildSummary.buildStatus == "FAILED").identifier')
if [ -z "$failed_tests" ]; then
echo "No failed tests found in batch $1"
exit 0
fi
echo $failed_tests | xargs yarn ts-node ./scripts/split-e2e-tests-codebuild.ts --debug
echo ""
echo "Next steps:"
echo "- add 'codebuild-breakpoint' to desired location in build specs or shared scripts"
echo "- git commit and push your changes"
echo "- run command 'yarn cloud-e2e-debug'"
}

generatedDebugSpecForFailedTests "$@"
22 changes: 18 additions & 4 deletions scripts/split-e2e-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ const REPO_ROOT = join(__dirname, '..');
const TEST_TIMINGS_PATH = join(REPO_ROOT, 'scripts', 'test-timings.data.json');
const CODEBUILD_CONFIG_BASE_PATH = join(REPO_ROOT, '.codebuild', 'e2e_workflow_base.yml');
const CODEBUILD_GENERATE_CONFIG_PATH = join(REPO_ROOT, '.codebuild', 'e2e_workflow.yml');
const CODEBUILD_DEBUG_CONFIG_PATH = join (REPO_ROOT, 'codebuild_specs', 'debug_workflow.yml');
const RUN_SOLO = [];
const EXCLUDE_TESTS = [
'src/__tests__/build-app-swift.test.ts',
'src/__tests__/build-app-android.test.ts',
];
const DEBUG_FLAG = '--debug';

export function loadConfigBase() {
return yaml.load(fs.readFileSync(CODEBUILD_CONFIG_BASE_PATH, 'utf8'));
}
export function saveConfig(config: any): void {
export function saveConfig(config: any, outputPath: string): void {
const output = ['# auto generated file. DO NOT EDIT manually', yaml.dump(config, { noRefs: true })];
fs.writeFileSync(CODEBUILD_GENERATE_CONFIG_PATH, output.join('\n'));
fs.writeFileSync(outputPath, output.join('\n'));
}
export function loadTestTimings(): { timingData: { test: string; medianRuntime: number }[] } {
return JSON.parse(fs.readFileSync(TEST_TIMINGS_PATH, 'utf-8'));
Expand Down Expand Up @@ -174,6 +176,7 @@ const splitTests = (
};

function main(): void {
const filteredTests = process.argv.slice(2);
const configBase: any = loadConfigBase();
const baseBuildGraph = configBase.batch['build-graph'];
const splitE2ETests = splitTests(
Expand All @@ -189,19 +192,30 @@ function main(): void {
(testSuites) => testSuites.filter((ts) => !EXCLUDE_TESTS.includes(ts)),
);

let outputPath = CODEBUILD_GENERATE_CONFIG_PATH;
let allBuilds = [...splitE2ETests];
if (filteredTests.length > 0) {
allBuilds = allBuilds.filter(build => filteredTests.includes(build.identifier));
if (filteredTests.includes(DEBUG_FLAG)) {
allBuilds = allBuilds.map(build => {
return { ...build, 'debug-session': true}
});
outputPath = CODEBUILD_DEBUG_CONFIG_PATH;
}
}
const cleanupResources = {
identifier: 'cleanup_e2e_resources',
buildspec: '.codebuild/cleanup_e2e_resources.yml',
env: {
'compute-type': 'BUILD_GENERAL1_MEDIUM'
},
'depend-on': [allBuilds[0].identifier]
'depend-on': allBuilds.length > 0 ? [allBuilds[0].identifier] : 'publish_to_local_registry',
}
console.log(`Total number of splitted jobs: ${allBuilds.length}`)
let currentBatch = [...baseBuildGraph, ...allBuilds, cleanupResources];
configBase.batch['build-graph'] = currentBatch;
saveConfig(configBase);
saveConfig(configBase, outputPath);
console.log(`Successfully generated the buildspec at ${outputPath}`);
}

main();
Loading

0 comments on commit 4bb037c

Please sign in to comment.