From f74b4f564920307ecb7cfcac2460cedb3e318e53 Mon Sep 17 00:00:00 2001 From: alexforsyth Date: Thu, 9 Nov 2023 13:45:46 -0500 Subject: [PATCH] feat: inital ci-cd workflow --- .../blueprints/blueprint-builder/package.json | 2 +- .../blueprint-builder/src/blueprint.ts | 16 ++++ .../src/build-release-workflow.ts | 91 +++++++++++++++++++ .../blueprint-builder/src/defaults.json | 3 +- 4 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 packages/blueprints/blueprint-builder/src/build-release-workflow.ts diff --git a/packages/blueprints/blueprint-builder/package.json b/packages/blueprints/blueprint-builder/package.json index 77a98f81c..6c3b0a7f8 100644 --- a/packages/blueprints/blueprint-builder/package.json +++ b/packages/blueprints/blueprint-builder/package.json @@ -69,7 +69,7 @@ "main": "lib/index.js", "license": "Apache-2.0", "homepage": "https://aws.amazon.com/", - "version": "0.3.24", + "version": "0.3.25-preview.0", "types": "lib/index.d.ts", "publishingSpace": "blueprints", "mediaUrls": [ diff --git a/packages/blueprints/blueprint-builder/src/blueprint.ts b/packages/blueprints/blueprint-builder/src/blueprint.ts index 6efc13c77..66f5eab2f 100644 --- a/packages/blueprints/blueprint-builder/src/blueprint.ts +++ b/packages/blueprints/blueprint-builder/src/blueprint.ts @@ -3,6 +3,7 @@ import devEnvPackage from '@amazon-codecatalyst/blueprint-component.dev-environm import envPackage from '@amazon-codecatalyst/blueprint-component.environments/package.json'; import { SourceRepository, SourceFile, StaticAsset, File } from '@amazon-codecatalyst/blueprint-component.source-repositories'; import sourceReposPackage from '@amazon-codecatalyst/blueprint-component.source-repositories/package.json'; +import { Workflow, WorkflowBuilder } from '@amazon-codecatalyst/blueprint-component.workflows'; import workflowsPackage from '@amazon-codecatalyst/blueprint-component.workflows/package.json'; import cliPackage from '@amazon-codecatalyst/blueprint-util.cli/package.json'; import { ProjenBlueprint, ProjenBlueprintOptions } from '@amazon-codecatalyst/blueprint-util.projen-blueprint'; @@ -15,6 +16,7 @@ import { } from '@amazon-codecatalyst/blueprints.blueprint'; import baseBlueprintPackage from '@amazon-codecatalyst/blueprints.blueprint/package.json'; import * as decamelize from 'decamelize'; +import { buildReleaseWorkflow } from './build-release-workflow'; import defaults from './defaults.json'; devEnvPackage.version; @@ -71,6 +73,12 @@ export interface Options extends ParentOptions { * @validationMessage Must contain only upper and lowercase letters, numbers and underscores, spaces, dashes */ repositoryName?: string; + + /** + * Generate a release workflow? + * If this is set, the blueprint will generate a release workflow. On push to main, a workflow will release this blueprint into your codecatalyst space. + */ + releaseWorkflow?: boolean; }; } @@ -226,6 +234,14 @@ export class Blueprint extends ParentBlueprint { ], }, }); + + /** + * todo: remove this once the release workflow is available in production + */ + if (this.context.environmentId == 'default' || options.advancedSettings.releaseWorkflow) { + const releaseWorkflow = new WorkflowBuilder(this); + new Workflow(this, repository, buildReleaseWorkflow(releaseWorkflow).getDefinition()); + } } synth(): void { diff --git a/packages/blueprints/blueprint-builder/src/build-release-workflow.ts b/packages/blueprints/blueprint-builder/src/build-release-workflow.ts new file mode 100644 index 000000000..523e69e61 --- /dev/null +++ b/packages/blueprints/blueprint-builder/src/build-release-workflow.ts @@ -0,0 +1,91 @@ +import { TriggerType, WorkflowBuilder } from '@amazon-codecatalyst/blueprint-component.workflows'; + +export function buildReleaseWorkflow(workflow: WorkflowBuilder): WorkflowBuilder { + + workflow.setName('blueprint-release'); + const RELEASE_COMMIT_PREFIX = 'chore(release):'; + + workflow.addBranchTrigger(['main']), + workflow.addTrigger({ + Type: TriggerType.MANUAL, + }); + workflow.addBuildAction({ + actionName: 'check_commit', + input: { + Sources: ['WorkflowSource'], + }, + output: { + Variables: [ + 'IS_RELEASE_COMMIT', + ], + }, + steps: [ + 'TRIGGER_COMMIT_ID=$CATALYST_EVENT_SHA', + 'COMMIT_MESSAGE="$(git log -n 1 $TRIGGER_COMMIT_ID --oneline)"', + `RELEASE_PREFIX='${RELEASE_COMMIT_PREFIX}'`, + 'IS_RELEASE_COMMIT=false', + 'if grep -q "$RELEASE_PREFIX" <<< "$COMMIT_MESSAGE"; then echo \'this is a release commit\' && IS_RELEASE_COMMIT=true; fi', + ], + }), + + workflow.addBuildAction({ + actionName: 'build_blueprint', + dependsOn: ['check_commit'], + 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', + 'yarn', + 'yarn build', + 'yarn bump', + 'yarn blueprint:package', + ], + }), + + 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.setDefinition({ + ...workflow.getDefinition(), + Actions: { + ...workflow.definition.Actions, + publish_blueprint: { + Identifier: 'aws/publish-blueprint-action', + dependsOn: ['commit_changes'], + Inputs: { + Sources: ['WorkflowSource'], + Variables: { + IS_RELEASE_COMMIT: '${check_commit.IS_RELEASE_COMMIT}', + }, + }, + Configuration: { + ArtifactPackagePath: 'dist/*.tgz', + PackageJSONPath: 'package.json', + TimeoutInSeconds: '120', + }, + }, + }, + }); + return workflow; +} \ No newline at end of file diff --git a/packages/blueprints/blueprint-builder/src/defaults.json b/packages/blueprints/blueprint-builder/src/defaults.json index 1bea7971b..9e45ab9ae 100644 --- a/packages/blueprints/blueprint-builder/src/defaults.json +++ b/packages/blueprints/blueprint-builder/src/defaults.json @@ -6,6 +6,7 @@ "blueprintPackageName": "", "repositoryName": "", "license": "Apache-2.0", - "tags": ["first-label", "second-label"] + "tags": ["first-label", "second-label"], + "releaseWorkflow": true } }