Skip to content

Commit

Permalink
fix(codebuild): ARM images have the wrong type and compute kind (#5541)
Browse files Browse the repository at this point in the history
As it turns out, ARM images require specifying a different type than Linux images.
They also only work with ComputeType.LARGE.
Add a new, module-private, class of IBuildImage, ArmImage,
that implements that behavior and validation,
and change LinuxBuildImage.AMAZON_LINUX_2_ARM to use the new class.

Fixes #5517

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
skinny85 and mergify[bot] committed Dec 31, 2019
1 parent 9cbbaea commit 6999baf
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 26 deletions.
84 changes: 58 additions & 26 deletions packages/@aws-cdk/aws-codebuild/lib/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,31 @@ export interface IBuildImage {
runScriptBuildspec(entrypoint: string): BuildSpec;
}

class ArmBuildImage implements IBuildImage {
public readonly type = 'ARM_CONTAINER';
public readonly defaultComputeType = ComputeType.LARGE;
public readonly imagePullPrincipalType = ImagePullPrincipalType.CODEBUILD;
public readonly imageId: string;

constructor(imageId: string) {
this.imageId = imageId;
}

public validate(buildEnvironment: BuildEnvironment): string[] {
const ret = [];
if (buildEnvironment.computeType &&
buildEnvironment.computeType !== ComputeType.LARGE) {
ret.push(`ARM images only support ComputeType '${ComputeType.LARGE}' - ` +
`'${buildEnvironment.computeType}' was given`);
}
return ret;
}

public runScriptBuildspec(entrypoint: string): BuildSpec {
return runScriptLinuxBuildSpec(entrypoint);
}
}

/**
* The options when creating a CodeBuild Docker build image
* using {@link LinuxBuildImage.fromDockerRegistry}
Expand Down Expand Up @@ -1157,9 +1182,12 @@ export class LinuxBuildImage implements IBuildImage {
public static readonly STANDARD_1_0 = LinuxBuildImage.codeBuildImage('aws/codebuild/standard:1.0');
public static readonly STANDARD_2_0 = LinuxBuildImage.codeBuildImage('aws/codebuild/standard:2.0');
public static readonly STANDARD_3_0 = LinuxBuildImage.codeBuildImage('aws/codebuild/standard:3.0');

public static readonly AMAZON_LINUX_2 = LinuxBuildImage.codeBuildImage('aws/codebuild/amazonlinux2-x86_64-standard:1.0');
public static readonly AMAZON_LINUX_2_2 = LinuxBuildImage.codeBuildImage('aws/codebuild/amazonlinux2-x86_64-standard:2.0');
public static readonly AMAZON_LINUX_2_ARM = LinuxBuildImage.codeBuildImage('aws/codebuild/amazonlinux2-aarch64-standard:1.0');

public static readonly AMAZON_LINUX_2_ARM: IBuildImage = new ArmBuildImage('aws/codebuild/amazonlinux2-aarch64-standard:1.0');

/** @deprecated Use {@link STANDARD_2_0} and specify runtime in buildspec runtime-versions section */
public static readonly UBUNTU_14_04_BASE = LinuxBuildImage.codeBuildImage('aws/codebuild/ubuntu-base:14.04');
/** @deprecated Use {@link STANDARD_2_0} and specify runtime in buildspec runtime-versions section */
Expand Down Expand Up @@ -1289,34 +1317,38 @@ export class LinuxBuildImage implements IBuildImage {
}

public runScriptBuildspec(entrypoint: string): BuildSpec {
return BuildSpec.fromObject({
version: '0.2',
phases: {
pre_build: {
commands: [
// Better echo the location here; if this fails, the error message only contains
// the unexpanded variables by default. It might fail if you're running an old
// definition of the CodeBuild project--the permissions will have been changed
// to only allow downloading the very latest version.
`echo "Downloading scripts from s3://\${${S3_BUCKET_ENV}}/\${${S3_KEY_ENV}}"`,
`aws s3 cp s3://\${${S3_BUCKET_ENV}}/\${${S3_KEY_ENV}} /tmp`,
`mkdir -p /tmp/scriptdir`,
`unzip /tmp/$(basename \$${S3_KEY_ENV}) -d /tmp/scriptdir`,
]
},
build: {
commands: [
'export SCRIPT_DIR=/tmp/scriptdir',
`echo "Running ${entrypoint}"`,
`chmod +x /tmp/scriptdir/${entrypoint}`,
`/tmp/scriptdir/${entrypoint}`,
]
}
}
});
return runScriptLinuxBuildSpec(entrypoint);
}
}

