Skip to content

Commit

Permalink
feat(stepfunctions-tasks): support complete overridings of start buil…
Browse files Browse the repository at this point in the history
…d of codebuild.
  • Loading branch information
zxkane committed Sep 15, 2020
1 parent f5c5d06 commit ff616b1
Show file tree
Hide file tree
Showing 5 changed files with 354 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-stepfunctions-tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ const task = new tasks.CodeBuildStartBuild(stack, 'Task', {
value: sfn.JsonPath.stringAt('$.envVariables.zone'),
},
},
sourceVersion: sfn.JsonPath.stringAt('$.commitHash'),
});
```

Expand Down
244 changes: 244 additions & 0 deletions packages/@aws-cdk/aws-stepfunctions-tasks/lib/codebuild/start-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,187 @@ export interface CodeBuildStartBuildProps extends sfn.TaskStateBaseProps {
* CodeBuild project to start
*/
readonly project: codebuild.IProject;
/**
* Build output artifact settings that override, for this build only.
*
* @default - the latest ones already defined in the build project.
*/
readonly artifactsOverride?: codebuild.IArtifacts;
/**
* A buildspec file declaration that overrides, for this build only.
*
* @default - the latest one already defined in the build project.
*/
readonly buildspecOverride?: string;
/**
* Contains information that defines how the build project reports the build status to the source provider.
*
* @default - the latest one already defined in the build project only if the build's source is GitHub, GitHub Enterprise, or Bitbucket.
*/
readonly buildStatusConfigOverride?: codebuild.CfnProject.BuildStatusConfigProperty;
/**
* A ProjectCache object specified for this build.
*
* @default - the one defined in the build project.
*/
readonly cacheOverride?: codebuild.CfnProject.ProjectCacheProperty;
/**
* The name of a certificate for this build.
*
* @default - the one specified in the build project.
*/
readonly certificateOverride?: string;
/**
* The name of a compute type for this build
*
* @default - the one specified in the build project.
*/
readonly computeTypeOverride?: codebuild.ComputeType;
/**
* Specifies if session debugging is enabled for this build.
*
* @default - the one specified in the build project.
*/
readonly debugSessionEnabled?: boolean;
/**
* The AWS Key Management Service (AWS KMS) customer master key (CMK).
*
* @default - the one specified in the build project.
*/
readonly encryptionKeyOverride?: string;
/**
* A container type for this build.
*
* @default - the one specified in the build project.
*/
readonly environmentTypeOverride?: 'WINDOWS_CONTAINER' | 'LINUX_CONTAINER' | 'LINUX_GPU_CONTAINER'
| 'ARM_CONTAINER' | 'WINDOWS_SERVER_2019_CONTAINER';
/**
* A set of environment variables to be used for this build only.
*
* @default - the latest environment variables already defined in the build project.
*/
readonly environmentVariablesOverride?: { [name: string]: codebuild.BuildEnvironmentVariable };
/**
* The user-defined depth of history, with a minimum value of 0.
*
* @default - any previous depth of history defined in the build project.
*/
readonly gitCloneDepthOverride?: number;
/**
* Information about the Git submodules configuration for this build。
*
* @default - the one specified in the build project.
*/
readonly gitSubmodulesConfigOverride?: codebuild.CfnProject.GitSubmodulesConfigProperty;
/**
* A unique, case sensitive identifier you provide to ensure the idempotency of the StartBuild request.
*
* @default - no idempotency token.
*/
readonly idempotencyToken?: string;
/**
* The name of an image for this build.
*
* @default - the one specified in the build project.
*/
readonly imageOverride?: string;
/**
* The type of credentials AWS CodeBuild uses to pull images in your build.
*
* @default - the one specified in the build project.
*/
readonly imagePullCredentialsTypeOverride?: 'CODEBUILD' | 'SERVICE_ROLE';
/**
* Enable this flag to override the insecure SSL setting that is specified in the build project.
*
* @default - the one specified in the build project only if the build's source is GitHub Enterprise.
*/
readonly insecureSslOverride?: boolean;
/**
* Log settings for this build.
*
* @default - the one specified in the build project.
*/
readonly logsConfigOverride?: codebuild.CfnProject.LogsConfigProperty;
/**
* Enable this flag to override privileged mode in the build project.
*
* @default - the one specified in the build project.
*/
readonly privilegedModeOverride?: boolean;
/**
* The number of minutes a build is allowed to be queued before it times out.
*
* @default - the one specified in the build project.
*/
readonly queuedTimeoutInMinutesOverride?: number;
/**
* The credentials for access to a private registry.
*
* @default - the one specified in the build project.
*/
readonly registryCredentialOverride?: codebuild.CfnProject.RegistryCredentialProperty;
/**
* Set to true to report to your source provider the status of a build's start and completion.
*
* @default - the one specified in the build project only if the build's source is GitHub, GitHub Enterprise, or Bitbucket.
*/
readonly reportBuildStatusOverride?: boolean;
/**
* An array of ProjectArtifacts objects.
*
* @default - the one specified in the build project.
*/
readonly secondaryArtifactsOverride?: [codebuild.IArtifacts];
/**
* An array of ProjectSource objects.
*
* @default - the one specified in the build project.
*/
readonly secondarySourcesOverride?: [codebuild.Source];
/**
* An array of ProjectSourceVersion objects that specify one or more versions of the project's secondary sources to be used for this build only.
*
* @default - the one specified in the build project.
*/
readonly secondarySourcesVersionOverride?: [codebuild.CfnProject.ProjectSourceVersionProperty];
/**
* The name of a service role for this build.
*
* @default - the one specified in the build project.
*/
readonly serviceRoleOverride?: string;
/**
* An authorization type for this build.
*
* @default - the one specified in the build project.
*/
readonly sourceAuthOverride?: codebuild.CfnProject.SourceAuthProperty;
/**
* The source location for the one defined in the build project.
*
* @default - the one specified in the build project.
*/
readonly sourceLocationOverride?: string;
/**
* The source input defined in the build project.
*
* @default - the one specified in the build project.
*/
readonly sourceTypeOverride?: 'CODECOMMIT' | 'CODEPIPELINE' | 'GITHUB' | 'S3' | 'BITBUCKET' | 'GITHUB_ENTERPRISE' | 'NO_SOURCE';
/**
* The version of the build input to be built.
*
* @default - the one specified in the build project.
*/
readonly sourceVersion?: string;
/**
* The number of build timeout minutes, from 5 to 480 (8 hours).
*
* @default - the latest setting already defined in the build project.
*/
readonly timeoutInMinutesOverride?: number;
}

/**
Expand All @@ -41,6 +216,7 @@ export class CodeBuildStartBuild extends sfn.TaskStateBase {
this.integrationPattern = props.integrationPattern ?? sfn.IntegrationPattern.REQUEST_RESPONSE;

validatePatternSupported(this.integrationPattern, CodeBuildStartBuild.SUPPORTED_INTEGRATION_PATTERNS);
this.validateOverridingParameters(props);

this.taskMetrics = {
metricPrefixSingular: 'CodeBuildProject',
Expand Down Expand Up @@ -94,9 +270,45 @@ export class CodeBuildStartBuild extends sfn.TaskStateBase {
Resource: integrationResourceArn('codebuild', 'startBuild', this.integrationPattern),
Parameters: sfn.FieldUtils.renderObject({
ProjectName: this.props.project.projectName,
ArtifactsOverride: this.props.artifactsOverride ?
this.props.artifactsOverride.bind(this.props.project.stack, this.props.project).artifactsProperty : undefined,
BuildspecOverride: this.props.buildspecOverride,
BuildStatusConfigOverride: this.props.buildStatusConfigOverride,
CacheOverride: this.props.cacheOverride,
CertificateOverride: this.props.certificateOverride,
ComputeTypeOverride: this.props.computeTypeOverride,
DebugSessionEnabled: this.props.debugSessionEnabled,
EncryptionKeyOverride: this.props.encryptionKeyOverride,
EnvironmentTypeOverride: this.props.environmentTypeOverride,
EnvironmentVariablesOverride: this.props.environmentVariablesOverride
? this.serializeEnvVariables(this.props.environmentVariablesOverride)
: undefined,
GitCloneDepthOverride: this.props.gitCloneDepthOverride,
GitSubmodulesConfigOverride: this.props.gitSubmodulesConfigOverride,
IdempotencyToken: this.props.idempotencyToken,
ImageOverride: this.props.imageOverride,
ImagePullCredentialsTypeOverride: this.props.imagePullCredentialsTypeOverride,
InsecureSslOverride: this.props.insecureSslOverride,
LogsConfigOverride: this.props.logsConfigOverride,
PrivilegedModeOverride: this.props.privilegedModeOverride,
QueuedTimeoutInMinutesOverride: this.props.queuedTimeoutInMinutesOverride,
RegistryCredentialOverride: this.props.registryCredentialOverride,
ReportBuildStatusOverride: this.props.reportBuildStatusOverride,
SecondaryArtifactsOverride: this.props.secondaryArtifactsOverride ?
this.props.secondaryArtifactsOverride.map(artifact =>
artifact.bind(this.props.project.stack, this.props.project).artifactsProperty,
) : undefined,
SecondarySourcesOverride: this.props.secondarySourcesOverride ?
this.props.secondarySourcesOverride.map(source =>
source.bind(this.props.project.stack, this.props.project).sourceProperty,
) : undefined,
SecondarySourcesVersionOverride: this.props.secondarySourcesVersionOverride,
ServiceRoleOverride: this.props.serviceRoleOverride,
SourceAuthOverride: this.props.sourceAuthOverride,
SourceLocationOverride: this.props.sourceLocationOverride,
SourceTypeOverride: this.props.sourceTypeOverride,
SourceVersion: this.props.sourceVersion,
TimeoutInMinutesOverride: this.props.timeoutInMinutesOverride,
}),
};
}
Expand All @@ -108,4 +320,36 @@ export class CodeBuildStartBuild extends sfn.TaskStateBase {
Value: environmentVariables[name].value,
}));
}

private validateOverridingParameters(props: CodeBuildStartBuildProps) {
if (props.encryptionKeyOverride && props.encryptionKeyOverride.length < 1) {
throw new Error('The minimum length of property "encryptionKeyOverride" is 1.');
}
if (props.gitCloneDepthOverride && props.gitCloneDepthOverride < 0) {
throw new Error('The minimum value of property "gitCloneDepthOverride" is 0.');
}
if (props.imageOverride && props.imageOverride.length < 1) {
throw new Error('The minimum length of property "imageOverride" is 1.');
}
if (props.queuedTimeoutInMinutesOverride && (props.queuedTimeoutInMinutesOverride < 5
|| props.queuedTimeoutInMinutesOverride > 480)) {
throw new Error('The value of property "queuedTimeoutInMinutesOverride" must be between 5 and 480.');
}
if (props.secondaryArtifactsOverride && props.secondaryArtifactsOverride.length > 12) {
throw new Error('The maximum array length of property "secondaryArtifactsOverride" is 12.');
}
if (props.secondarySourcesOverride && props.secondarySourcesOverride.length > 12) {
throw new Error('The maximum array length of property "secondarySourcesOverride" is 12.');
}
if (props.secondarySourcesVersionOverride && props.secondarySourcesVersionOverride.length > 12) {
throw new Error('The maximum array length of property "secondarySourcesVersionOverride" is 12.');
}
if (props.serviceRoleOverride && props.serviceRoleOverride.length < 1) {
throw new Error('The minimum length of property "serviceRoleOverride" is 1.');
}
if (props.timeoutInMinutesOverride && (props.timeoutInMinutesOverride < 5
|| props.timeoutInMinutesOverride > 480)) {
throw new Error('The value of property "timeoutInMinutesOverride" must be between 5 and 480.');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
{
"Ref": "ProjectC78D97AD"
},
"\",\"EnvironmentVariablesOverride\":[{\"Name\":\"ZONE\",\"Type\":\"PLAINTEXT\",\"Value.$\":\"$.envVariables.zone\"}]}}}}"
"\",\"ComputeTypeOverride\":\"BUILD_GENERAL1_2XLARGE\",\"EnvironmentVariablesOverride\":[{\"Name\":\"ZONE\",\"Type\":\"PLAINTEXT\",\"Value.$\":\"$.envVariables.zone\"}],\"SourceVersion.$\":\"$.sourceCommit\"}}}}"
]
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class StartBuildStack extends cdk.Stack {
value: sfn.JsonPath.stringAt('$.envVariables.zone'),
},
},
sourceVersion: sfn.JsonPath.stringAt('$.sourceCommit'),
computeTypeOverride: codebuild.ComputeType.X2_LARGE,
});

const definition = new sfn.Pass(this, 'Start', {
Expand Down
Loading

0 comments on commit ff616b1

Please sign in to comment.