From dbc3a8bead1d5ad83688dd721f26b132fc1307ba Mon Sep 17 00:00:00 2001 From: Andy Gagen Date: Thu, 16 Nov 2023 15:16:07 -0800 Subject: [PATCH] feat: Add release script --- .../blueprint-builder/src/blueprint.ts | 6 ++- .../src/build-release-workflow.ts | 53 +++++-------------- .../static-assets/release.sh | 35 ++++++++++++ 3 files changed, 54 insertions(+), 40 deletions(-) create mode 100644 packages/blueprints/blueprint-builder/static-assets/release.sh diff --git a/packages/blueprints/blueprint-builder/src/blueprint.ts b/packages/blueprints/blueprint-builder/src/blueprint.ts index 66f5eab2f..243bab57d 100644 --- a/packages/blueprints/blueprint-builder/src/blueprint.ts +++ b/packages/blueprints/blueprint-builder/src/blueprint.ts @@ -166,6 +166,10 @@ export class Blueprint extends ParentBlueprint { // copy-paste additional code over it StaticAsset.findAll().forEach(asset => { + if (asset.path() === 'release.sh') { + return; + } + new File(repository, asset.path(), asset.content()); }); @@ -240,7 +244,7 @@ export class Blueprint extends ParentBlueprint { */ if (this.context.environmentId == 'default' || options.advancedSettings.releaseWorkflow) { const releaseWorkflow = new WorkflowBuilder(this); - new Workflow(this, repository, buildReleaseWorkflow(releaseWorkflow).getDefinition()); + new Workflow(this, repository, buildReleaseWorkflow(releaseWorkflow, repository).getDefinition()); } } diff --git a/packages/blueprints/blueprint-builder/src/build-release-workflow.ts b/packages/blueprints/blueprint-builder/src/build-release-workflow.ts index 38a085245..c81e9b26a 100644 --- a/packages/blueprints/blueprint-builder/src/build-release-workflow.ts +++ b/packages/blueprints/blueprint-builder/src/build-release-workflow.ts @@ -1,10 +1,14 @@ +import { SourceFile, SourceRepository, SubstitionAsset } from '@amazon-codecatalyst/blueprint-component.source-repositories'; import { TriggerType, WorkflowBuilder } from '@amazon-codecatalyst/blueprint-component.workflows'; -export function buildReleaseWorkflow(workflow: WorkflowBuilder): WorkflowBuilder { +export function buildReleaseWorkflow(workflow: WorkflowBuilder, repository: SourceRepository): WorkflowBuilder { workflow.setName('blueprint-release'); const RELEASE_COMMIT_PREFIX = 'chore(release):'; const BUILD_ARTIFACT_NAME = 'codebase'; + const releaseScript = new SubstitionAsset('release.sh'); + new SourceFile(repository, 'release.sh', releaseScript.substitute({ commitPrefix: RELEASE_COMMIT_PREFIX })); + workflow.addBranchTrigger(['main']); workflow.addTrigger({ Type: TriggerType.MANUAL, @@ -26,7 +30,7 @@ export function buildReleaseWorkflow(workflow: WorkflowBuilder): WorkflowBuilder ], }); workflow.addBuildAction({ - actionName: 'build_blueprint', + actionName: 'build_and_commit', dependsOn: ['check_commit'], input: { Sources: ['WorkflowSource'], @@ -42,48 +46,19 @@ export function buildReleaseWorkflow(workflow: WorkflowBuilder): WorkflowBuilder }, ], }, - steps: [ - "if $IS_RELEASE_COMMIT; then echo 'This is a release commit, skipping' && exit 1; fi", - 'yum install -y rsync', - 'npm install -g yarn', - 'yarn', - 'yarn build', - 'yarn bump', - 'yarn blueprint:package', - ], + steps: ["if $IS_RELEASE_COMMIT; then echo 'This is a release commit, skipping'; else chmod +x release.sh && ./release.sh; fi"], }); - // TODO: Build actions can't push back to source yet: - // workflow.addBuildAction({ - // actionName: 'commit_changes', - // dependsOn: ['build_blueprint'], - // input: { - // Sources: ['WorkflowSource'], - // Variables: { - // IS_RELEASE_COMMIT: '${check_commit.IS_RELEASE_COMMIT}', - // }, - // }, - // output: {}, - // steps: [ - // "if $IS_RELEASE_COMMIT; then echo 'This is a release commit, skipping' && exit 1; fi", - // `RELEASE_COMMIT_MESSAGE="${RELEASE_COMMIT_PREFIX} release on $(date +"%Y %m %d %H:%M:%S")"`, - // 'git add .', - // 'git commit -m $RELEASE_COMMIT_MESSAGE', - // 'git push --force', - // ], - // }); workflow.addPublishBlueprintAction({ actionName: 'publish_blueprint', - dependsOn: ['build_blueprint'], + dependsOn: ['build_and_commit'], inputs: { - Sources: ['WorkflowSource'], Artifacts: [BUILD_ARTIFACT_NAME], - // TODO: The action doesn't handle this env var correctly: - // Variables: [ - // { - // Name: 'IS_RELEASE_COMMIT', - // Value: '${check_commit.IS_RELEASE_COMMIT}', - // }, - // ], + Variables: [ + { + Name: 'IS_RELEASE_COMMIT', + Value: '${check_commit.IS_RELEASE_COMMIT}', + }, + ], }, configuration: { ArtifactPackagePath: 'dist/js/*.tgz', diff --git a/packages/blueprints/blueprint-builder/static-assets/release.sh b/packages/blueprints/blueprint-builder/static-assets/release.sh new file mode 100644 index 000000000..bff7930de --- /dev/null +++ b/packages/blueprints/blueprint-builder/static-assets/release.sh @@ -0,0 +1,35 @@ +# This script is used by the blueprint-release workflow to build +# the blueprint, bump its package version, and commit the version +# bump back into the repository. +echo "Installing dependencies..." +yum install -y rsync +npm install -g yarn +yarn + +echo "Building blueprint..." +yarn build + +echo "Bumping package version..." +yarn bump +NEW_VERSION=`jq -r .version package.json` +yarn blueprint:package + +echo "Getting credentials..." +MI=`curl $AWS_CONTAINER_TOKEN_ENDPOINT` +ACCESS_KEY_ID=$(echo "$MI" | jq -r '.AccessKeyId') +SECRET_ACCESS_KEY=$(echo "$MI" | jq -r '.SecretAccessKey') +ORIGINAL_REMOTE=`git config --get remote.origin.url` +SOURCE_REPO_URL=`sed -e "s^//^//$ACCESS_KEY_ID:$SECRET_ACCESS_KEY@^" <<< $ORIGINAL_REMOTE` + +echo "Configuring git..." +git remote set-url origin $SOURCE_REPO_URL +git config --global user.email "noreply@amazon.com" +git config --global user.name "Release Workflow" +git add . + +echo "Committing changes..." +RELEASE_COMMIT_MESSAGE="{{commitPrefix}} release $NEW_VERSION" +git commit -m "$RELEASE_COMMIT_MESSAGE" + +echo "Pushing to origin..." +git push origin HEAD:main