Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,20 @@ export class CodeBuildAction extends Action {
}
}

// Serialize environment variables early to trigger validation at construct time
const serializedEnvVars = this.props.environmentVariables
? codebuild.Project.serializeEnvVariables(
this.props.environmentVariables,
this.props.checkSecretsInPlainTextEnvVariables ?? true,
this.props.project)
: undefined;

const configuration: any = {
ProjectName: this.props.project.projectName,
EnvironmentVariables: this.props.environmentVariables &&
cdk.Stack.of(scope).toJsonString(codebuild.Project.serializeEnvVariables(this.props.environmentVariables,
this.props.checkSecretsInPlainTextEnvVariables ?? true, this.props.project)),
EnvironmentVariables: serializedEnvVars &&
cdk.Lazy.string({
produce: () => JSON.stringify(serializedEnvVars),
}),
};
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
@@ -1,10 +1,10 @@
import { Template } from '../../../assertions';
import { Match, Template } from '../../../assertions';
import * as codebuild from '../../../aws-codebuild';
import * as codecommit from '../../../aws-codecommit';
import * as codepipeline from '../../../aws-codepipeline';
import * as s3 from '../../../aws-s3';
import * as sns from '../../../aws-sns';
import { App, SecretValue, Stack } from '../../../core';
import { App, CfnParameter, SecretValue, Stack } from '../../../core';
import * as cpactions from '../../lib';

/* eslint-disable quote-props */
Expand Down Expand Up @@ -378,5 +378,76 @@ describe('CodeBuild Action', () => {
});
});
});

test('environment variables with tokens are correctly serialized', () => {
const stack = new Stack();

// Create a token-based environment variable using CfnParameter
const parameter = new CfnParameter(stack, 'MyParameter', {
type: 'String',
default: 'test-value',
});

const sourceOutput = new codepipeline.Artifact();
new codepipeline.Pipeline(stack, 'Pipeline', {
stages: [
{
stageName: 'Source',
actions: [
new cpactions.S3SourceAction({
actionName: 'S3_Source',
bucket: new s3.Bucket(stack, 'Bucket'),
bucketKey: 'key',
output: sourceOutput,
}),
],
},
{
stageName: 'Build',
actions: [
new cpactions.CodeBuildAction({
actionName: 'CodeBuild',
input: sourceOutput,
project: new codebuild.PipelineProject(stack, 'Project'),
environmentVariables: {
MY_VAR: {
value: parameter.valueAsString,
},
},
}),
],
},
],
});

// Verify the CloudFormation template structure
const template = Template.fromStack(stack);

// The EnvironmentVariables should be a Fn::Join that properly embeds the token reference
// This ensures tokens are resolved at deployment time without double-encoding
template.hasResourceProperties('AWS::CodePipeline::Pipeline', {
Stages: Match.arrayWith([
Match.objectLike({
Name: 'Build',
Actions: [
Match.objectLike({
Name: 'CodeBuild',
Configuration: Match.objectLike({
EnvironmentVariables: {
'Fn::Join': [
'',
Match.arrayWith([
Match.stringLikeRegexp('.*MY_VAR.*'),
{ Ref: 'MyParameter' },
]),
],
},
}),
}),
],
}),
]),
});
});
});
});
Loading