Skip to content

Commit

Permalink
feat(codepipeline): add ability to override env variables in CodeBuil…
Browse files Browse the repository at this point in the history
…d actions (aws#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 aws#4531
  • Loading branch information
skinny85 authored Oct 23, 2019
1 parent b00c0af commit c0c0513
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
23 changes: 18 additions & 5 deletions packages/@aws-cdk/aws-codebuild/lib/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

/**
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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,
Expand Down

0 comments on commit c0c0513

Please sign in to comment.