-
Notifications
You must be signed in to change notification settings - Fork 2
/
simple-codebuild.ts
60 lines (55 loc) · 2.28 KB
/
simple-codebuild.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { BuildSpec, LinuxBuildImage, Project, Source } from '@aws-cdk/aws-codebuild';
import { SnsTopic } from '@aws-cdk/aws-events-targets';
import { Topic } from '@aws-cdk/aws-sns';
import { EmailSubscription } from '@aws-cdk/aws-sns-subscriptions';
import { App, Stack } from '@aws-cdk/core';
export interface SimpleCodeBuildConfig {
readonly githubOwner : string;
readonly githubRepo : string;
readonly branch? : string;
readonly useBuildSpecFile? : boolean;
readonly alertEmail? : string;
}
export class SimpleCodeBuildStack extends Stack {
constructor(parent : App, config : SimpleCodeBuildConfig) {
super(parent, `${config.githubOwner}-${config.githubRepo}-codebuild`);
this.templateOptions.description = `The CodeBuild project for repo ${config.githubOwner}/${config.githubRepo}`;
const buildSpec = BuildSpec.fromObject({
version: 0.2,
phases: {
pre_build: {
commands: ['npm install'],
},
build: {
commands: [
'npm test',
'npm run build',
],
},
},
});
const alertTopic = new Topic(this, 'AlertTopic', {
displayName: `Alert Topic for repo ${config.githubOwner}/${config.githubRepo}`,
});
if (config.alertEmail) {
alertTopic.addSubscription(new EmailSubscription(config.alertEmail, { json: false }));
}
const source = Source.gitHub({
owner: config.githubOwner,
repo: config.githubRepo,
webhook: true,
reportBuildStatus: true,
});
const buildProject = new Project(this, 'BuildProject', {
source,
badge: true,
buildSpec: config.useBuildSpecFile ? BuildSpec.fromSourceFilename('buildspec.yaml') : buildSpec,
environment: {
buildImage: LinuxBuildImage.UBUNTU_14_04_NODEJS_10_1_0,
},
description: `The CodeBuild project for repo ${config.githubOwner}/${config.githubRepo}`,
projectName: `${config.githubOwner}-${config.githubRepo}`,
});
buildProject.onBuildFailed('BuildFailed', {target: new SnsTopic(alertTopic)});
}
}