-
Notifications
You must be signed in to change notification settings - Fork 4k
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(stepfunctions-tasks): add support for CodeBuild StartBuild API #9757
Merged
mergify
merged 10 commits into
aws:master
from
DaWyz:DaWyz/sfn-codebuild-start-build-task
Aug 21, 2020
+637
−0
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d15e4a4
feat(aws-stepfunctions-tasks): Add CodeBuild Start Build Task
47269ac
Add missing docstring to start-build.ts
DaWyz 31e5c1a
Fix missing exported error
DaWyz ac9b3c5
Fix build error (invalid ComputeType)
DaWyz b1ac5b7
Fixing build issue (GitPod working now)
a2d69e6
Improve integration test (add full list of parameters)
171f1ad
Reduce start-build scope
bdac4ea
Update README.md
d6430d6
Remove dependency on CfnProject and fix nits
ebdbdbd
Merge branch 'master' into DaWyz/sfn-codebuild-start-build-task
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
packages/@aws-cdk/aws-stepfunctions-tasks/lib/codebuild/start-build.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import * as codebuild from '@aws-cdk/aws-codebuild'; | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
import * as sfn from '@aws-cdk/aws-stepfunctions'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import { integrationResourceArn, validatePatternSupported } from '../private/task-utils'; | ||
|
||
/** | ||
* Properties for CodeBuildStartBuild | ||
*/ | ||
export interface CodeBuildStartBuildProps extends sfn.TaskStateBaseProps { | ||
/** | ||
* CodeBuild project to start | ||
*/ | ||
readonly project: codebuild.IProject; | ||
/** | ||
* 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 }; | ||
} | ||
|
||
/** | ||
* Start a CodeBuild Build as a task | ||
* | ||
* @see https://docs.aws.amazon.com/step-functions/latest/dg/connect-codebuild.html | ||
*/ | ||
export class CodeBuildStartBuild extends sfn.TaskStateBase { | ||
private static readonly SUPPORTED_INTEGRATION_PATTERNS: sfn.IntegrationPattern[] = [ | ||
sfn.IntegrationPattern.REQUEST_RESPONSE, | ||
sfn.IntegrationPattern.RUN_JOB, | ||
]; | ||
|
||
protected readonly taskMetrics?: sfn.TaskMetricsConfig; | ||
protected readonly taskPolicies?: iam.PolicyStatement[]; | ||
|
||
private readonly integrationPattern: sfn.IntegrationPattern; | ||
|
||
constructor(scope: cdk.Construct, id: string, private readonly props: CodeBuildStartBuildProps) { | ||
super(scope, id, props); | ||
this.integrationPattern = props.integrationPattern ?? sfn.IntegrationPattern.REQUEST_RESPONSE; | ||
|
||
validatePatternSupported(this.integrationPattern, CodeBuildStartBuild.SUPPORTED_INTEGRATION_PATTERNS); | ||
|
||
this.taskMetrics = { | ||
metricPrefixSingular: 'CodeBuildProject', | ||
metricPrefixPlural: 'CodeBuildProjects', | ||
metricDimensions: { | ||
ProjectArn: this.props.project.projectArn, | ||
}, | ||
}; | ||
|
||
this.taskPolicies = this.configurePolicyStatements(); | ||
} | ||
|
||
private configurePolicyStatements(): iam.PolicyStatement[] { | ||
let policyStatements = [ | ||
new iam.PolicyStatement({ | ||
resources: [this.props.project.projectArn], | ||
actions: [ | ||
'codebuild:StartBuild', | ||
'codebuild:StopBuild', | ||
], | ||
}), | ||
]; | ||
|
||
if (this.integrationPattern === sfn.IntegrationPattern.RUN_JOB) { | ||
policyStatements.push( | ||
new iam.PolicyStatement({ | ||
actions: ['events:PutTargets', 'events:PutRule', 'events:DescribeRule'], | ||
resources: [ | ||
cdk.Stack.of(this).formatArn({ | ||
service: 'events', | ||
resource: 'rule/StepFunctionsGetEventForCodeBuildStartBuildRule', | ||
}), | ||
], | ||
}), | ||
); | ||
} | ||
|
||
return policyStatements; | ||
} | ||
|
||
/** | ||
* Provides the CodeBuild StartBuild service integration task configuration | ||
*/ | ||
/** | ||
* @internal | ||
*/ | ||
protected _renderTask(): any { | ||
DaWyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return { | ||
Resource: integrationResourceArn('codebuild', 'startBuild', this.integrationPattern), | ||
Parameters: sfn.FieldUtils.renderObject({ | ||
ProjectName: this.props.project.projectName, | ||
EnvironmentVariablesOverride: this.props.environmentVariablesOverride | ||
? this.serializeEnvVariables(this.props.environmentVariablesOverride) | ||
: undefined, | ||
}), | ||
}; | ||
} | ||
|
||
private serializeEnvVariables(environmentVariables: { [name: string]: codebuild.BuildEnvironmentVariable }) { | ||
return Object.keys(environmentVariables).map(name => ({ | ||
Name: name, | ||
Type: environmentVariables[name].type || codebuild.BuildEnvironmentVariableType.PLAINTEXT, | ||
Value: environmentVariables[name].value, | ||
})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this file feels really large, can we break it down?
would any of this apply for
StopBuild
- if so we should start moving shared types out.take a look at the
EcsRunTask
service integration for inspiration on how to break this down