Skip to content

Commit

Permalink
fix: No child with id: 'CodeBuild' or 'Infrastructure' (#493)
Browse files Browse the repository at this point in the history
When a builder is created but not used, calling `failedImageBuildsTopic()` raises an exception.

```
No child with id: 'CodeBuild'
```

or

```
No child with id: 'Infrastructure'
```

For example, this would trigger the exceptions:

```
  const app = new cdk.App();
  const stack = new cdk.Stack(app, 'test');

  const vpc = new ec2.Vpc(stack, 'vpc');

  LambdaRunnerProvider.imageBuilder(stack, 'codebuild builder');
  Ec2RunnerProvider.imageBuilder(stack, 'ec2 image builder', { vpc });

  new GitHubRunners(stack, 'runners', {
    providers: [new LambdaRunnerProvider(stack, 'p1' /* not using builder on purpose */)],
  }).failedImageBuildsTopic();

  app.synth();
```

Fixes #490
  • Loading branch information
kichik authored Jan 13, 2024
1 parent 06ffc53 commit e606585
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/image-builders/aws-image-builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,13 @@ export class AwsImageBuilderFailedBuildNotifier implements cdk.IAspect {
public visit(node: IConstruct): void {
if (node instanceof AwsImageBuilderRunnerImageBuilder) {
const builder = node as AwsImageBuilderRunnerImageBuilder;
const infra = builder.node.findChild('Infrastructure') as imagebuilder.CfnInfrastructureConfiguration;
infra.snsTopicArn = this.topic.topicArn;
const infraNode = builder.node.tryFindChild('Infrastructure');
if (infraNode) {
const infra = infraNode as imagebuilder.CfnInfrastructureConfiguration;
infra.snsTopicArn = this.topic.topicArn;
} else {
cdk.Annotations.of(builder).addWarning('Unused builder cannot get notifications of failed builds');
}
}
}
}
9 changes: 7 additions & 2 deletions src/image-builders/codebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,13 @@ export class CodeBuildImageBuilderFailedBuildNotifier implements cdk.IAspect {
public visit(node: IConstruct): void {
if (node instanceof CodeBuildRunnerImageBuilder) {
const builder = node as CodeBuildRunnerImageBuilder;
const project = builder.node.findChild('CodeBuild') as codebuild.Project;
project.notifyOnBuildFailed('BuildFailed', this.topic);
const projectNode = builder.node.tryFindChild('CodeBuild');
if (projectNode) {
const project = projectNode as codebuild.Project;
project.notifyOnBuildFailed('BuildFailed', this.topic);
} else {
cdk.Annotations.of(builder).addWarning('Unused builder cannot get notifications of failed builds');
}
}
}
}
26 changes: 23 additions & 3 deletions test/imagebuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import { aws_ec2 as ec2 } from 'aws-cdk-lib';
import { Match, Template } from 'aws-cdk-lib/assertions';
import {
AmiBuilder,
Architecture, CodeBuildRunnerProvider,
Architecture,
CodeBuildRunnerProvider,
ContainerImageBuilder,
Ec2RunnerProvider,
FargateRunnerProvider, LambdaRunnerProvider,
FargateRunnerProvider,
GitHubRunners,
LambdaRunnerProvider,
Os,
RunnerImageBuilder,
RunnerImageBuilderType, RunnerImageComponent,
RunnerImageBuilderType,
RunnerImageComponent,
} from '../src';

test('AMI builder matching instance type (DEPRECATED)', () => {
Expand Down Expand Up @@ -318,3 +322,19 @@ test('Lambda image builder only accepts AMZL2', () => {
baseDockerImage: 'some-fake-ubuntu-image',
});
});

test('Unused builder doesn\'t throw exceptions', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'test');

const vpc = new ec2.Vpc(stack, 'vpc');

LambdaRunnerProvider.imageBuilder(stack, 'codebuild builder');
Ec2RunnerProvider.imageBuilder(stack, 'ec2 image builder', { vpc });

new GitHubRunners(stack, 'runners', {
providers: [new LambdaRunnerProvider(stack, 'p1' /* not using builder on purpose */)],
}).failedImageBuildsTopic();

app.synth();
});

0 comments on commit e606585

Please sign in to comment.