Skip to content

Commit

Permalink
feat: Add publish action to workflow builder
Browse files Browse the repository at this point in the history
  • Loading branch information
aggagen committed Nov 10, 2023
1 parent f74b4f5 commit 58f392c
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 49 deletions.
2 changes: 1 addition & 1 deletion packages/blueprints/blueprint-builder/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 53 additions & 47 deletions packages/blueprints/blueprint-builder/src/build-release-workflow.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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):';
const BUILD_ARTIFACT_NAME = 'codebase';

workflow.addBranchTrigger(['main']),
workflow.addBranchTrigger(['main']);
workflow.addTrigger({
Type: TriggerType.MANUAL,
});
Expand All @@ -15,9 +15,7 @@ export function buildReleaseWorkflow(workflow: WorkflowBuilder): WorkflowBuilder
Sources: ['WorkflowSource'],
},
output: {
Variables: [
'IS_RELEASE_COMMIT',
],
Variables: ['IS_RELEASE_COMMIT'],
},
steps: [
'TRIGGER_COMMIT_ID=$CATALYST_EVENT_SHA',
Expand All @@ -26,8 +24,7 @@ export function buildReleaseWorkflow(workflow: WorkflowBuilder): WorkflowBuilder
'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'],
Expand All @@ -37,55 +34,64 @@ export function buildReleaseWorkflow(workflow: WorkflowBuilder): WorkflowBuilder
IS_RELEASE_COMMIT: '${check_commit.IS_RELEASE_COMMIT}',
},
},
output: {},
output: {
Artifacts: [
{
Name: BUILD_ARTIFACT_NAME,
Files: ['**/*'],
},
],
},
steps: [
'if $IS_RELEASE_COMMIT; then echo \'This is a release commit, skipping\' && exit 1; fi',
"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',
],
}),

workflow.addBuildAction({
actionName: 'commit_changes',
});
// 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'],
input: {
inputs: {
Sources: ['WorkflowSource'],
Variables: {
IS_RELEASE_COMMIT: '${check_commit.IS_RELEASE_COMMIT}',
},
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}',
// },
// ],
},
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',
},
},
configuration: {
ArtifactPackagePath: 'dist/js/*.tgz',
PackageJSONPath: 'package.json',
InputArtifactName: BUILD_ARTIFACT_NAME,
TimeoutInSeconds: '120',
},
});

return workflow;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Blueprint } from '@amazon-codecatalyst/blueprints.blueprint';
import { ActionDefiniton, ActionIdentifierAlias, ComputeConfiguration, InputsDefinition, getDefaultActionIdentifier } from './action';
import { WorkflowDefinition } from '../workflow/workflow';

export interface PublishBlueprintActionConfiguration {
ArtifactPackagePath: string;
PackageJSONPath: string;
InputArtifactName: string;
TimeoutInSeconds?: string;
}

export interface PublishBlueprintActionParameters {
actionName: string;
inputs: InputsDefinition;
configuration: PublishBlueprintActionConfiguration;
dependsOn?: string[];
computeName?: ComputeConfiguration;
}

export function addGenericPublishBlueprintAction(
params: PublishBlueprintActionParameters & {
blueprint: Blueprint;
workflow: WorkflowDefinition;
},
): string {
const { blueprint, inputs, dependsOn, computeName, configuration } = params;
const actionName = (params.actionName || 'PublishBlueprint').replace(new RegExp('-', 'g'), '_');

const publishBlueprintAction: ActionDefiniton = {
Identifier: getDefaultActionIdentifier(ActionIdentifierAlias.publishBlueprint, blueprint.context.environmentId),
Inputs: inputs,
DependsOn: dependsOn,
Compute: computeName,
Configuration: configuration,
};

params.workflow.Actions = params.workflow.Actions || {};
params.workflow.Actions[actionName] = publishBlueprintAction;

return actionName;
}
10 changes: 9 additions & 1 deletion packages/components/workflows/src/actions/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BuildActionConfiguration } from './action-build';
import { CdkBootstrapActionConfiguration } from './action-cdk-bootstrap';
import { CdkDeployActionYamlOutput } from './action-cdk-deploy';
import { CfnDeployActionConfiguration } from './action-cfn-deploy';
import { PublishBlueprintActionConfiguration } from './action-publish-blueprint';
import { TestActionConfiguration } from './action-test-reports';
import { WorkflowEnvironment } from '../environment/workflow-environment';

Expand All @@ -16,6 +17,7 @@ export enum ActionIdentifierAlias {
test = 'test',
cdkDeploy = 'cdkDeploy',
cdkBootstrap = 'cdkBootstrap',
publishBlueprint = 'publishBlueprint',
}

const ACTION_IDENTIFIERS: { [key: string]: { default: string; prod: string } } = {
Expand All @@ -39,6 +41,10 @@ const ACTION_IDENTIFIERS: { [key: string]: { default: string; prod: string } } =
default: 'aws/cdk-bootstrap-gamma@v1',
prod: 'aws/cdk-bootstrap@v1',
},
publishBlueprint: {
default: 'aws/publish-blueprint-action@v1',
prod: 'aws/publish-blueprint-action@v1',
},
};

export function getDefaultActionIdentifier(alias: ActionIdentifierAlias, environmentIdentifier: string = 'default'): string | undefined {
Expand All @@ -51,7 +57,9 @@ type TypeSupportedActions =
| CfnDeployActionConfiguration
| TestActionConfiguration
| CdkDeployActionYamlOutput
| CdkBootstrapActionConfiguration;
| CdkBootstrapActionConfiguration
| PublishBlueprintActionConfiguration;

export interface ActionDefiniton {
Identifier?: string;
Compute?: TypeSupportedCompute | string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { addGenericCdkBootstrapAction, CdkBootstrapActionParameters } from '../a
import { addGenericCdkDeployAction, CdkDeployActionParameters } from '../actions/action-cdk-deploy';
import { addGenericCloudFormationCleanupAction, CfnCleanupActionParameters } from '../actions/action-cfn-cleanup';
import { addGenericCloudFormationDeployAction, CfnDeployActionParameters } from '../actions/action-cfn-deploy';
import { addGenericPublishBlueprintAction, PublishBlueprintActionParameters } from '../actions/action-publish-blueprint';
import { addGenericTestReports, TestReportActionParameters } from '../actions/action-test-reports';

export class WorkflowBuilder {
Expand Down Expand Up @@ -99,4 +100,12 @@ export class WorkflowBuilder {
workflow: this.definition,
});
}

addPublishBlueprintAction(configuration: PublishBlueprintActionParameters) {
addGenericPublishBlueprintAction({
...configuration,
blueprint: this.blueprint,
workflow: this.definition,
});
}
}

0 comments on commit 58f392c

Please sign in to comment.