Skip to content

Commit c0c0513

Browse files
authored
feat(codepipeline): add ability to override env variables in CodeBuild actions (#4502)
Previously, environment variables were always defined on the CodeBuild project level, which made it difficult to re-use the same project in the pipeline. Now, you can specify environment variables on the CodeBuild action level, which will override any project-level settings. Fixes #4531
1 parent b00c0af commit c0c0513

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

packages/@aws-cdk/aws-codebuild/lib/project.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,23 @@ export class Project extends ProjectBase {
617617
return new Import(scope, id);
618618
}
619619

620+
/**
621+
* Convert the environment variables map of string to {@link BuildEnvironmentVariable},
622+
* which is the customer-facing type, to a list of {@link CfnProject.EnvironmentVariableProperty},
623+
* which is the representation of environment variables in CloudFormation.
624+
*
625+
* @param environmentVariables the map of string to environment variables
626+
* @returns an array of {@link CfnProject.EnvironmentVariableProperty} instances
627+
*/
628+
public static serializeEnvVariables(environmentVariables: { [name: string]: BuildEnvironmentVariable }):
629+
CfnProject.EnvironmentVariableProperty[] {
630+
return Object.keys(environmentVariables).map(name => ({
631+
name,
632+
type: environmentVariables[name].type || BuildEnvironmentVariableType.PLAINTEXT,
633+
value: environmentVariables[name].value,
634+
}));
635+
}
636+
620637
public readonly grantPrincipal: iam.IPrincipal;
621638

622639
/**
@@ -870,11 +887,7 @@ export class Project extends ProjectBase {
870887
: undefined,
871888
privilegedMode: env.privileged || false,
872889
computeType: env.computeType || this.buildImage.defaultComputeType,
873-
environmentVariables: !hasEnvironmentVars ? undefined : Object.keys(vars).map(name => ({
874-
name,
875-
type: vars[name].type || BuildEnvironmentVariableType.PLAINTEXT,
876-
value: vars[name].value
877-
}))
890+
environmentVariables: hasEnvironmentVars ? Project.serializeEnvVariables(vars) : undefined,
878891
};
879892
}
880893

packages/@aws-cdk/aws-codepipeline-actions/lib/codebuild/build-action.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ export interface CodeBuildActionProps extends codepipeline.CommonAwsActionProps
6060
* @default CodeBuildActionType.BUILD
6161
*/
6262
readonly type?: CodeBuildActionType;
63+
64+
/**
65+
* The environment variables to pass to the CodeBuild project when this action executes.
66+
* If a variable with the same name was set both on the project level, and here,
67+
* this value will take precedence.
68+
*
69+
* @default - No additional environment variables are specified.
70+
*/
71+
readonly environmentVariables?: { [name: string]: codebuild.BuildEnvironmentVariable };
6372
}
6473

6574
/**
@@ -125,6 +134,8 @@ export class CodeBuildAction extends Action {
125134

126135
const configuration: any = {
127136
ProjectName: this.props.project.projectName,
137+
EnvironmentVariables: this.props.environmentVariables &&
138+
cdk.Stack.of(scope).toJsonString(codebuild.Project.serializeEnvVariables(this.props.environmentVariables)),
128139
};
129140
if ((this.actionProperties.inputs || []).length > 1) {
130141
// lazy, because the Artifact name might be generated lazily

packages/@aws-cdk/aws-codepipeline-actions/test/integ.pipeline-code-commit-build.expected.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,8 @@
578578
"Configuration": {
579579
"ProjectName": {
580580
"Ref": "MyBuildProject30DB9D6E"
581-
}
581+
},
582+
"EnvironmentVariables": "[{\"name\":\"TEST_ENV_VARIABLE\",\"type\":\"PLAINTEXT\",\"value\":\"test env variable value\"},{\"name\":\"PARAM_STORE_VARIABLE\",\"type\":\"PARAMETER_STORE\",\"value\":\"param_store\"}]"
582583
},
583584
"InputArtifacts": [
584585
{

packages/@aws-cdk/aws-codepipeline-actions/test/integ.pipeline-code-commit-build.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import codepipeline = require('@aws-cdk/aws-codepipeline');
44
import cdk = require('@aws-cdk/core');
55
import cpactions = require('../lib');
66

7+
// tslint:disable:object-literal-key-quotes
8+
79
const app = new cdk.App();
810

911
const stack = new cdk.Stack(app, 'aws-cdk-codepipeline-codecommit-codebuild');
@@ -25,6 +27,15 @@ const buildAction = new cpactions.CodeBuildAction({
2527
project,
2628
input: sourceOutput,
2729
outputs: [new codepipeline.Artifact()],
30+
environmentVariables: {
31+
'TEST_ENV_VARIABLE': {
32+
value: 'test env variable value',
33+
},
34+
'PARAM_STORE_VARIABLE': {
35+
value: 'param_store',
36+
type: codebuild.BuildEnvironmentVariableType.PARAMETER_STORE,
37+
},
38+
},
2839
});
2940
const testAction = new cpactions.CodeBuildAction({
3041
type: cpactions.CodeBuildActionType.TEST,

0 commit comments

Comments
 (0)