diff --git a/packages/@aws-cdk/aws-codebuild/lib/cache.ts b/packages/@aws-cdk/aws-codebuild/lib/cache.ts index cf268615eba25..1d8e264649634 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/cache.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/cache.ts @@ -37,7 +37,13 @@ export enum LocalCacheMode { */ export abstract class Cache { public static none(): Cache { - return { _toCloudFormation: () => undefined, _bind: () => { return; } }; + return { + _toCloudFormation(): CfnProject.ProjectCacheProperty | undefined { + return { type: 'NO_CACHE' }; + }, + _bind(): void { + }, + }; } /** diff --git a/packages/@aws-cdk/aws-codebuild/lib/project.ts b/packages/@aws-cdk/aws-codebuild/lib/project.ts index c6a816e3aedd2..c91f88b227491 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/project.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/project.ts @@ -1040,10 +1040,10 @@ export class Project extends ProjectBase { : new NoArtifacts()); const artifactsConfig = artifacts.bind(this, this); - const cache = props.cache || Cache.none(); + const cache = props.cache; // give the caching strategy the option to grant permissions to any required resources - cache._bind(this); + cache?._bind(this); // Inject download commands for asset if requested const environmentVariables = props.environmentVariables || {}; @@ -1085,7 +1085,7 @@ export class Project extends ProjectBase { // empty will not remove existing encryptionKeys during an update (ref. t/D17810523) encryptionKey: Lazy.string({ produce: () => this._encryptionKey ? this._encryptionKey.keyArn : 'alias/aws/s3' }), badgeEnabled: props.badge, - cache: cache._toCloudFormation(), + cache: cache?._toCloudFormation(), name: this.physicalName, timeoutInMinutes: props.timeout && props.timeout.toMinutes(), queuedTimeoutInMinutes: props.queuedTimeout && props.queuedTimeout.toMinutes(), @@ -2125,4 +2125,4 @@ export enum ProjectNotificationEvents { function isBindableBuildImage(x: unknown): x is IBindableBuildImage { return typeof x === 'object' && !!x && !!(x as any).bind; -} \ No newline at end of file +} diff --git a/packages/@aws-cdk/aws-codebuild/test/project.test.ts b/packages/@aws-cdk/aws-codebuild/test/project.test.ts index 0041d1651ec9d..264a192becf84 100644 --- a/packages/@aws-cdk/aws-codebuild/test/project.test.ts +++ b/packages/@aws-cdk/aws-codebuild/test/project.test.ts @@ -1,4 +1,4 @@ -import { objectLike, ResourcePart, arrayWith } from '@aws-cdk/assert-internal'; +import { ABSENT, objectLike, ResourcePart, arrayWith } from '@aws-cdk/assert-internal'; import '@aws-cdk/assert-internal/jest'; import * as ec2 from '@aws-cdk/aws-ec2'; import * as iam from '@aws-cdk/aws-iam'; @@ -292,103 +292,123 @@ describe('BitBucket source', () => { }); }); -test('project with s3 cache bucket', () => { - // GIVEN - const stack = new cdk.Stack(); +describe('caching', () => { + test('using Cache.none() results in NO_CACHE in the template', () => { + // GIVEN + const stack = new cdk.Stack(); - // WHEN - new codebuild.Project(stack, 'Project', { - source: codebuild.Source.s3({ - bucket: new s3.Bucket(stack, 'SourceBucket'), - path: 'path', - }), - cache: codebuild.Cache.bucket(new s3.Bucket(stack, 'Bucket'), { - prefix: 'cache-prefix', - }), + // WHEN + new codebuild.PipelineProject(stack, 'Project', { + cache: codebuild.Cache.none(), + }); + + // THEN + expect(stack).toHaveResourceLike('AWS::CodeBuild::Project', { + Cache: { + Type: 'NO_CACHE', + Location: ABSENT, + }, + }); }); - // THEN - expect(stack).toHaveResourceLike('AWS::CodeBuild::Project', { - Cache: { - Type: 'S3', - Location: { - 'Fn::Join': [ - '/', - [ - { - 'Ref': 'Bucket83908E77', - }, - 'cache-prefix', + test('project with s3 cache bucket', () => { + // GIVEN + const stack = new cdk.Stack(); + + // WHEN + new codebuild.Project(stack, 'Project', { + source: codebuild.Source.s3({ + bucket: new s3.Bucket(stack, 'SourceBucket'), + path: 'path', + }), + cache: codebuild.Cache.bucket(new s3.Bucket(stack, 'Bucket'), { + prefix: 'cache-prefix', + }), + }); + + // THEN + expect(stack).toHaveResourceLike('AWS::CodeBuild::Project', { + Cache: { + Type: 'S3', + Location: { + 'Fn::Join': [ + '/', + [ + { + 'Ref': 'Bucket83908E77', + }, + 'cache-prefix', + ], ], - ], + }, }, - }, + }); }); -}); -test('s3 codebuild project with sourceVersion', () => { - // GIVEN - const stack = new cdk.Stack(); + test('s3 codebuild project with sourceVersion', () => { + // GIVEN + const stack = new cdk.Stack(); - // WHEN - new codebuild.Project(stack, 'Project', { - source: codebuild.Source.s3({ - bucket: new s3.Bucket(stack, 'Bucket'), - path: 'path', - version: 's3version', - }), - cache: codebuild.Cache.local(codebuild.LocalCacheMode.CUSTOM, codebuild.LocalCacheMode.DOCKER_LAYER, - codebuild.LocalCacheMode.SOURCE), - }); + // WHEN + new codebuild.Project(stack, 'Project', { + source: codebuild.Source.s3({ + bucket: new s3.Bucket(stack, 'Bucket'), + path: 'path', + version: 's3version', + }), + cache: codebuild.Cache.local(codebuild.LocalCacheMode.CUSTOM, codebuild.LocalCacheMode.DOCKER_LAYER, + codebuild.LocalCacheMode.SOURCE), + }); - // THEN - expect(stack).toHaveResource('AWS::CodeBuild::Project', { - SourceVersion: 's3version', + // THEN + expect(stack).toHaveResource('AWS::CodeBuild::Project', { + SourceVersion: 's3version', + }); }); -}); -test('project with local cache modes', () => { - // GIVEN - const stack = new cdk.Stack(); + test('project with local cache modes', () => { + // GIVEN + const stack = new cdk.Stack(); - // WHEN - new codebuild.Project(stack, 'Project', { - source: codebuild.Source.s3({ - bucket: new s3.Bucket(stack, 'Bucket'), - path: 'path', - }), - cache: codebuild.Cache.local(codebuild.LocalCacheMode.CUSTOM, codebuild.LocalCacheMode.DOCKER_LAYER, - codebuild.LocalCacheMode.SOURCE), - }); + // WHEN + new codebuild.Project(stack, 'Project', { + source: codebuild.Source.s3({ + bucket: new s3.Bucket(stack, 'Bucket'), + path: 'path', + }), + cache: codebuild.Cache.local(codebuild.LocalCacheMode.CUSTOM, codebuild.LocalCacheMode.DOCKER_LAYER, + codebuild.LocalCacheMode.SOURCE), + }); - // THEN - expect(stack).toHaveResourceLike('AWS::CodeBuild::Project', { - Cache: { - Type: 'LOCAL', - Modes: [ - 'LOCAL_CUSTOM_CACHE', - 'LOCAL_DOCKER_LAYER_CACHE', - 'LOCAL_SOURCE_CACHE', - ], - }, + // THEN + expect(stack).toHaveResourceLike('AWS::CodeBuild::Project', { + Cache: { + Type: 'LOCAL', + Modes: [ + 'LOCAL_CUSTOM_CACHE', + 'LOCAL_DOCKER_LAYER_CACHE', + 'LOCAL_SOURCE_CACHE', + ], + }, + }); }); -}); -test('project by default has no cache modes', () => { - // GIVEN - const stack = new cdk.Stack(); + test('project by default has no cache modes', () => { + // GIVEN + const stack = new cdk.Stack(); - // WHEN - new codebuild.Project(stack, 'Project', { - source: codebuild.Source.s3({ - bucket: new s3.Bucket(stack, 'Bucket'), - path: 'path', - }), - }); + // WHEN + new codebuild.Project(stack, 'Project', { + source: codebuild.Source.s3({ + bucket: new s3.Bucket(stack, 'Bucket'), + path: 'path', + }), + }); - // THEN - expect(stack).not.toHaveResourceLike('AWS::CodeBuild::Project', { - Cache: {}, + // THEN + expect(stack).not.toHaveResourceLike('AWS::CodeBuild::Project', { + Cache: {}, + }); }); });