function runScriptLinuxBuildSpec(entrypoint: string) {
return BuildSpec.fromObject({
version: '0.2',
phases: {
pre_build: {
commands: [
// Better echo the location here; if this fails, the error message only contains
// the unexpanded variables by default. It might fail if you're running an old
// definition of the CodeBuild project--the permissions will have been changed
// to only allow downloading the very latest version.
`echo "Downloading scripts from s3://\${${S3_BUCKET_ENV}}/\${${S3_KEY_ENV}}"`,
`aws s3 cp s3://\${${S3_BUCKET_ENV}}/\${${S3_KEY_ENV}} /tmp`,
`mkdir -p /tmp/scriptdir`,
`unzip /tmp/$(basename \$${S3_KEY_ENV}) -d /tmp/scriptdir`,
]
},
build: {
commands: [
'export SCRIPT_DIR=/tmp/scriptdir',
`echo "Running ${entrypoint}"`,
`chmod +x /tmp/scriptdir/${entrypoint}`,
`/tmp/scriptdir/${entrypoint}`,
]
}
}
});
}

/**
* Construction properties of {@link WindowsBuildImage}.
* Module-private, as the constructor of {@link WindowsBuildImage} is private.
Expand Down
67 changes: 67 additions & 0 deletions packages/@aws-cdk/aws-codebuild/test/test.codebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,73 @@ export = {
test.done();
},

'ARM image': {
'AMAZON_LINUX_2_ARM': {
'has type ARM_CONTAINER and default ComputeType LARGE'(test: Test) {
const stack = new cdk.Stack();
new codebuild.PipelineProject(stack, 'Project', {
environment: {
buildImage: codebuild.LinuxBuildImage.AMAZON_LINUX_2_ARM,
},
});

expect(stack).to(haveResourceLike('AWS::CodeBuild::Project', {
"Environment": {
"Type": "ARM_CONTAINER",
"ComputeType": "BUILD_GENERAL1_LARGE",
},
}));

test.done();
},

'cannot be used in conjunction with ComputeType SMALL'(test: Test) {
const stack = new cdk.Stack();

test.throws(() => {
new codebuild.PipelineProject(stack, 'Project', {
environment: {
buildImage: codebuild.LinuxBuildImage.AMAZON_LINUX_2_ARM,
computeType: codebuild.ComputeType.SMALL,
},
});
}, /ARM images only support ComputeType 'BUILD_GENERAL1_LARGE' - 'BUILD_GENERAL1_SMALL' was given/);

test.done();
},

'cannot be used in conjunction with ComputeType MEDIUM'(test: Test) {
const stack = new cdk.Stack();

test.throws(() => {
new codebuild.PipelineProject(stack, 'Project', {
environment: {
buildImage: codebuild.LinuxBuildImage.AMAZON_LINUX_2_ARM,
computeType: codebuild.ComputeType.MEDIUM,
},
});
}, /ARM images only support ComputeType 'BUILD_GENERAL1_LARGE' - 'BUILD_GENERAL1_MEDIUM' was given/);

test.done();
},

'cannot be used in conjunction with ComputeType X2_LARGE'(test: Test) {
const stack = new cdk.Stack();

test.throws(() => {
new codebuild.PipelineProject(stack, 'Project', {
environment: {
buildImage: codebuild.LinuxBuildImage.AMAZON_LINUX_2_ARM,
computeType: codebuild.ComputeType.X2_LARGE,
},
});
}, /ARM images only support ComputeType 'BUILD_GENERAL1_LARGE' - 'BUILD_GENERAL1_2XLARGE' was given/);

test.done();
},
},
},

'badge support test'(test: Test) {
const stack = new cdk.Stack();

Expand Down

0 comments on commit 6999baf

Please sign in to comment.