diff --git a/packages/@aws-cdk/aws-codebuild/lib/project.ts b/packages/@aws-cdk/aws-codebuild/lib/project.ts index d42c0bd6055df..b7e277481ed1f 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/project.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/project.ts @@ -617,6 +617,23 @@ export class Project extends ProjectBase { return new Import(scope, id); } + /** + * Convert the environment variables map of string to {@link BuildEnvironmentVariable}, + * which is the customer-facing type, to a list of {@link CfnProject.EnvironmentVariableProperty}, + * which is the representation of environment variables in CloudFormation. + * + * @param environmentVariables the map of string to environment variables + * @returns an array of {@link CfnProject.EnvironmentVariableProperty} instances + */ + public static serializeEnvVariables(environmentVariables: { [name: string]: BuildEnvironmentVariable }): + CfnProject.EnvironmentVariableProperty[] { + return Object.keys(environmentVariables).map(name => ({ + name, + type: environmentVariables[name].type || BuildEnvironmentVariableType.PLAINTEXT, + value: environmentVariables[name].value, + })); + } + public readonly grantPrincipal: iam.IPrincipal; /** @@ -870,11 +887,7 @@ export class Project extends ProjectBase { : undefined, privilegedMode: env.privileged || false, computeType: env.computeType || this.buildImage.defaultComputeType, - environmentVariables: !hasEnvironmentVars ? undefined : Object.keys(vars).map(name => ({ - name, - type: vars[name].type || BuildEnvironmentVariableType.PLAINTEXT, - value: vars[name].value - })) + environmentVariables: hasEnvironmentVars ? Project.serializeEnvVariables(vars) : undefined, }; } diff --git a/packages/@aws-cdk/aws-codepipeline-actions/lib/codebuild/build-action.ts b/packages/@aws-cdk/aws-codepipeline-actions/lib/codebuild/build-action.ts index 09bfcc85489d3..b6c1e826d620d 100644 --- a/packages/@aws-cdk/aws-codepipeline-actions/lib/codebuild/build-action.ts +++ b/packages/@aws-cdk/aws-codepipeline-actions/lib/codebuild/build-action.ts @@ -60,6 +60,15 @@ export interface CodeBuildActionProps extends codepipeline.CommonAwsActionProps * @default CodeBuildActionType.BUILD */ readonly type?: CodeBuildActionType; + + /** + * The environment variables to pass to the CodeBuild project when this action executes. + * If a variable with the same name was set both on the project level, and here, + * this value will take precedence. + * + * @default - No additional environment variables are specified. + */ + readonly environmentVariables?: { [name: string]: codebuild.BuildEnvironmentVariable }; } /** @@ -125,6 +134,8 @@ export class CodeBuildAction extends Action { const configuration: any = { ProjectName: this.props.project.projectName, + EnvironmentVariables: this.props.environmentVariables && + cdk.Stack.of(scope).toJsonString(codebuild.Project.serializeEnvVariables(this.props.environmentVariables)), }; if ((this.actionProperties.inputs || []).length > 1) { // lazy, because the Artifact name might be generated lazily diff --git a/packages/@aws-cdk/aws-codepipeline-actions/test/integ.pipeline-code-commit-build.expected.json b/packages/@aws-cdk/aws-codepipeline-actions/test/integ.pipeline-code-commit-build.expected.json index 0e844eaed2a35..f6d7ff99a57d0 100644 --- a/packages/@aws-cdk/aws-codepipeline-actions/test/integ.pipeline-code-commit-build.expected.json +++ b/packages/@aws-cdk/aws-codepipeline-actions/test/integ.pipeline-code-commit-build.expected.json @@ -578,7 +578,8 @@ "Configuration": { "ProjectName": { "Ref": "MyBuildProject30DB9D6E" - } + }, + "EnvironmentVariables": "[{\"name\":\"TEST_ENV_VARIABLE\",\"type\":\"PLAINTEXT\",\"value\":\"test env variable value\"},{\"name\":\"PARAM_STORE_VARIABLE\",\"type\":\"PARAMETER_STORE\",\"value\":\"param_store\"}]" }, "InputArtifacts": [ { diff --git a/packages/@aws-cdk/aws-codepipeline-actions/test/integ.pipeline-code-commit-build.ts b/packages/@aws-cdk/aws-codepipeline-actions/test/integ.pipeline-code-commit-build.ts index fc1e812e4a976..0a02b4cc66e29 100644 --- a/packages/@aws-cdk/aws-codepipeline-actions/test/integ.pipeline-code-commit-build.ts +++ b/packages/@aws-cdk/aws-codepipeline-actions/test/integ.pipeline-code-commit-build.ts @@ -4,6 +4,8 @@ import codepipeline = require('@aws-cdk/aws-codepipeline'); import cdk = require('@aws-cdk/core'); import cpactions = require('../lib'); +// tslint:disable:object-literal-key-quotes + const app = new cdk.App(); const stack = new cdk.Stack(app, 'aws-cdk-codepipeline-codecommit-codebuild'); @@ -25,6 +27,15 @@ const buildAction = new cpactions.CodeBuildAction({ project, input: sourceOutput, outputs: [new codepipeline.Artifact()], + environmentVariables: { + 'TEST_ENV_VARIABLE': { + value: 'test env variable value', + }, + 'PARAM_STORE_VARIABLE': { + value: 'param_store', + type: codebuild.BuildEnvironmentVariableType.PARAMETER_STORE, + }, + }, }); const testAction = new cpactions.CodeBuildAction({ type: cpactions.CodeBuildActionType.TEST,