From de8e670159065e1c1fe6d69a51c1596755dcbcc6 Mon Sep 17 00:00:00 2001 From: Katie Normandin <51719777+knorms101@users.noreply.github.com> Date: Thu, 2 Apr 2020 17:39:42 -0400 Subject: [PATCH] feat(aws-codebuild): add from codebuild image option (#7117) feat: add from codebuild image option Addresses comment [here](https://github.com/aws/aws-cdk/issues/2606#issuecomment-606114708). --- packages/@aws-cdk/aws-codebuild/README.md | 3 ++- packages/@aws-cdk/aws-codebuild/lib/project.ts | 14 ++++++++++++++ .../aws-codebuild/test/test.codebuild.ts | 17 +++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-codebuild/README.md b/packages/@aws-cdk/aws-codebuild/README.md index d3c5e533221c5..279c42eceed61 100644 --- a/packages/@aws-cdk/aws-codebuild/README.md +++ b/packages/@aws-cdk/aws-codebuild/README.md @@ -188,7 +188,7 @@ can use the `environment` property to customize the build environment: The CodeBuild library supports both Linux and Windows images via the `LinuxBuildImage` and `WindowsBuildImage` classes, respectively. -You can either specify one of the predefined Windows/Linux images by using one +You can specify one of the predefined Windows/Linux images by using one of the constants such as `WindowsBuildImage.WINDOWS_BASE_2_0` or `LinuxBuildImage.STANDARD_2_0`. @@ -200,6 +200,7 @@ Alternatively, you can specify a custom image using one of the static methods on ECR repository. * Use `.fromAsset(directory)` to use an image created from a local asset. +* Use `.fromCodeBuildImageId(id)` to reference a pre-defined, CodeBuild-provided Docker image. The following example shows how to define an image from a Docker asset: diff --git a/packages/@aws-cdk/aws-codebuild/lib/project.ts b/packages/@aws-cdk/aws-codebuild/lib/project.ts index e0efb49a6ee0c..34fee6f8bec57 100644 --- a/packages/@aws-cdk/aws-codebuild/lib/project.ts +++ b/packages/@aws-cdk/aws-codebuild/lib/project.ts @@ -1345,6 +1345,20 @@ export class LinuxBuildImage implements IBuildImage { }); } + /** + * Uses a Docker image provided by CodeBuild. + * + * @returns A Docker image provided by CodeBuild. + * + * @see https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-available.html + * + * @param id The image identifier + * @example 'aws/codebuild/standard:4.0' + */ + public static fromCodeBuildImageId(id: string): IBuildImage { + return LinuxBuildImage.codeBuildImage(id); + } + private static codeBuildImage(name: string): IBuildImage { return new LinuxBuildImage({ imageId: name, diff --git a/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts b/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts index 9127e324d83ed..b97b87b0d5fac 100644 --- a/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts +++ b/packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts @@ -1369,6 +1369,23 @@ export = { test.done(); }, + 'fromCodebuildImage'(test: Test) { + const stack = new cdk.Stack(); + new codebuild.PipelineProject(stack, 'Project', { + environment: { + buildImage: codebuild.LinuxBuildImage.fromCodeBuildImageId('aws/codebuild/standard:4.0') + }, + }); + + expect(stack).to(haveResourceLike('AWS::CodeBuild::Project', { + "Environment": { + "Image": "aws/codebuild/standard:4.0", + }, + })); + + test.done(); + }, + 'ARM image': { 'AMAZON_LINUX_2_ARM': { 'has type ARM_CONTAINER and default ComputeType LARGE'(test: Test) {