Skip to content

Commit cd51a5d

Browse files
authored
fix(codebuild): setting Cache.none() renders nothing in the template (#18194)
When implementing caching in CodeBuild, we made the default cache `Cache.none()`, and, for that reason, do not render anything in the template for that type of cache. However, that does not work well with the CodeBuild API, which interprets the lack of a property as the signal to leave it unchanged. Which means it's not possible currently to disable caching on a Project once it has been enabled once. Fix this by differentiating between the case of "no Cache has been provided", and "the none() Cache has been provided". Closes #18165 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 884d3a8 commit cd51a5d

33 files changed

+262
-83
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ export enum LocalCacheMode {
3737
*/
3838
export abstract class Cache {
3939
public static none(): Cache {
40-
return { _toCloudFormation: () => undefined, _bind: () => { return; } };
40+
return {
41+
_toCloudFormation(): CfnProject.ProjectCacheProperty | undefined {
42+
return { type: 'NO_CACHE' };
43+
},
44+
_bind(): void {
45+
},
46+
};
4147
}
4248

4349
/**

packages/@aws-cdk/aws-codebuild/test/codebuild.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ describe('default properties', () => {
155155
'ComputeType': 'BUILD_GENERAL1_SMALL',
156156
},
157157
'EncryptionKey': 'alias/aws/s3',
158+
'Cache': {
159+
'Type': 'NO_CACHE',
160+
},
158161
},
159162
},
160163
},
@@ -335,6 +338,9 @@ describe('default properties', () => {
335338
'Type': 'CODECOMMIT',
336339
},
337340
'EncryptionKey': 'alias/aws/s3',
341+
'Cache': {
342+
'Type': 'NO_CACHE',
343+
},
338344
},
339345
},
340346
},
@@ -539,6 +545,9 @@ describe('default properties', () => {
539545
'Type': 'S3',
540546
},
541547
'EncryptionKey': 'alias/aws/s3',
548+
'Cache': {
549+
'Type': 'NO_CACHE',
550+
},
542551
},
543552
},
544553
},

packages/@aws-cdk/aws-codebuild/test/integ.aws-deep-learning-container-build-image.expected.json

+3
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@
210210
"BuildSpec": "{\n \"version\": \"0.2\",\n \"phases\": {\n \"build\": {\n \"commands\": [\n \"ls\"\n ]\n }\n }\n}",
211211
"Type": "NO_SOURCE"
212212
},
213+
"Cache": {
214+
"Type": "NO_CACHE"
215+
},
213216
"EncryptionKey": "alias/aws/s3"
214217
}
215218
}

packages/@aws-cdk/aws-codebuild/test/integ.defaults.lit.expected.json

+3
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@
147147
"BuildSpec": "{\n \"version\": \"0.2\",\n \"phases\": {\n \"build\": {\n \"commands\": [\n \"echo \\\"Hello, CodeBuild!\\\"\"\n ]\n }\n }\n}",
148148
"Type": "NO_SOURCE"
149149
},
150+
"Cache": {
151+
"Type": "NO_CACHE"
152+
},
150153
"EncryptionKey": "alias/aws/s3"
151154
}
152155
}

packages/@aws-cdk/aws-codebuild/test/integ.docker-asset.lit.expected.json

+3
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@
164164
"BuildSpec": "{\n \"version\": \"0.2\",\n \"phases\": {\n \"build\": {\n \"commands\": [\n \"ls\"\n ]\n }\n }\n}",
165165
"Type": "NO_SOURCE"
166166
},
167+
"Cache": {
168+
"Type": "NO_CACHE"
169+
},
167170
"EncryptionKey": "alias/aws/s3"
168171
}
169172
}

packages/@aws-cdk/aws-codebuild/test/integ.docker-registry.lit.expected.json

+3
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@
155155
"BuildSpec": "{\n \"version\": \"0.2\",\n \"phases\": {\n \"build\": {\n \"commands\": [\n \"ls\"\n ]\n }\n }\n}",
156156
"Type": "NO_SOURCE"
157157
},
158+
"Cache": {
159+
"Type": "NO_CACHE"
160+
},
158161
"EncryptionKey": "alias/aws/s3"
159162
}
160163
}

packages/@aws-cdk/aws-codebuild/test/integ.ecr.lit.expected.json

+3
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@
185185
"BuildSpec": "{\n \"version\": \"0.2\",\n \"phases\": {\n \"build\": {\n \"commands\": [\n \"ls\"\n ]\n }\n }\n}",
186186
"Type": "NO_SOURCE"
187187
},
188+
"Cache": {
189+
"Type": "NO_CACHE"
190+
},
188191
"EncryptionKey": "alias/aws/s3"
189192
}
190193
}

packages/@aws-cdk/aws-codebuild/test/integ.github-webhook-batch.expected.json

+3
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@
169169
]
170170
}
171171
},
172+
"Cache": {
173+
"Type": "NO_CACHE"
174+
},
172175
"EncryptionKey": "alias/aws/s3",
173176
"Triggers": {
174177
"BuildType": "BUILD_BATCH",

packages/@aws-cdk/aws-codebuild/test/integ.github.expected.json

+3
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@
114114
"ReportBuildStatus": false,
115115
"Type": "GITHUB"
116116
},
117+
"Cache": {
118+
"Type": "NO_CACHE"
119+
},
117120
"EncryptionKey": "alias/aws/s3"
118121
}
119122
}

packages/@aws-cdk/aws-codebuild/test/integ.project-bucket.expected.json

+3
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@
158158
},
159159
"Type": "S3"
160160
},
161+
"Cache": {
162+
"Type": "NO_CACHE"
163+
},
161164
"EncryptionKey": "alias/aws/s3"
162165
}
163166
}

packages/@aws-cdk/aws-codebuild/test/integ.project-buildspec-artifacts.expected.json

+3
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@
159159
"BuildSpec": "{\n \"version\": \"0.2\"\n}",
160160
"Type": "NO_SOURCE"
161161
},
162+
"Cache": {
163+
"Type": "NO_CACHE"
164+
},
162165
"EncryptionKey": "alias/aws/s3"
163166
}
164167
}

packages/@aws-cdk/aws-codebuild/test/integ.project-file-system-location.expected.json

+3
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,9 @@
380380
"BuildSpec": "{\n \"version\": \"0.2\"\n}",
381381
"Type": "NO_SOURCE"
382382
},
383+
"Cache": {
384+
"Type": "NO_CACHE"
385+
},
383386
"EncryptionKey": "alias/aws/s3",
384387
"FileSystemLocations": [
385388
{

packages/@aws-cdk/aws-codebuild/test/integ.project-logging.expected.json

+3
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@
202202
"Source": {
203203
"Type": "CODEPIPELINE"
204204
},
205+
"Cache": {
206+
"Type": "NO_CACHE"
207+
},
205208
"EncryptionKey": "alias/aws/s3",
206209
"LogsConfig": {
207210
"CloudWatchLogs": {

packages/@aws-cdk/aws-codebuild/test/integ.project-notification.expected.json

+3
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@
147147
"BuildSpec": "{\n \"version\": \"0.2\",\n \"phases\": {\n \"build\": {\n \"commands\": [\n \"echo \\\"Nothing to do!\\\"\"\n ]\n }\n }\n}",
148148
"Type": "NO_SOURCE"
149149
},
150+
"Cache": {
151+
"Type": "NO_CACHE"
152+
},
150153
"EncryptionKey": "alias/aws/s3"
151154
}
152155
},

packages/@aws-cdk/aws-codebuild/test/integ.project-secondary-sources-artifacts.expected.json

+3
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@
181181
"BuildSpec": "{\n \"version\": \"0.2\"\n}",
182182
"Type": "NO_SOURCE"
183183
},
184+
"Cache": {
185+
"Type": "NO_CACHE"
186+
},
184187
"EncryptionKey": "alias/aws/s3",
185188
"SecondaryArtifacts": [
186189
{

packages/@aws-cdk/aws-codebuild/test/integ.project-vpc.expected.json

+3
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,9 @@
380380
"BuildSpec": "{\n \"version\": \"0.2\",\n \"phases\": {\n \"build\": {\n \"commands\": [\n \"echo \\\"Nothing to do!\\\"\"\n ]\n }\n }\n}",
381381
"Type": "NO_SOURCE"
382382
},
383+
"Cache": {
384+
"Type": "NO_CACHE"
385+
},
383386
"EncryptionKey": "alias/aws/s3",
384387
"VpcConfig": {
385388
"SecurityGroupIds": [

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

+104-81
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { objectLike, ResourcePart, arrayWith } from '@aws-cdk/assert-internal';
1+
import { ABSENT, objectLike, ResourcePart, arrayWith } from '@aws-cdk/assert-internal';
22
import '@aws-cdk/assert-internal/jest';
33
import * as ec2 from '@aws-cdk/aws-ec2';
44
import * as iam from '@aws-cdk/aws-iam';
@@ -292,103 +292,126 @@ describe('BitBucket source', () => {
292292
});
293293
});
294294

295-
test('project with s3 cache bucket', () => {
296-
// GIVEN
297-
const stack = new cdk.Stack();
295+
describe('caching', () => {
296+
test('using Cache.none() results in NO_CACHE in the template', () => {
297+
// GIVEN
298+
const stack = new cdk.Stack();
298299

299-
// WHEN
300-
new codebuild.Project(stack, 'Project', {
301-
source: codebuild.Source.s3({
302-
bucket: new s3.Bucket(stack, 'SourceBucket'),
303-
path: 'path',
304-
}),
305-
cache: codebuild.Cache.bucket(new s3.Bucket(stack, 'Bucket'), {
306-
prefix: 'cache-prefix',
307-
}),
300+
// WHEN
301+
new codebuild.PipelineProject(stack, 'Project', {
302+
cache: codebuild.Cache.none(),
303+
});
304+
305+
// THEN
306+
expect(stack).toHaveResourceLike('AWS::CodeBuild::Project', {
307+
Cache: {
308+
Type: 'NO_CACHE',
309+
Location: ABSENT,
310+
},
311+
});
308312
});
309313

310-
// THEN
311-
expect(stack).toHaveResourceLike('AWS::CodeBuild::Project', {
312-
Cache: {
313-
Type: 'S3',
314-
Location: {
315-
'Fn::Join': [
316-
'/',
317-
[
318-
{
319-
'Ref': 'Bucket83908E77',
320-
},
321-
'cache-prefix',
314+
test('project with s3 cache bucket', () => {
315+
// GIVEN
316+
const stack = new cdk.Stack();
317+
318+
// WHEN
319+
new codebuild.Project(stack, 'Project', {
320+
source: codebuild.Source.s3({
321+
bucket: new s3.Bucket(stack, 'SourceBucket'),
322+
path: 'path',
323+
}),
324+
cache: codebuild.Cache.bucket(new s3.Bucket(stack, 'Bucket'), {
325+
prefix: 'cache-prefix',
326+
}),
327+
});
328+
329+
// THEN
330+
expect(stack).toHaveResourceLike('AWS::CodeBuild::Project', {
331+
Cache: {
332+
Type: 'S3',
333+
Location: {
334+
'Fn::Join': [
335+
'/',
336+
[
337+
{
338+
'Ref': 'Bucket83908E77',
339+
},
340+
'cache-prefix',
341+
],
322342
],
323-
],
343+
},
324344
},
325-
},
345+
});
326346
});
327-
});
328347

329-
test('s3 codebuild project with sourceVersion', () => {
330-
// GIVEN
331-
const stack = new cdk.Stack();
348+
test('s3 codebuild project with sourceVersion', () => {
349+
// GIVEN
350+
const stack = new cdk.Stack();
332351

333-
// WHEN
334-
new codebuild.Project(stack, 'Project', {
335-
source: codebuild.Source.s3({
336-
bucket: new s3.Bucket(stack, 'Bucket'),
337-
path: 'path',
338-
version: 's3version',
339-
}),
340-
cache: codebuild.Cache.local(codebuild.LocalCacheMode.CUSTOM, codebuild.LocalCacheMode.DOCKER_LAYER,
341-
codebuild.LocalCacheMode.SOURCE),
342-
});
352+
// WHEN
353+
new codebuild.Project(stack, 'Project', {
354+
source: codebuild.Source.s3({
355+
bucket: new s3.Bucket(stack, 'Bucket'),
356+
path: 'path',
357+
version: 's3version',
358+
}),
359+
cache: codebuild.Cache.local(codebuild.LocalCacheMode.CUSTOM, codebuild.LocalCacheMode.DOCKER_LAYER,
360+
codebuild.LocalCacheMode.SOURCE),
361+
});
343362

344-
// THEN
345-
expect(stack).toHaveResource('AWS::CodeBuild::Project', {
346-
SourceVersion: 's3version',
363+
// THEN
364+
expect(stack).toHaveResource('AWS::CodeBuild::Project', {
365+
SourceVersion: 's3version',
366+
});
347367
});
348-
});
349368

350-
test('project with local cache modes', () => {
351-
// GIVEN
352-
const stack = new cdk.Stack();
369+
test('project with local cache modes', () => {
370+
// GIVEN
371+
const stack = new cdk.Stack();
353372

354-
// WHEN
355-
new codebuild.Project(stack, 'Project', {
356-
source: codebuild.Source.s3({
357-
bucket: new s3.Bucket(stack, 'Bucket'),
358-
path: 'path',
359-
}),
360-
cache: codebuild.Cache.local(codebuild.LocalCacheMode.CUSTOM, codebuild.LocalCacheMode.DOCKER_LAYER,
361-
codebuild.LocalCacheMode.SOURCE),
362-
});
373+
// WHEN
374+
new codebuild.Project(stack, 'Project', {
375+
source: codebuild.Source.s3({
376+
bucket: new s3.Bucket(stack, 'Bucket'),
377+
path: 'path',
378+
}),
379+
cache: codebuild.Cache.local(codebuild.LocalCacheMode.CUSTOM, codebuild.LocalCacheMode.DOCKER_LAYER,
380+
codebuild.LocalCacheMode.SOURCE),
381+
});
363382

364-
// THEN
365-
expect(stack).toHaveResourceLike('AWS::CodeBuild::Project', {
366-
Cache: {
367-
Type: 'LOCAL',
368-
Modes: [
369-
'LOCAL_CUSTOM_CACHE',
370-
'LOCAL_DOCKER_LAYER_CACHE',
371-
'LOCAL_SOURCE_CACHE',
372-
],
373-
},
383+
// THEN
384+
expect(stack).toHaveResourceLike('AWS::CodeBuild::Project', {
385+
Cache: {
386+
Type: 'LOCAL',
387+
Modes: [
388+
'LOCAL_CUSTOM_CACHE',
389+
'LOCAL_DOCKER_LAYER_CACHE',
390+
'LOCAL_SOURCE_CACHE',
391+
],
392+
},
393+
});
374394
});
375-
});
376395

377-
test('project by default has no cache modes', () => {
378-
// GIVEN
379-
const stack = new cdk.Stack();
396+
test('project by default has cache type set to NO_CACHE', () => {
397+
// GIVEN
398+
const stack = new cdk.Stack();
380399

381-
// WHEN
382-
new codebuild.Project(stack, 'Project', {
383-
source: codebuild.Source.s3({
384-
bucket: new s3.Bucket(stack, 'Bucket'),
385-
path: 'path',
386-
}),
387-
});
400+
// WHEN
401+
new codebuild.Project(stack, 'Project', {
402+
source: codebuild.Source.s3({
403+
bucket: new s3.Bucket(stack, 'Bucket'),
404+
path: 'path',
405+
}),
406+
});
388407

389-
// THEN
390-
expect(stack).not.toHaveResourceLike('AWS::CodeBuild::Project', {
391-
Cache: {},
408+
// THEN
409+
expect(stack).toHaveResourceLike('AWS::CodeBuild::Project', {
410+
Cache: {
411+
Type: 'NO_CACHE',
412+
Location: ABSENT,
413+
},
414+
});
392415
});
393416
});
394417

0 commit comments

Comments
 (0)