|
| 1 | +import * as codebuild from '@aws-cdk/aws-codebuild'; |
| 2 | +import * as iam from '@aws-cdk/aws-iam'; |
| 3 | +import * as sfn from '@aws-cdk/aws-stepfunctions'; |
| 4 | +import * as cdk from '@aws-cdk/core'; |
| 5 | +import { integrationResourceArn, validatePatternSupported } from '../private/task-utils'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Properties for CodeBuildStartBuild |
| 9 | + */ |
| 10 | +export interface CodeBuildStartBuildProps extends sfn.TaskStateBaseProps { |
| 11 | + /** |
| 12 | + * CodeBuild project to start |
| 13 | + */ |
| 14 | + readonly project: codebuild.IProject; |
| 15 | + /** |
| 16 | + * A set of environment variables to be used for this build only. |
| 17 | + * |
| 18 | + * @default - the latest environment variables already defined in the build project. |
| 19 | + */ |
| 20 | + readonly environmentVariablesOverride?: { [name: string]: codebuild.BuildEnvironmentVariable }; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Start a CodeBuild Build as a task |
| 25 | + * |
| 26 | + * @see https://docs.aws.amazon.com/step-functions/latest/dg/connect-codebuild.html |
| 27 | + */ |
| 28 | +export class CodeBuildStartBuild extends sfn.TaskStateBase { |
| 29 | + private static readonly SUPPORTED_INTEGRATION_PATTERNS: sfn.IntegrationPattern[] = [ |
| 30 | + sfn.IntegrationPattern.REQUEST_RESPONSE, |
| 31 | + sfn.IntegrationPattern.RUN_JOB, |
| 32 | + ]; |
| 33 | + |
| 34 | + protected readonly taskMetrics?: sfn.TaskMetricsConfig; |
| 35 | + protected readonly taskPolicies?: iam.PolicyStatement[]; |
| 36 | + |
| 37 | + private readonly integrationPattern: sfn.IntegrationPattern; |
| 38 | + |
| 39 | + constructor(scope: cdk.Construct, id: string, private readonly props: CodeBuildStartBuildProps) { |
| 40 | + super(scope, id, props); |
| 41 | + this.integrationPattern = props.integrationPattern ?? sfn.IntegrationPattern.REQUEST_RESPONSE; |
| 42 | + |
| 43 | + validatePatternSupported(this.integrationPattern, CodeBuildStartBuild.SUPPORTED_INTEGRATION_PATTERNS); |
| 44 | + |
| 45 | + this.taskMetrics = { |
| 46 | + metricPrefixSingular: 'CodeBuildProject', |
| 47 | + metricPrefixPlural: 'CodeBuildProjects', |
| 48 | + metricDimensions: { |
| 49 | + ProjectArn: this.props.project.projectArn, |
| 50 | + }, |
| 51 | + }; |
| 52 | + |
| 53 | + this.taskPolicies = this.configurePolicyStatements(); |
| 54 | + } |
| 55 | + |
| 56 | + private configurePolicyStatements(): iam.PolicyStatement[] { |
| 57 | + let policyStatements = [ |
| 58 | + new iam.PolicyStatement({ |
| 59 | + resources: [this.props.project.projectArn], |
| 60 | + actions: [ |
| 61 | + 'codebuild:StartBuild', |
| 62 | + 'codebuild:StopBuild', |
| 63 | + ], |
| 64 | + }), |
| 65 | + ]; |
| 66 | + |
| 67 | + if (this.integrationPattern === sfn.IntegrationPattern.RUN_JOB) { |
| 68 | + policyStatements.push( |
| 69 | + new iam.PolicyStatement({ |
| 70 | + actions: ['events:PutTargets', 'events:PutRule', 'events:DescribeRule'], |
| 71 | + resources: [ |
| 72 | + cdk.Stack.of(this).formatArn({ |
| 73 | + service: 'events', |
| 74 | + resource: 'rule/StepFunctionsGetEventForCodeBuildStartBuildRule', |
| 75 | + }), |
| 76 | + ], |
| 77 | + }), |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + return policyStatements; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Provides the CodeBuild StartBuild service integration task configuration |
| 86 | + */ |
| 87 | + /** |
| 88 | + * @internal |
| 89 | + */ |
| 90 | + protected _renderTask(): any { |
| 91 | + return { |
| 92 | + Resource: integrationResourceArn('codebuild', 'startBuild', this.integrationPattern), |
| 93 | + Parameters: sfn.FieldUtils.renderObject({ |
| 94 | + ProjectName: this.props.project.projectName, |
| 95 | + EnvironmentVariablesOverride: this.props.environmentVariablesOverride |
| 96 | + ? this.serializeEnvVariables(this.props.environmentVariablesOverride) |
| 97 | + : undefined, |
| 98 | + }), |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + private serializeEnvVariables(environmentVariables: { [name: string]: codebuild.BuildEnvironmentVariable }) { |
| 103 | + return Object.keys(environmentVariables).map(name => ({ |
| 104 | + Name: name, |
| 105 | + Type: environmentVariables[name].type || codebuild.BuildEnvironmentVariableType.PLAINTEXT, |
| 106 | + Value: environmentVariables[name].value, |
| 107 | + })); |
| 108 | + } |
| 109 | +} |
0 commit comments