Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(component-workflows): build a generic workflows sdk from model types #430

Merged
merged 7 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
4 changes: 4 additions & 0 deletions packages/components/workflows/.projen/deps.json

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

2 changes: 1 addition & 1 deletion packages/components/workflows/.projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const project = new ProjenBlueprintComponent({
defaultReleaseBranch: 'main',
name: 'codecatalyst-workflows',
copyrightOwner: 'Amazon.com',
deps: ['yaml'],
deps: ['yaml', '@aws/codecatalyst-workflows-sdk'],
peerDeps: [
'projen',
'@amazon-codecatalyst/blueprint-component.source-repositories',
Expand Down
1 change: 1 addition & 0 deletions packages/components/workflows/package.json

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

2 changes: 1 addition & 1 deletion packages/components/workflows/src/actions/action-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
OutputDefinition,
} from './action';
import { WorkflowEnvironment } from '../environment/workflow-environment';
import { WorkflowDefinition } from '../workflow/workflow';
import { WorkflowDefinition } from '../workflow/workflow-definition';

export interface BuildActionConfiguration {
ActionRoleArn?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
InputsDefinition,
} from './action';
import { WorkflowEnvironment } from '../environment/workflow-environment';
import { WorkflowDefinition } from '../workflow/workflow';
import { WorkflowDefinition } from '../workflow/workflow-definition';

export interface CdkBootstrapActionConfiguration {
Region: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
convertInputsToJsonString,
} from './action';
import { WorkflowEnvironment } from '../environment/workflow-environment';
import { WorkflowDefinition } from '../workflow/workflow';
import { WorkflowDefinition } from '../workflow/workflow-definition';

export interface CdkDeployActionConfiguration {
StackName: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Blueprint } from '@amazon-codecatalyst/blueprints.blueprint';
import { ActionDefiniton, ActionIdentifierAlias, getDefaultActionIdentifier } from './action';
import { BuildActionParameters } from './action-build';
import { WorkflowDefinition } from '../workflow/workflow';
import { WorkflowDefinition } from '../workflow/workflow-definition';

export const DEFAULT_DELETE_RESOURCE_WORKFLOW_NAME = 'DANGER-hard-delete-deployed-resources';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Blueprint } from '@amazon-codecatalyst/blueprints.blueprint';
import { getDefaultActionIdentifier, ActionIdentifierAlias, ActionDefiniton } from './action';
import { WorkflowEnvironment } from '../environment/workflow-environment';
import { WorkflowDefinition } from '../workflow/workflow';
import { WorkflowDefinition } from '../workflow/workflow-definition';

export interface DeployInputConfiguration {
Artifacts: string[];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Blueprint } from '@amazon-codecatalyst/blueprints.blueprint';
import { ActionDefiniton, ActionIdentifierAlias, ComputeConfiguration, InputsDefinition, getDefaultActionIdentifier } from './action';
import { WorkflowDefinition } from '../workflow/workflow';
import { WorkflowDefinition } from '../workflow/workflow-definition';

export interface PublishBlueprintActionConfiguration {
ArtifactPackagePath: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Blueprint } from '@amazon-codecatalyst/blueprints.blueprint';
import { getDefaultActionIdentifier, ActionIdentifierAlias, ActionDefiniton } from './action';
import { WorkflowDefinition } from '../workflow/workflow';
import { WorkflowDefinition } from '../workflow/workflow-definition';

export interface TestReportActionParameters {
steps: Step[];
Expand Down
39 changes: 31 additions & 8 deletions packages/components/workflows/src/actions/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,57 @@ export enum ActionIdentifierAlias {
}

const ACTION_IDENTIFIERS: { [key: string]: { default: string; prod: string } } = {
build: {
'build': {
default: 'aws/build-beta@v1',
prod: 'aws/build@v1',
},
test: {
'test': {
default: 'aws/managed-test-gamma@v1',
prod: 'aws/managed-test@v1',
},
deploy: {
'deploy': {
default: 'aws/cfn-deploy-gamma@v1',
prod: 'aws/cfn-deploy@v1',
},
cdkDeploy: {
'cdkDeploy': {
default: 'aws/cdk-deploy-gamma@v1',
prod: 'aws/cdk-deploy@v1',
},
cdkBootstrap: {
'cdkBootstrap': {
default: 'aws/cdk-bootstrap-gamma@v1',
prod: 'aws/cdk-bootstrap@v1',
},
publishBlueprint: {
'aws/build@v1': {
default: 'aws/build-beta@v1',
prod: 'aws/build@v1',
},
'aws/managed-test@v1': {
default: 'aws/managed-test-gamma@v1',
prod: 'aws/managed-test@v1',
},
'aws/cfn-deploy@v1': {
default: 'aws/cfn-deploy-gamma@v1',
prod: 'aws/cfn-deploy@v1',
},
'aws/cdk-deploy@v1': {
default: 'aws/cdk-deploy-gamma@v1',
prod: 'aws/cdk-deploy@v1',
},
'aws/cdk-bootstrap@v1': {
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 {
return ACTION_IDENTIFIERS[alias]?.[environmentIdentifier] ?? ACTION_IDENTIFIERS[alias]?.default;
export function getDefaultActionIdentifier(identifer: string, environmentIdentifier: string = 'default'): string | undefined {
if (ACTION_IDENTIFIERS[identifer]) {
return ACTION_IDENTIFIERS[identifer]?.[environmentIdentifier] ?? ACTION_IDENTIFIERS[identifer]?.default;
}
return identifer;
}

type TypeSupportedCompute = ComputeConfiguration;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Blueprint } from '@amazon-codecatalyst/blueprints.blueprint';
import * as sdk from '@aws/codecatalyst-workflows-sdk';
import { getDefaultActionIdentifier } from '../actions/action';

/**
* This wraps the generated codecatalyst workflows sdk [@aws/codecatalyst-workflows-sdk].
* @experimental
*/
export class ActionGroupDefinitionBuilder {
blueprint: Blueprint;
definition: sdk.ActionGroup;

constructor(
blueprint: Blueprint,
options: {
starterDefinition?: Partial<sdk.ActionGroup>;
},
) {
this.blueprint = blueprint;
this.definition = {
...options.starterDefinition,
Actions: {},
};
}

setDependsOn(dependsOn: string[]) {
this.definition.DependsOn = dependsOn;
}

addAction<T extends sdk.Action>(name: string, action: T, _options?: {} | undefined): void {
this.definition.Actions = this.definition.Actions || {};
this.definition.Actions[name] = {
...action,
Identifier: getDefaultActionIdentifier(action.Identifier, this.blueprint.context.environmentId) as any,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Blueprint } from '@amazon-codecatalyst/blueprints.blueprint';
import * as sdk from '@aws/codecatalyst-workflows-sdk';
import { getDefaultActionIdentifier } from '../actions/action';

/**
* This wraps the generated codecatalyst workflows sdk [@aws/codecatalyst-workflows-sdk].
* @experimental - there may be backwards breaking model changes via @aws/codecatalyst-workflows-sdk
*/
export class WorkflowDefinitionBuilder {
blueprint: Blueprint;
genericBuilder: sdk.WorkflowDefinition;

constructor(
blueprint: Blueprint,
options?: {
starterDefinition?: Partial<sdk.Workflow>;
},
) {
const name = options?.starterDefinition?.Name || 'build-workflow';

this.genericBuilder = new sdk.WorkflowDefinition(name, {
workflow: options?.starterDefinition,
});
this.blueprint = blueprint;
}

setName(name: string) {
this.genericBuilder.definition.Name = name;
}

getDefinition(): sdk.Workflow {
return this.genericBuilder.definition;
}

setDefinition(definition: sdk.Workflow) {
this.genericBuilder.definition = definition;
}

addAction<T extends sdk.Action>(name: string, action: T, _options?: {} | undefined): void {
this.genericBuilder.addAction(name, {
...action,
Identifier: getDefaultActionIdentifier(action.Identifier, this.blueprint.context.environmentId),
});
}

addActionGroup<T extends sdk.ActionGroup>(name: string, actionGroup: T, _options?: {} | undefined): void {
this.genericBuilder.addAction(name, {
...actionGroup,
});
}
}
11 changes: 10 additions & 1 deletion packages/components/workflows/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './workflow/workflow';
export * from './workflow/workflow-definition';
export * from './workflow/workflow-builder';
export * from './workflow/sources';
export * from './workflow/triggers';
Expand All @@ -18,3 +18,12 @@ export * from './actions/action-cfn-deploy';
export * from './actions/action-test-reports';
export * from './actions/action-cdk-deploy';
export * from './actions/action-cdk-bootstrap';

export * from './workflow';

/**
* Experimental sdk generated from schemas
*/
export * as sdk from '@aws/codecatalyst-workflows-sdk';
export * from './generated-sdk/action-group-builder';
export * from './generated-sdk/workflow-definition-builder';
2 changes: 1 addition & 1 deletion packages/components/workflows/src/samples/empty.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ComputeType, ComputeFleet } from '../workflow/compute';
import { WorkflowDefinition } from '../workflow/workflow';
import { WorkflowDefinition } from '../workflow/workflow-definition';

export function makeEmptyWorkflow(): WorkflowDefinition {
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/components/workflows/src/samples/node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TriggerType } from '../workflow/triggers';
import { WorkflowDefinition } from '../workflow/workflow';
import { WorkflowDefinition } from '../workflow/workflow-definition';

export class NodeWorkflowDefinitionSamples {
public static readonly build: WorkflowDefinition = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
import { SourceFile, SourceRepository } from '@amazon-codecatalyst/blueprint-component.source-repositories';
import { Blueprint } from '@amazon-codecatalyst/blueprints.blueprint';
import * as sdk from '@aws/codecatalyst-workflows-sdk';
import { Component } from 'projen';
import * as YAML from 'yaml';
import { ComputeDefintion } from './compute';
import { SourceDefiniton } from './sources';
import { TriggerDefiniton } from './triggers';

export enum RunModeDefiniton {
PARALLEL = 'PARALLEL',
QUEUED = 'QUEUED',
SUPERSEDED = 'SUPERSEDED',
}

export interface WorkflowDefinition {
Name: string;
SchemaVersion?: string;
RunMode?: RunModeDefiniton;
Sources?: SourceDefiniton;
Triggers?: TriggerDefiniton[];
Compute?: ComputeDefintion;
Actions?: {
[id: string]: any;
};
}
import { WorkflowDefinition } from './workflow/workflow-definition';

export const workflowLocation = '.codecatalyst/workflows';
export interface WorkflowOptions {
/**
* Additional comments to be added to the top of the generated .yaml file of the workflow.
Expand All @@ -42,10 +24,13 @@ export interface WorkflowOptions {
YAMLOptions?: YAML.DocumentOptions & YAML.SchemaOptions & YAML.ParseOptions & YAML.CreateNodeOptions & YAML.ToStringOptions;
}

export const workflowLocation = '.codecatalyst/workflows';

export class Workflow extends Component {
constructor(blueprint: Blueprint, sourceRepository: SourceRepository, workflow: WorkflowDefinition | any, options?: WorkflowOptions) {
constructor(
blueprint: Blueprint,
sourceRepository: SourceRepository,
workflow: WorkflowDefinition | sdk.Workflow | any,
options?: WorkflowOptions,
) {
super(blueprint);

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/components/workflows/src/workflow/compute.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WorkflowDefinition } from './workflow';
import { WorkflowDefinition } from './workflow-definition';
export interface ComputeDefintion {
Type: ComputeType;
Fleet: ComputeFleet;
Expand Down
2 changes: 1 addition & 1 deletion packages/components/workflows/src/workflow/triggers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WorkflowDefinition } from './workflow';
import { WorkflowDefinition } from './workflow-definition';

export interface TriggerDefiniton {
Type: TriggerType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Blueprint } from '@amazon-codecatalyst/blueprints.blueprint';
import { addGenericBranchTrigger, addGenericPullRequestTrigger, PullRequestEvent } from './triggers';
import { WorkflowDefinition } from './workflow';
import { WorkflowDefinition } from './workflow-definition';
import { ComputeDefintion, TriggerDefiniton } from '..';
import { addGenericBuildAction, BuildActionParameters } from '../actions/action-build';
import { addGenericCdkBootstrapAction, CdkBootstrapActionParameters } from '../actions/action-cdk-bootstrap';
Expand Down
21 changes: 21 additions & 0 deletions packages/components/workflows/src/workflow/workflow-definition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ComputeDefintion } from './compute';
import { SourceDefiniton } from './sources';
import { TriggerDefiniton } from './triggers';

export enum RunModeDefiniton {
PARALLEL = 'PARALLEL',
QUEUED = 'QUEUED',
SUPERSEDED = 'SUPERSEDED',
}

export interface WorkflowDefinition {
Name: string;
SchemaVersion?: string;
RunMode?: RunModeDefiniton;
Sources?: SourceDefiniton;
Triggers?: TriggerDefiniton[];
Compute?: ComputeDefintion;
Actions?: {
[id: string]: any;
};
}
Loading